|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | + |
| 6 | +"golang.org/x/xerrors" |
| 7 | + |
| 8 | +"github.com/coder/coder/v2/cli/cliui" |
| 9 | +"github.com/coder/coder/v2/coderd/util/slice" |
| 10 | +"github.com/coder/coder/v2/codersdk" |
| 11 | +"github.com/coder/serpent" |
| 12 | +) |
| 13 | + |
| 14 | +func (r*RootCmd)provisionerJobs()*serpent.Command { |
| 15 | +cmd:=&serpent.Command{ |
| 16 | +Use:"jobs", |
| 17 | +Short:"View and manage provisioner jobs", |
| 18 | +Handler:func(inv*serpent.Invocation)error { |
| 19 | +returninv.Command.HelpHandler(inv) |
| 20 | +}, |
| 21 | +Aliases: []string{"job"}, |
| 22 | +Children: []*serpent.Command{ |
| 23 | +r.provisionerJobsList(), |
| 24 | +}, |
| 25 | +} |
| 26 | +returncmd |
| 27 | +} |
| 28 | + |
| 29 | +func (r*RootCmd)provisionerJobsList()*serpent.Command { |
| 30 | +typeprovisionerJobRowstruct { |
| 31 | +codersdk.ProvisionerJob`table:"provisioner_job,recursive_inline"` |
| 32 | +OrganizationNamestring`json:"organization_name" table:"organization"` |
| 33 | +Queuestring`json:"-" table:"queue"` |
| 34 | +} |
| 35 | + |
| 36 | +var ( |
| 37 | +client=new(codersdk.Client) |
| 38 | +orgContext=NewOrganizationContext() |
| 39 | +formatter=cliui.NewOutputFormatter( |
| 40 | +cliui.TableFormat([]provisionerJobRow{}, []string{"created at","id","organization","status","type","queue","tags"}), |
| 41 | +cliui.JSONFormat(), |
| 42 | +) |
| 43 | +status []string |
| 44 | +limitint64 |
| 45 | +) |
| 46 | + |
| 47 | +cmd:=&serpent.Command{ |
| 48 | +Use:"list", |
| 49 | +Short:"List provisioner jobs", |
| 50 | +Aliases: []string{"ls"}, |
| 51 | +Middleware:serpent.Chain( |
| 52 | +serpent.RequireNArgs(0), |
| 53 | +r.InitClient(client), |
| 54 | +), |
| 55 | +Handler:func(inv*serpent.Invocation)error { |
| 56 | +ctx:=inv.Context() |
| 57 | +org,err:=orgContext.Selected(inv,client) |
| 58 | +iferr!=nil { |
| 59 | +returnxerrors.Errorf("current organization: %w",err) |
| 60 | +} |
| 61 | + |
| 62 | +jobs,err:=client.OrganizationProvisionerJobs(ctx,org.ID,&codersdk.OrganizationProvisionerJobsOptions{ |
| 63 | +Status: slice.StringEnums[codersdk.ProvisionerJobStatus](status), |
| 64 | +Limit:int(limit), |
| 65 | +}) |
| 66 | +iferr!=nil { |
| 67 | +returnxerrors.Errorf("list provisioner jobs: %w",err) |
| 68 | +} |
| 69 | + |
| 70 | +iflen(jobs)==0 { |
| 71 | +_,_=fmt.Fprintln(inv.Stdout,"No provisioner jobs found") |
| 72 | +returnnil |
| 73 | +} |
| 74 | + |
| 75 | +varrows []provisionerJobRow |
| 76 | +for_,job:=rangejobs { |
| 77 | +row:=provisionerJobRow{ |
| 78 | +ProvisionerJob:job, |
| 79 | +OrganizationName:org.HumanName(), |
| 80 | +} |
| 81 | +ifjob.Status==codersdk.ProvisionerJobPending { |
| 82 | +row.Queue=fmt.Sprintf("%d/%d",job.QueuePosition,job.QueueSize) |
| 83 | +} |
| 84 | +rows=append(rows,row) |
| 85 | +} |
| 86 | + |
| 87 | +out,err:=formatter.Format(ctx,rows) |
| 88 | +iferr!=nil { |
| 89 | +returnxerrors.Errorf("display provisioner daemons: %w",err) |
| 90 | +} |
| 91 | + |
| 92 | +_,_=fmt.Fprintln(inv.Stdout,out) |
| 93 | + |
| 94 | +returnnil |
| 95 | +}, |
| 96 | +} |
| 97 | + |
| 98 | +cmd.Options=append(cmd.Options, []serpent.Option{ |
| 99 | +{ |
| 100 | +Flag:"status", |
| 101 | +FlagShorthand:"s", |
| 102 | +Env:"CODER_PROVISIONER_JOB_LIST_STATUS", |
| 103 | +Description:"Filter by job status.", |
| 104 | +Value:serpent.EnumArrayOf(&status,slice.ToStrings(codersdk.ProvisionerJobStatusEnums())...), |
| 105 | +}, |
| 106 | +{ |
| 107 | +Flag:"limit", |
| 108 | +FlagShorthand:"l", |
| 109 | +Env:"CODER_PROVISIONER_JOB_LIST_LIMIT", |
| 110 | +Description:"Limit the number of jobs returned.", |
| 111 | +Default:"50", |
| 112 | +Value:serpent.Int64Of(&limit), |
| 113 | +}, |
| 114 | +}...) |
| 115 | + |
| 116 | +orgContext.AttachOptions(cmd) |
| 117 | +formatter.AttachOptions(&cmd.Options) |
| 118 | + |
| 119 | +returncmd |
| 120 | +} |