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