|
1 | 1 | package cli
|
2 | 2 |
|
3 |
| -import "github.com/spf13/cobra" |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/jedib0t/go-pretty/v6/table" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "golang.org/x/xerrors" |
| 11 | + |
| 12 | + "github.com/coder/coder/cli/cliui" |
| 13 | + "github.com/coder/coder/codersdk" |
| 14 | +) |
4 | 15 |
|
5 | 16 | func templateVersions() *cobra.Command {
|
6 |
| - return &cobra.Command{ |
| 17 | + cmd := &cobra.Command{ |
7 | 18 | Use: "versions",
|
| 19 | + Short: "Manage different versions of the specified template", |
8 | 20 | Aliases: []string{"version"},
|
| 21 | + Example: formatExamples( |
| 22 | + example{ |
| 23 | + Description: "List versions of a specific template", |
| 24 | + Command: "coder templates versions list my-template", |
| 25 | + }, |
| 26 | + ), |
| 27 | + } |
| 28 | + cmd.AddCommand( |
| 29 | + templateVersionsList(), |
| 30 | + ) |
| 31 | + |
| 32 | + return cmd |
| 33 | +} |
| 34 | + |
| 35 | +func templateVersionsList() *cobra.Command { |
| 36 | + return &cobra.Command{ |
| 37 | + Use: "list <template>", |
| 38 | + Args: cobra.ExactArgs(1), |
| 39 | + Short: "List all the versions of the specified template", |
9 | 40 | RunE: func(cmd *cobra.Command, args []string) error {
|
10 |
| - return nil |
| 41 | + client, err := createClient(cmd) |
| 42 | + if err != nil { |
| 43 | + return xerrors.Errorf("create client: %w", err) |
| 44 | + } |
| 45 | + organization, err := currentOrganization(cmd, client) |
| 46 | + if err != nil { |
| 47 | + return xerrors.Errorf("get current organization: %w", err) |
| 48 | + } |
| 49 | + template, err := client.TemplateByName(cmd.Context(), organization.ID, args[0]) |
| 50 | + if err != nil { |
| 51 | + return xerrors.Errorf("get template by name: %w", err) |
| 52 | + } |
| 53 | + req := codersdk.TemplateVersionsByTemplateRequest{ |
| 54 | + TemplateID: template.ID, |
| 55 | + } |
| 56 | + |
| 57 | + versions, err := client.TemplateVersionsByTemplate(cmd.Context(), req) |
| 58 | + if err != nil { |
| 59 | + return xerrors.Errorf("get template versions by template: %w", err) |
| 60 | + } |
| 61 | + _, err = fmt.Fprintln(cmd.OutOrStdout(), displayTemplateVersions(template.ActiveVersionID, versions...)) |
| 62 | + return err |
11 | 63 | },
|
12 | 64 | }
|
13 | 65 | }
|
14 | 66 |
|
15 |
| -// coder template versions |
| 67 | +// displayTemplateVersions will return a table displaying existing |
| 68 | +// template versions for the specified template. |
| 69 | +func displayTemplateVersions(activeVersionID uuid.UUID, templateVersions ...codersdk.TemplateVersion) string { |
| 70 | + tableWriter := cliui.Table() |
| 71 | + header := table.Row{ |
| 72 | + "Name", "Created At", "Created By", "Status", ""} |
| 73 | + tableWriter.AppendHeader(header) |
| 74 | + for _, templateVersion := range templateVersions { |
| 75 | + var activeStatus = "" |
| 76 | + if templateVersion.ID == activeVersionID { |
| 77 | + activeStatus = cliui.Styles.Code.Render(cliui.Styles.Keyword.Render("Active")) |
| 78 | + } |
| 79 | + tableWriter.AppendRow(table.Row{ |
| 80 | + templateVersion.Name, |
| 81 | + templateVersion.CreatedAt.Format("03:04:05 PM MST on Jan 2, 2006"), |
| 82 | + templateVersion.CreatedByName, |
| 83 | + strings.Title(string(templateVersion.Job.Status)), |
| 84 | + activeStatus, |
| 85 | + }) |
| 86 | + } |
| 87 | + return tableWriter.Render() |
| 88 | +} |
0 commit comments