- Notifications
You must be signed in to change notification settings - Fork1k
feat(cli): implement exp task status command#19533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
11 commits Select commitHold shift + click to select a range
22a81ae
feat(cli): implement exp task status command
johnstcn8aba85d
fix task status
johnstcndaed514
match function name under test
johnstcn89141c1
--watch instead of --follow
johnstcn90a540b
reorder fields
johnstcnbbce2cc
improve output formatting and tests
johnstcn48b2ab7
fixup! improve output formatting and tests
johnstcndb35d83
change json output format
johnstcn5347e61
use full codersdk.Task in json output
johnstcn81692b4
Merge remote-tracking branch 'origin/main' into cj/exp-cli-status
johnstcna301baa
fix task by uuid resolution
johnstcnFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionscli/exp_task.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletionscli/exp_task_status.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package cli | ||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/v2/cli/cliui" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/serpent" | ||
) | ||
func (r *RootCmd) taskStatus() *serpent.Command { | ||
var ( | ||
client = new(codersdk.Client) | ||
formatter = cliui.NewOutputFormatter( | ||
cliui.TableFormat( | ||
[]taskStatusRow{}, | ||
[]string{ | ||
"state changed", | ||
"status", | ||
"state", | ||
"message", | ||
}, | ||
), | ||
cliui.ChangeFormatterData( | ||
cliui.JSONFormat(), | ||
func(data any) (any, error) { | ||
rows, ok := data.([]taskStatusRow) | ||
if !ok { | ||
return nil, xerrors.Errorf("expected []taskStatusRow, got %T", data) | ||
} | ||
if len(rows) != 1 { | ||
return nil, xerrors.Errorf("expected exactly 1 row, got %d", len(rows)) | ||
} | ||
return rows[0], nil | ||
}, | ||
), | ||
) | ||
watchArg bool | ||
watchIntervalArg time.Duration | ||
) | ||
cmd := &serpent.Command{ | ||
Short: "Show the status of a task.", | ||
Use: "status", | ||
Aliases: []string{"stat"}, | ||
Options: serpent.OptionSet{ | ||
{ | ||
Default: "false", | ||
Description: "Watch the task status output. This will stream updates to the terminal until the underlying workspace is stopped.", | ||
Flag: "watch", | ||
Name: "watch", | ||
Value: serpent.BoolOf(&watchArg), | ||
}, | ||
{ | ||
Default: "1s", | ||
Description: "Interval to poll the task for updates. Only used in tests.", | ||
Hidden: true, | ||
Flag: "watch-interval", | ||
Name: "watch-interval", | ||
Value: serpent.DurationOf(&watchIntervalArg), | ||
}, | ||
}, | ||
Middleware: serpent.Chain( | ||
serpent.RequireNArgs(1), | ||
r.InitClient(client), | ||
), | ||
Handler: func(i *serpent.Invocation) error { | ||
ctx := i.Context() | ||
ec := codersdk.NewExperimentalClient(client) | ||
identifier := i.Args[0] | ||
taskID, err := uuid.Parse(identifier) | ||
if err != nil { | ||
// Try to resolve the task as a named workspace | ||
// TODO: right now tasks are still "workspaces" under the hood. | ||
// We should update this once we have a proper task model. | ||
ws, err := namedWorkspace(ctx, client, identifier) | ||
if err != nil { | ||
return err | ||
} | ||
taskID = ws.ID | ||
} | ||
task, err := ec.TaskByID(ctx, taskID) | ||
if err != nil { | ||
return err | ||
} | ||
out, err := formatter.Format(ctx, toStatusRow(task)) | ||
if err != nil { | ||
return xerrors.Errorf("format task status: %w", err) | ||
} | ||
_, _ = fmt.Fprintln(i.Stdout, out) | ||
if !watchArg { | ||
return nil | ||
} | ||
lastStatus := task.Status | ||
lastState := task.CurrentState | ||
t := time.NewTicker(watchIntervalArg) | ||
defer t.Stop() | ||
// TODO: implement streaming updates instead of polling | ||
for range t.C { | ||
task, err := ec.TaskByID(ctx, taskID) | ||
if err != nil { | ||
return err | ||
} | ||
if lastStatus == task.Status && taskStatusEqual(lastState, task.CurrentState) { | ||
continue | ||
} | ||
out, err := formatter.Format(ctx, toStatusRow(task)) | ||
if err != nil { | ||
return xerrors.Errorf("format task status: %w", err) | ||
} | ||
// hack: skip the extra column header from formatter | ||
if formatter.FormatID() != cliui.JSONFormat().ID() { | ||
out = strings.SplitN(out, "\n", 2)[1] | ||
} | ||
_, _ = fmt.Fprintln(i.Stdout, out) | ||
johnstcn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if task.Status == codersdk.WorkspaceStatusStopped { | ||
return nil | ||
} | ||
lastStatus = task.Status | ||
lastState = task.CurrentState | ||
} | ||
return nil | ||
}, | ||
} | ||
formatter.AttachOptions(&cmd.Options) | ||
return cmd | ||
} | ||
func taskStatusEqual(s1, s2 *codersdk.TaskStateEntry) bool { | ||
if s1 == nil && s2 == nil { | ||
return true | ||
} | ||
if s1 == nil || s2 == nil { | ||
return false | ||
} | ||
return s1.State == s2.State | ||
} | ||
type taskStatusRow struct { | ||
codersdk.Task `table:"-"` | ||
ChangedAgo string `json:"-" table:"state changed,default_sort"` | ||
Timestamp time.Time `json:"-" table:"-"` | ||
TaskStatus string `json:"-" table:"status"` | ||
TaskState string `json:"-" table:"state"` | ||
Message string `json:"-" table:"message"` | ||
} | ||
func toStatusRow(task codersdk.Task) []taskStatusRow { | ||
tsr := taskStatusRow{ | ||
Task: task, | ||
ChangedAgo: time.Since(task.UpdatedAt).Truncate(time.Second).String() + " ago", | ||
Timestamp: task.UpdatedAt, | ||
TaskStatus: string(task.Status), | ||
} | ||
if task.CurrentState != nil { | ||
tsr.ChangedAgo = time.Since(task.CurrentState.Timestamp).Truncate(time.Second).String() + " ago" | ||
tsr.Timestamp = task.CurrentState.Timestamp | ||
tsr.TaskState = string(task.CurrentState.State) | ||
tsr.Message = task.CurrentState.Message | ||
} | ||
return []taskStatusRow{tsr} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.