|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"strings" |
| 6 | +"time" |
| 7 | + |
| 8 | +"github.com/google/uuid" |
| 9 | +"golang.org/x/xerrors" |
| 10 | + |
| 11 | +"github.com/coder/coder/v2/cli/cliui" |
| 12 | +"github.com/coder/coder/v2/codersdk" |
| 13 | +"github.com/coder/serpent" |
| 14 | +) |
| 15 | + |
| 16 | +func (r*RootCmd)taskStatus()*serpent.Command { |
| 17 | +var ( |
| 18 | +client=new(codersdk.Client) |
| 19 | +formatter=cliui.NewOutputFormatter( |
| 20 | +cliui.TableFormat( |
| 21 | +[]taskStatusRow{}, |
| 22 | +[]string{ |
| 23 | +"state changed", |
| 24 | +"status", |
| 25 | +"state", |
| 26 | +"message", |
| 27 | +}, |
| 28 | +), |
| 29 | +cliui.ChangeFormatterData( |
| 30 | +cliui.JSONFormat(), |
| 31 | +func(dataany) (any,error) { |
| 32 | +rows,ok:=data.([]taskStatusRow) |
| 33 | +if!ok { |
| 34 | +returnnil,xerrors.Errorf("expected []taskStatusRow, got %T",data) |
| 35 | +} |
| 36 | +iflen(rows)!=1 { |
| 37 | +returnnil,xerrors.Errorf("expected exactly 1 row, got %d",len(rows)) |
| 38 | +} |
| 39 | +returnrows[0],nil |
| 40 | +}, |
| 41 | +), |
| 42 | +) |
| 43 | +watchArgbool |
| 44 | +watchIntervalArg time.Duration |
| 45 | +) |
| 46 | +cmd:=&serpent.Command{ |
| 47 | +Short:"Show the status of a task.", |
| 48 | +Use:"status", |
| 49 | +Aliases: []string{"stat"}, |
| 50 | +Options: serpent.OptionSet{ |
| 51 | +{ |
| 52 | +Default:"false", |
| 53 | +Description:"Watch the task status output. This will stream updates to the terminal until the underlying workspace is stopped.", |
| 54 | +Flag:"watch", |
| 55 | +Name:"watch", |
| 56 | +Value:serpent.BoolOf(&watchArg), |
| 57 | +}, |
| 58 | +{ |
| 59 | +Default:"1s", |
| 60 | +Description:"Interval to poll the task for updates. Only used in tests.", |
| 61 | +Hidden:true, |
| 62 | +Flag:"watch-interval", |
| 63 | +Name:"watch-interval", |
| 64 | +Value:serpent.DurationOf(&watchIntervalArg), |
| 65 | +}, |
| 66 | +}, |
| 67 | +Middleware:serpent.Chain( |
| 68 | +serpent.RequireNArgs(1), |
| 69 | +r.InitClient(client), |
| 70 | +), |
| 71 | +Handler:func(i*serpent.Invocation)error { |
| 72 | +ctx:=i.Context() |
| 73 | +ec:=codersdk.NewExperimentalClient(client) |
| 74 | +identifier:=i.Args[0] |
| 75 | + |
| 76 | +taskID,err:=uuid.Parse(identifier) |
| 77 | +iferr!=nil { |
| 78 | +// Try to resolve the task as a named workspace |
| 79 | +// TODO: right now tasks are still "workspaces" under the hood. |
| 80 | +// We should update this once we have a proper task model. |
| 81 | +ws,err:=namedWorkspace(ctx,client,identifier) |
| 82 | +iferr!=nil { |
| 83 | +returnerr |
| 84 | +} |
| 85 | +taskID=ws.ID |
| 86 | +} |
| 87 | +task,err:=ec.TaskByID(ctx,taskID) |
| 88 | +iferr!=nil { |
| 89 | +returnerr |
| 90 | +} |
| 91 | + |
| 92 | +out,err:=formatter.Format(ctx,toStatusRow(task)) |
| 93 | +iferr!=nil { |
| 94 | +returnxerrors.Errorf("format task status: %w",err) |
| 95 | +} |
| 96 | +_,_=fmt.Fprintln(i.Stdout,out) |
| 97 | + |
| 98 | +if!watchArg { |
| 99 | +returnnil |
| 100 | +} |
| 101 | + |
| 102 | +lastStatus:=task.Status |
| 103 | +lastState:=task.CurrentState |
| 104 | +t:=time.NewTicker(watchIntervalArg) |
| 105 | +defert.Stop() |
| 106 | +// TODO: implement streaming updates instead of polling |
| 107 | +forranget.C { |
| 108 | +task,err:=ec.TaskByID(ctx,taskID) |
| 109 | +iferr!=nil { |
| 110 | +returnerr |
| 111 | +} |
| 112 | +iflastStatus==task.Status&&taskStatusEqual(lastState,task.CurrentState) { |
| 113 | +continue |
| 114 | +} |
| 115 | +out,err:=formatter.Format(ctx,toStatusRow(task)) |
| 116 | +iferr!=nil { |
| 117 | +returnxerrors.Errorf("format task status: %w",err) |
| 118 | +} |
| 119 | +// hack: skip the extra column header from formatter |
| 120 | +ifformatter.FormatID()!=cliui.JSONFormat().ID() { |
| 121 | +out=strings.SplitN(out,"\n",2)[1] |
| 122 | +} |
| 123 | +_,_=fmt.Fprintln(i.Stdout,out) |
| 124 | + |
| 125 | +iftask.Status==codersdk.WorkspaceStatusStopped { |
| 126 | +returnnil |
| 127 | +} |
| 128 | +lastStatus=task.Status |
| 129 | +lastState=task.CurrentState |
| 130 | +} |
| 131 | +returnnil |
| 132 | +}, |
| 133 | +} |
| 134 | +formatter.AttachOptions(&cmd.Options) |
| 135 | +returncmd |
| 136 | +} |
| 137 | + |
| 138 | +functaskStatusEqual(s1,s2*codersdk.TaskStateEntry)bool { |
| 139 | +ifs1==nil&&s2==nil { |
| 140 | +returntrue |
| 141 | +} |
| 142 | +ifs1==nil||s2==nil { |
| 143 | +returnfalse |
| 144 | +} |
| 145 | +returns1.State==s2.State |
| 146 | +} |
| 147 | + |
| 148 | +typetaskStatusRowstruct { |
| 149 | +codersdk.Task`table:"-"` |
| 150 | +ChangedAgostring`json:"-" table:"state changed,default_sort"` |
| 151 | +Timestamp time.Time`json:"-" table:"-"` |
| 152 | +TaskStatusstring`json:"-" table:"status"` |
| 153 | +TaskStatestring`json:"-" table:"state"` |
| 154 | +Messagestring`json:"-" table:"message"` |
| 155 | +} |
| 156 | + |
| 157 | +functoStatusRow(task codersdk.Task) []taskStatusRow { |
| 158 | +tsr:=taskStatusRow{ |
| 159 | +Task:task, |
| 160 | +ChangedAgo:time.Since(task.UpdatedAt).Truncate(time.Second).String()+" ago", |
| 161 | +Timestamp:task.UpdatedAt, |
| 162 | +TaskStatus:string(task.Status), |
| 163 | +} |
| 164 | +iftask.CurrentState!=nil { |
| 165 | +tsr.ChangedAgo=time.Since(task.CurrentState.Timestamp).Truncate(time.Second).String()+" ago" |
| 166 | +tsr.Timestamp=task.CurrentState.Timestamp |
| 167 | +tsr.TaskState=string(task.CurrentState.State) |
| 168 | +tsr.Message=task.CurrentState.Message |
| 169 | +} |
| 170 | +return []taskStatusRow{tsr} |
| 171 | +} |