-
Notifications
You must be signed in to change notification settings - Fork 18
Minor clean / tidying up the code #113
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,14 @@ import ( | |
"go.coder.com/flog" | ||
) | ||
|
||
var ( | ||
version string = "unknown" | ||
) | ||
// Using a global for the version so it can be set at build time using ldflags. | ||
var version = "unknown" | ||
|
||
func main() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
// If requested, spin up the pprof webserver. | ||
if os.Getenv("PPROF") != "" { | ||
go func() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wish we put panic handlers in all go routines. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, panics are developer errors, not really expected in production. As a safety net on a service to log/alert about a bug makes sense, and in general, for anything expected to be long-running, I do agree, we should have panic handler, but here, we are in a CLI and specifically in a debug handler, so it is probably not necessary. |
||
log.Println(http.ListenAndServe("localhost:6060", nil)) | ||
|
@@ -31,15 +31,19 @@ func main() { | |
|
||
stdoutState, err := xterminal.MakeOutputRaw(os.Stdout.Fd()) | ||
if err != nil { | ||
flog.Fatal("set output to raw: %v", err) | ||
flog.Fatal("set output to raw: %s", err) | ||
} | ||
defer xterminal.Restore(os.Stdout.Fd(), stdoutState) | ||
defer func() { | ||
// Best effort. Would result in broken terminal on window but nothing we can do about it. | ||
_ = xterminal.Restore(os.Stdout.Fd(), stdoutState) | ||
}() | ||
|
||
app := cmd.Make() | ||
app.Version = fmt.Sprintf("%s %s %s/%s", version, runtime.Version(), runtime.GOOS, runtime.GOARCH) | ||
|
||
err = app.ExecuteContext(ctx) | ||
if err != nil { | ||
if err := app.ExecuteContext(ctx); err != nil { | ||
// NOTE: The returned error is already handled and logged by the cmd lib (cobra), so no need to re-handle it here. | ||
// As we are in the main, if there was an error, exit the process with an error code. | ||
os.Exit(1) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,13 +31,18 @@ type Environment struct { | |
UpdatedAt time.Time `json:"updated_at" tab:"-"` | ||
LastOpenedAt time.Time `json:"last_opened_at" tab:"-"` | ||
LastConnectionAt time.Time `json:"last_connection_at" tab:"-"` | ||
AutoOffThreshold xjson.Duration `json:"auto_off_threshold" tab:"-"` | ||
AutoOffThreshold xjson.MSDuration `json:"auto_off_threshold" tab:"-"` | ||
} | ||
|
||
// RebuildMessage defines the message shown when an Environment requires a rebuild for it can be accessed. | ||
type RebuildMessage struct { | ||
Text string `json:"text"` | ||
Required bool `json:"required"` | ||
Text string `json:"text"` | ||
Required bool `json:"required"` | ||
AutoOffThreshold xjson.MSDuration `json:"auto_off_threshold" tab:"-"` | ||
RebuildMessages []struct { | ||
Text string `json:"text"` | ||
Required bool `json:"required"` | ||
} `json:"rebuild_messages" tab:"-"` | ||
} | ||
|
||
// EnvironmentStat represents the state of an environment | ||
|
@@ -53,9 +58,7 @@ type EnvironmentStat struct { | |
DiskUsed int64 `json:"disk_used"` | ||
} | ||
|
||
func (e EnvironmentStat) String() string { | ||
return string(e.ContainerStatus) | ||
} | ||
func (e EnvironmentStat) String() string { return string(e.ContainerStatus) } | ||
|
||
// EnvironmentStatus refers to the states of an environment. | ||
type EnvironmentStatus string | ||
|
@@ -69,7 +72,7 @@ const ( | |
EnvironmentUnknown EnvironmentStatus = "UNKNOWN" | ||
) | ||
|
||
// CreateEnvironmentRequest is used to configure a new environment | ||
// CreateEnvironmentRequest is used to configure a new environment. | ||
type CreateEnvironmentRequest struct { | ||
Name string `json:"name"` | ||
ImageID string `json:"image_id"` | ||
|
@@ -84,61 +87,50 @@ type CreateEnvironmentRequest struct { | |
// CreateEnvironment sends a request to create an environment. | ||
func (c Client) CreateEnvironment(ctx context.Context, orgID string, req CreateEnvironmentRequest) (*Environment, error) { | ||
var env Environment | ||
err := c.requestBody( | ||
ctx, | ||
http.MethodPost, "/api/orgs/"+orgID+"/environments", | ||
req, | ||
&env, | ||
) | ||
return &env, err | ||
if err := c.requestBody(ctx, http.MethodPost, "/api/orgs/"+orgID+"/environments", req, &env); err != nil { | ||
tychoish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, err | ||
} | ||
return &env, nil | ||
} | ||
|
||
// EnvironmentsByOrganization gets the list of environments owned by the given user. | ||
func (c Client) EnvironmentsByOrganization(ctx context.Context, userID, orgID string) ([]Environment, error) { | ||
var envs []Environment | ||
err := c.requestBody( | ||
ctx, | ||
http.MethodGet, "/api/orgs/"+orgID+"/members/"+userID+"/environments", | ||
nil, | ||
&envs, | ||
) | ||
return envs, err | ||
if err := c.requestBody(ctx, http.MethodGet, "/api/orgs/"+orgID+"/members/"+userID+"/environments", nil, &envs); err != nil { | ||
return nil, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we be wrapping these as discussed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When there is only one error path, imho it is not necessary, but more is better, I'll add it. |
||
} | ||
return envs, nil | ||
} | ||
|
||
// DeleteEnvironment deletes the environment. | ||
func (c Client) DeleteEnvironment(ctx context.Context, envID string) error { | ||
return c.requestBody( | ||
ctx, | ||
http.MethodDelete, "/api/environments/"+envID, | ||
nil, | ||
nil, | ||
) | ||
return c.requestBody(ctx, http.MethodDelete, "/api/environments/"+envID, nil, nil) | ||
} | ||
|
||
// DialWsep dials an environments command execution interface | ||
// See github.com/cdr/wsep for details | ||
// See https://github.com/cdr/wsep for details. | ||
func (c Client) DialWsep(ctx context.Context, env *Environment) (*websocket.Conn, error) { | ||
return c.dialWs(ctx, "/proxy/environments/"+env.ID+"/wsep") | ||
return c.dialWebsocket(ctx, "/proxy/environments/"+env.ID+"/wsep") | ||
} | ||
|
||
// DialIDEStatus opens a websocket connection for cpu load metrics on the environment | ||
func (c Client) DialIDEStatus(ctx context.Context, envID string) (*websocket.Conn, error) { | ||
return c.dialWs(ctx, "/proxy/environments/"+envID+"/ide/api/status") | ||
return c.dialWebsocket(ctx, "/proxy/environments/"+envID+"/ide/api/status") | ||
} | ||
|
||
// DialEnvironmentBuildLog opens a websocket connection for the environment build log messages | ||
// DialEnvironmentBuildLog opens a websocket connection for the environment build log messages. | ||
func (c Client) DialEnvironmentBuildLog(ctx context.Context, envID string) (*websocket.Conn, error) { | ||
return c.dialWs(ctx, "/api/environments/"+envID+"/watch-update") | ||
return c.dialWebsocket(ctx, "/api/environments/"+envID+"/watch-update") | ||
} | ||
|
||
// DialEnvironmentStats opens a websocket connection for environment stats | ||
// DialEnvironmentStats opens a websocket connection for environment stats. | ||
func (c Client) DialEnvironmentStats(ctx context.Context, envID string) (*websocket.Conn, error) { | ||
return c.dialWs(ctx, "/api/environments/"+envID+"/watch-stats") | ||
return c.dialWebsocket(ctx, "/api/environments/"+envID+"/watch-stats") | ||
} | ||
|
||
// DialResourceLoad opens a websocket connection for cpu load metrics on the environment | ||
func (c Client) DialResourceLoad(ctx context.Context, envID string) (*websocket.Conn, error) { | ||
return c.dialWs(ctx, "/api/environments/"+envID+"/watch-resource-load") | ||
return c.dialWebsocket(ctx, "/api/environments/"+envID+"/watch-resource-load") | ||
} | ||
|
||
// BuildLogType describes the type of an event. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that one is particularly embarrassing... leftover from when it was MacOS and Linux. 😭