Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 648f647

Browse files
committed
Add environment status to "envs ls" command
1 parent 11d24a7 commit 648f647

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

coder-sdk/env.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ import (
1111

1212
// Environment describes a Coder environment
1313
type Environment struct {
14-
ID string `json:"id" tab:"-"`
15-
Name string `json:"name"`
16-
ImageID string `json:"image_id" tab:"-"`
17-
ImageTag string `json:"image_tag"`
18-
OrganizationID string `json:"organization_id" tab:"-"`
19-
UserID string `json:"user_id" tab:"-"`
20-
LastBuiltAt time.Time `json:"last_built_at" tab:"-"`
21-
CPUCores float32 `json:"cpu_cores"`
22-
MemoryGB int `json:"memory_gb"`
23-
DiskGB int `json:"disk_gb"`
24-
GPUs int `json:"gpus"`
25-
Updating bool `json:"updating"`
14+
ID string `json:"id" tab:"-"`
15+
Name string `json:"name"`
16+
ImageID string `json:"image_id" tab:"-"`
17+
ImageTag string `json:"image_tag"`
18+
OrganizationID string `json:"organization_id" tab:"-"`
19+
UserID string `json:"user_id" tab:"-"`
20+
LastBuiltAt time.Time `json:"last_built_at" tab:"-"`
21+
CPUCores float32 `json:"cpu_cores"`
22+
MemoryGB int `json:"memory_gb"`
23+
DiskGB int `json:"disk_gb"`
24+
GPUs int `json:"gpus"`
25+
Updating bool `json:"updating"`
26+
LatestStat EnvironmentStat `json:"latest_stat" tab:"Status"`
2627
RebuildMessages []struct {
2728
Text string `json:"text"`
2829
Required bool `json:"required"`
@@ -34,6 +35,35 @@ type Environment struct {
3435
AutoOffThreshold xjson.Duration `json:"auto_off_threshold" tab:"-"`
3536
}
3637

38+
// EnvironmentStat represents the state of an environment
39+
type EnvironmentStat struct {
40+
Time time.Time `json:"time"`
41+
LastOnline time.Time `json:"last_online"`
42+
ContainerStatus EnvironmentStatus `json:"container_status"`
43+
StatError string `json:"stat_error"`
44+
CPUUsage float32 `json:"cpu_usage"`
45+
MemoryTotal int64 `json:"memory_total"`
46+
MemoryUsage float32 `json:"memory_usage"`
47+
DiskTotal int64 `json:"disk_total"`
48+
DiskUsed int64 `json:"disk_used"`
49+
}
50+
51+
func (e EnvironmentStat) String() string {
52+
return string(e.ContainerStatus)
53+
}
54+
55+
// EnvironmentStatus refers to the states of an environment.
56+
type EnvironmentStatus string
57+
58+
// The following represent the possible environment container states
59+
const (
60+
EnvironmentCreating EnvironmentStatus = "CREATING"
61+
EnvironmentOff EnvironmentStatus = "OFF"
62+
EnvironmentOn EnvironmentStatus = "ON"
63+
EnvironmentFailed EnvironmentStatus = "FAILED"
64+
EnvironmentUnknown EnvironmentStatus = "UNKNOWN"
65+
)
66+
3767
// CreateEnvironmentRequest is used to configure a new environment
3868
type CreateEnvironmentRequest struct {
3969
Name string `json:"name"`

internal/x/xtabwriter/tabwriter.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func StructValues(data interface{}) string {
2525
if shouldHideField(v.Type().Field(i)) {
2626
continue
2727
}
28-
s.WriteString(fmt.Sprintf("%v\t", v.Field(i).Interface()))
28+
fmt.Fprintf(s, "%v\t", v.Field(i).Interface())
2929
}
3030
return s.String()
3131
}
@@ -41,7 +41,7 @@ func StructFieldNames(data interface{}) string {
4141
if shouldHideField(field) {
4242
continue
4343
}
44-
s.WriteString(fmt.Sprintf("%s\t", field.Name))
44+
fmt.Fprintf(s, "%s\t", fieldName(field))
4545
}
4646
return s.String()
4747
}
@@ -72,6 +72,14 @@ func WriteTable(length int, each func(i int) interface{}) error {
7272
return nil
7373
}
7474

75+
func fieldName(f reflect.StructField) string {
76+
custom, ok := f.Tag.Lookup(structFieldTagKey)
77+
if ok {
78+
return custom
79+
}
80+
return f.Name
81+
}
82+
7583
func shouldHideField(f reflect.StructField) bool {
7684
return f.Tag.Get(structFieldTagKey) == "-"
7785
}

0 commit comments

Comments
 (0)