2022-03-22 13:17:50 -06:00
|
|
|
package cli
|
|
|
|
|
|
2022-07-26 18:31:17 -04:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
2022-08-19 02:41:00 +10:00
|
|
|
"time"
|
2022-07-26 18:31:17 -04:00
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/cli/cliui"
|
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2023-10-18 15:08:02 -05:00
|
|
|
"github.com/coder/pretty"
|
2024-03-15 11:24:38 -05:00
|
|
|
"github.com/coder/serpent"
|
2022-07-26 18:31:17 -04:00
|
|
|
)
|
2022-03-22 13:17:50 -06:00
|
|
|
|
2024-03-17 09:45:26 -05:00
|
|
|
func (r *RootCmd) templateVersions() *serpent.Command {
|
|
|
|
|
cmd := &serpent.Command{
|
2022-03-22 13:17:50 -06:00
|
|
|
Use: "versions",
|
2022-07-26 18:31:17 -04:00
|
|
|
Short: "Manage different versions of the specified template",
|
2022-03-22 13:17:50 -06:00
|
|
|
Aliases: []string{"version"},
|
2024-05-21 13:26:34 -05:00
|
|
|
Long: FormatExamples(
|
|
|
|
|
Example{
|
2022-07-26 18:31:17 -04:00
|
|
|
Description: "List versions of a specific template",
|
|
|
|
|
Command: "coder templates versions list my-template",
|
|
|
|
|
},
|
|
|
|
|
),
|
2024-03-15 11:24:38 -05:00
|
|
|
Handler: func(inv *serpent.Invocation) error {
|
2023-03-23 17:42:20 -05:00
|
|
|
return inv.Command.HelpHandler(inv)
|
|
|
|
|
},
|
2024-03-17 09:45:26 -05:00
|
|
|
Children: []*serpent.Command{
|
2023-03-23 17:42:20 -05:00
|
|
|
r.templateVersionsList(),
|
2023-10-11 09:26:22 -05:00
|
|
|
r.archiveTemplateVersion(),
|
|
|
|
|
r.unarchiveTemplateVersion(),
|
2024-10-17 14:45:14 +05:30
|
|
|
r.templateVersionsPromote(),
|
2022-10-06 16:15:28 -04:00
|
|
|
},
|
2022-07-26 18:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 09:45:26 -05:00
|
|
|
func (r *RootCmd) templateVersionsList() *serpent.Command {
|
2023-10-11 09:26:22 -05:00
|
|
|
defaultColumns := []string{
|
2024-08-20 14:47:46 +10:00
|
|
|
"name",
|
|
|
|
|
"created at",
|
|
|
|
|
"created by",
|
|
|
|
|
"status",
|
|
|
|
|
"active",
|
2023-10-11 09:26:22 -05:00
|
|
|
}
|
2023-02-09 04:09:38 +11:00
|
|
|
formatter := cliui.NewOutputFormatter(
|
2023-10-11 09:26:22 -05:00
|
|
|
cliui.TableFormat([]templateVersionRow{}, defaultColumns),
|
2023-02-09 04:09:38 +11:00
|
|
|
cliui.JSONFormat(),
|
|
|
|
|
)
|
2024-07-01 11:04:45 -10:00
|
|
|
orgContext := NewOrganizationContext()
|
2023-02-09 04:09:38 +11:00
|
|
|
|
2024-03-15 11:24:38 -05:00
|
|
|
var includeArchived serpent.Bool
|
2023-10-11 09:26:22 -05:00
|
|
|
|
2024-03-17 09:45:26 -05:00
|
|
|
cmd := &serpent.Command{
|
2023-03-23 17:42:20 -05:00
|
|
|
Use: "list <template>",
|
2024-03-15 11:24:38 -05:00
|
|
|
Middleware: serpent.Chain(
|
|
|
|
|
serpent.RequireNArgs(1),
|
|
|
|
|
func(next serpent.HandlerFunc) serpent.HandlerFunc {
|
|
|
|
|
return func(i *serpent.Invocation) error {
|
2023-10-11 09:26:22 -05:00
|
|
|
// This is the only way to dynamically add the "archived"
|
|
|
|
|
// column if '--include-archived' is true.
|
|
|
|
|
// It does not make sense to show this column if the
|
|
|
|
|
// flag is false.
|
|
|
|
|
if includeArchived {
|
|
|
|
|
for _, opt := range i.Command.Options {
|
|
|
|
|
if opt.Flag == "column" {
|
2024-03-15 11:24:38 -05:00
|
|
|
if opt.ValueSource == serpent.ValueSourceDefault {
|
2024-08-20 14:47:46 +10:00
|
|
|
v, ok := opt.Value.(*serpent.EnumArray)
|
2023-10-11 09:26:22 -05:00
|
|
|
if ok {
|
|
|
|
|
// Add the extra new default column.
|
2024-08-20 14:47:46 +10:00
|
|
|
_ = v.Append("Archived")
|
2023-10-11 09:26:22 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return next(i)
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-03-23 17:42:20 -05:00
|
|
|
),
|
2022-07-26 18:31:17 -04:00
|
|
|
Short: "List all the versions of the specified template",
|
2024-03-15 11:24:38 -05:00
|
|
|
Options: serpent.OptionSet{
|
2023-10-11 09:26:22 -05:00
|
|
|
{
|
|
|
|
|
Name: "include-archived",
|
|
|
|
|
Description: "Include archived versions in the result list.",
|
|
|
|
|
Flag: "include-archived",
|
|
|
|
|
Value: &includeArchived,
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-03-15 11:24:38 -05:00
|
|
|
Handler: func(inv *serpent.Invocation) error {
|
2025-09-22 17:14:07 +04:00
|
|
|
client, err := r.InitClient(inv)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-07-01 11:04:45 -10:00
|
|
|
organization, err := orgContext.Selected(inv, client)
|
2022-07-26 18:31:17 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("get current organization: %w", err)
|
|
|
|
|
}
|
2023-03-23 17:42:20 -05:00
|
|
|
template, err := client.TemplateByName(inv.Context(), organization.ID, inv.Args[0])
|
2022-07-26 18:31:17 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("get template by name: %w", err)
|
|
|
|
|
}
|
|
|
|
|
req := codersdk.TemplateVersionsByTemplateRequest{
|
2023-10-11 09:26:22 -05:00
|
|
|
TemplateID: template.ID,
|
|
|
|
|
IncludeArchived: includeArchived.Value(),
|
2022-07-26 18:31:17 -04:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
versions, err := client.TemplateVersionsByTemplate(inv.Context(), req)
|
2022-07-26 18:31:17 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("get template versions by template: %w", err)
|
|
|
|
|
}
|
2022-08-19 02:41:00 +10:00
|
|
|
|
2023-02-09 04:09:38 +11:00
|
|
|
rows := templateVersionsToRows(template.ActiveVersionID, versions...)
|
2023-03-23 17:42:20 -05:00
|
|
|
out, err := formatter.Format(inv.Context(), rows)
|
2022-08-19 02:41:00 +10:00
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("render table: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 11:32:59 +02:00
|
|
|
if out == "" {
|
|
|
|
|
cliui.Infof(inv.Stderr, "No template versions found.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 17:42:20 -05:00
|
|
|
_, err = fmt.Fprintln(inv.Stdout, out)
|
2022-07-26 18:31:17 -04:00
|
|
|
return err
|
2022-03-22 13:17:50 -06:00
|
|
|
},
|
|
|
|
|
}
|
2023-02-09 04:09:38 +11:00
|
|
|
|
2024-07-01 11:04:45 -10:00
|
|
|
orgContext.AttachOptions(cmd)
|
2023-03-23 17:42:20 -05:00
|
|
|
formatter.AttachOptions(&cmd.Options)
|
2023-02-09 04:09:38 +11:00
|
|
|
return cmd
|
2022-03-22 13:17:50 -06:00
|
|
|
}
|
|
|
|
|
|
2022-08-19 02:41:00 +10:00
|
|
|
type templateVersionRow struct {
|
2023-02-09 04:09:38 +11:00
|
|
|
// For json format:
|
|
|
|
|
TemplateVersion codersdk.TemplateVersion `table:"-"`
|
2026-02-19 09:31:12 +11:00
|
|
|
ActiveJSON bool `json:"active" table:"-"`
|
2023-02-09 04:09:38 +11:00
|
|
|
|
|
|
|
|
// For table format:
|
2026-02-18 16:47:45 +11:00
|
|
|
ID string `json:"-" table:"id"`
|
2023-02-09 04:09:38 +11:00
|
|
|
Name string `json:"-" table:"name,default_sort"`
|
|
|
|
|
CreatedAt time.Time `json:"-" table:"created at"`
|
|
|
|
|
CreatedBy string `json:"-" table:"created by"`
|
|
|
|
|
Status string `json:"-" table:"status"`
|
|
|
|
|
Active string `json:"-" table:"active"`
|
2023-10-11 09:26:22 -05:00
|
|
|
Archived string `json:"-" table:"archived"`
|
2022-08-19 02:41:00 +10:00
|
|
|
}
|
|
|
|
|
|
2023-02-09 04:09:38 +11:00
|
|
|
// templateVersionsToRows converts a list of template versions to a list of rows
|
|
|
|
|
// for outputting.
|
|
|
|
|
func templateVersionsToRows(activeVersionID uuid.UUID, templateVersions ...codersdk.TemplateVersion) []templateVersionRow {
|
2022-08-19 02:41:00 +10:00
|
|
|
rows := make([]templateVersionRow, len(templateVersions))
|
|
|
|
|
for i, templateVersion := range templateVersions {
|
2023-02-18 18:32:09 -06:00
|
|
|
activeStatus := ""
|
2022-07-26 18:31:17 -04:00
|
|
|
if templateVersion.ID == activeVersionID {
|
2023-09-07 17:28:22 -04:00
|
|
|
activeStatus = cliui.Keyword("Active")
|
2022-07-26 18:31:17 -04:00
|
|
|
}
|
2022-08-19 02:41:00 +10:00
|
|
|
|
2023-10-11 09:26:22 -05:00
|
|
|
archivedStatus := ""
|
|
|
|
|
if templateVersion.Archived {
|
|
|
|
|
archivedStatus = pretty.Sprint(cliui.DefaultStyles.Warn, "Archived")
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 02:41:00 +10:00
|
|
|
rows[i] = templateVersionRow{
|
2023-06-12 14:35:51 -05:00
|
|
|
TemplateVersion: templateVersion,
|
2026-02-19 09:31:12 +11:00
|
|
|
ActiveJSON: templateVersion.ID == activeVersionID,
|
2026-02-18 16:47:45 +11:00
|
|
|
ID: templateVersion.ID.String(),
|
2023-06-12 14:35:51 -05:00
|
|
|
Name: templateVersion.Name,
|
|
|
|
|
CreatedAt: templateVersion.CreatedAt,
|
|
|
|
|
CreatedBy: templateVersion.CreatedBy.Username,
|
|
|
|
|
Status: strings.Title(string(templateVersion.Job.Status)),
|
|
|
|
|
Active: activeStatus,
|
2023-10-11 09:26:22 -05:00
|
|
|
Archived: archivedStatus,
|
2022-08-19 02:41:00 +10:00
|
|
|
}
|
2022-07-26 18:31:17 -04:00
|
|
|
}
|
2022-08-19 02:41:00 +10:00
|
|
|
|
2023-02-09 04:09:38 +11:00
|
|
|
return rows
|
2022-07-26 18:31:17 -04:00
|
|
|
}
|
2024-10-17 14:45:14 +05:30
|
|
|
|
|
|
|
|
func (r *RootCmd) templateVersionsPromote() *serpent.Command {
|
|
|
|
|
var (
|
|
|
|
|
templateName string
|
|
|
|
|
templateVersionName string
|
|
|
|
|
orgContext = NewOrganizationContext()
|
|
|
|
|
)
|
|
|
|
|
cmd := &serpent.Command{
|
|
|
|
|
Use: "promote --template=<template_name> --template-version=<template_version_name>",
|
|
|
|
|
Short: "Promote a template version to active.",
|
|
|
|
|
Long: "Promote an existing template version to be the active version for the specified template.",
|
|
|
|
|
Handler: func(inv *serpent.Invocation) error {
|
2025-09-22 17:14:07 +04:00
|
|
|
client, err := r.InitClient(inv)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2024-10-17 14:45:14 +05:30
|
|
|
organization, err := orgContext.Selected(inv, client)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template, err := client.TemplateByName(inv.Context(), organization.ID, templateName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("get template by name: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
version, err := client.TemplateVersionByName(inv.Context(), template.ID, templateVersionName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("get template version by name: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = client.UpdateActiveTemplateVersion(inv.Context(), template.ID, codersdk.UpdateActiveTemplateVersion{
|
|
|
|
|
ID: version.ID,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return xerrors.Errorf("update active template version: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, _ = fmt.Fprintf(inv.Stdout, "Successfully promoted version %q to active for template %q\n", templateVersionName, templateName)
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cmd.Options = serpent.OptionSet{
|
|
|
|
|
{
|
|
|
|
|
Flag: "template",
|
|
|
|
|
FlagShorthand: "t",
|
|
|
|
|
Env: "CODER_TEMPLATE_NAME",
|
|
|
|
|
Description: "Specify the template name.",
|
|
|
|
|
Required: true,
|
|
|
|
|
Value: serpent.StringOf(&templateName),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Flag: "template-version",
|
|
|
|
|
Description: "Specify the template version name to promote.",
|
|
|
|
|
Env: "CODER_TEMPLATE_VERSION_NAME",
|
|
|
|
|
Required: true,
|
|
|
|
|
Value: serpent.StringOf(&templateVersionName),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
orgContext.AttachOptions(cmd)
|
|
|
|
|
return cmd
|
|
|
|
|
}
|