-
Notifications
You must be signed in to change notification settings - Fork 897
feat: archive template versions to hide them from the ui #10179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
cc560ca
ba1310a
b804fcf
b9160ae
49b509b
7c09da2
2a2de29
17132e6
105f8c8
e7d8359
fee3680
8677a79
61f3761
6e2a520
a01f797
088a9a1
1000167
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/coder/pretty" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/v2/cli/clibase" | ||
"github.com/coder/coder/v2/cli/cliui" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
|
||
func (r *RootCmd) unarchiveTemplateVersion() *clibase.Cmd { | ||
return r.setArchiveTemplateVersion(false) | ||
} | ||
|
||
func (r *RootCmd) archiveTemplateVersion() *clibase.Cmd { | ||
return r.setArchiveTemplateVersion(true) | ||
} | ||
|
||
//nolint:revive | ||
func (r *RootCmd) setArchiveTemplateVersion(archive bool) *clibase.Cmd { | ||
presentVerb := "archive" | ||
pastVerb := "archived" | ||
if !archive { | ||
presentVerb = "unarchive" | ||
pastVerb = "unarchived" | ||
} | ||
|
||
client := new(codersdk.Client) | ||
cmd := &clibase.Cmd{ | ||
Use: presentVerb + " <template-name> [template-version-names...] ", | ||
Short: strings.ToUpper(string(presentVerb[0])) + presentVerb[1:] + " a template version(s).", | ||
Middleware: clibase.Chain( | ||
r.InitClient(client), | ||
), | ||
Options: clibase.OptionSet{ | ||
cliui.SkipPromptOption(), | ||
}, | ||
Handler: func(inv *clibase.Invocation) error { | ||
var ( | ||
ctx = inv.Context() | ||
versions []codersdk.TemplateVersion | ||
) | ||
|
||
organization, err := CurrentOrganization(inv, client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(inv.Args) == 0 { | ||
return xerrors.Errorf("missing template name") | ||
} | ||
if len(inv.Args) < 2 { | ||
return xerrors.Errorf("missing template version name(s)") | ||
} | ||
|
||
templateName := inv.Args[0] | ||
template, err := client.TemplateByName(ctx, organization.ID, templateName) | ||
if err != nil { | ||
return xerrors.Errorf("get template by name: %w", err) | ||
} | ||
for _, versionName := range inv.Args[1:] { | ||
version, err := client.TemplateVersionByOrganizationAndName(ctx, organization.ID, template.Name, versionName) | ||
if err != nil { | ||
return xerrors.Errorf("get template version by name %q: %w", versionName, err) | ||
} | ||
versions = append(versions, version) | ||
} | ||
|
||
for _, version := range versions { | ||
if version.Archived == archive { | ||
_, _ = fmt.Fprintln( | ||
inv.Stdout, fmt.Sprintf("Version "+pretty.Sprint(cliui.DefaultStyles.Keyword, version.Name)+" already "+pastVerb), | ||
) | ||
continue | ||
} | ||
|
||
err := client.SetArchiveTemplateVersion(ctx, version.ID, archive) | ||
if err != nil { | ||
return xerrors.Errorf("%s template version %q: %w", presentVerb, version.Name, err) | ||
} | ||
|
||
_, _ = fmt.Fprintln( | ||
inv.Stdout, fmt.Sprintf("Version "+pretty.Sprint(cliui.DefaultStyles.Keyword, version.Name)+" "+pastVerb+" at "+cliui.Timestamp(time.Now())), | ||
) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func (r *RootCmd) archiveTemplateVersions() *clibase.Cmd { | ||
var all clibase.Bool | ||
client := new(codersdk.Client) | ||
cmd := &clibase.Cmd{ | ||
Use: "archive [template-name...] ", | ||
Short: "Archive unused failed template versions from a given template(s)", | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Middleware: clibase.Chain( | ||
r.InitClient(client), | ||
), | ||
Options: clibase.OptionSet{ | ||
cliui.SkipPromptOption(), | ||
clibase.Option{ | ||
Name: "all", | ||
Description: "Include all unused template versions. By default, only failed template versions are archived.", | ||
Flag: "all", | ||
Value: &all, | ||
}, | ||
}, | ||
Handler: func(inv *clibase.Invocation) error { | ||
var ( | ||
ctx = inv.Context() | ||
templateNames = []string{} | ||
templates = []codersdk.Template{} | ||
) | ||
|
||
organization, err := CurrentOrganization(inv, client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(inv.Args) > 0 { | ||
templateNames = inv.Args | ||
|
||
for _, templateName := range templateNames { | ||
template, err := client.TemplateByName(ctx, organization.ID, templateName) | ||
if err != nil { | ||
return xerrors.Errorf("get template by name: %w", err) | ||
} | ||
templates = append(templates, template) | ||
} | ||
} else { | ||
template, err := selectTemplate(inv, client, organization) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
templates = append(templates, template) | ||
templateNames = append(templateNames, template.Name) | ||
} | ||
|
||
// Confirm archive of the template. | ||
_, err = cliui.Prompt(inv, cliui.PromptOptions{ | ||
Text: fmt.Sprintf("Archive template versions of these templates: %s?", pretty.Sprint(cliui.DefaultStyles.Code, strings.Join(templateNames, ", "))), | ||
IsConfirm: true, | ||
Default: cliui.ConfirmNo, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, template := range templates { | ||
resp, err := client.ArchiveTemplateVersions(ctx, template.ID, all.Value()) | ||
if err != nil { | ||
return xerrors.Errorf("archive template %q: %w", template.Name, err) | ||
} | ||
|
||
_, _ = fmt.Fprintln( | ||
inv.Stdout, fmt.Sprintf("Archived %d versions from "+pretty.Sprint(cliui.DefaultStyles.Keyword, template.Name)+" at "+cliui.Timestamp(time.Now()), len(resp.ArchivedIDs)), | ||
) | ||
|
||
if ok, _ := inv.ParsedFlags().GetBool("verbose"); err == nil && ok { | ||
data, err := json.Marshal(resp) | ||
if err != nil { | ||
return xerrors.Errorf("marshal verbose response: %w", err) | ||
} | ||
_, _ = fmt.Fprintln( | ||
inv.Stdout, string(data), | ||
) | ||
} | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"github.com/coder/pretty" | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
|
||
|
@@ -29,26 +30,68 @@ func (r *RootCmd) templateVersions() *clibase.Cmd { | |
}, | ||
Children: []*clibase.Cmd{ | ||
r.templateVersionsList(), | ||
r.archiveTemplateVersion(), | ||
r.unarchiveTemplateVersion(), | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func (r *RootCmd) templateVersionsList() *clibase.Cmd { | ||
defaultColumns := []string{ | ||
"Name", | ||
"Created At", | ||
"Created By", | ||
"Status", | ||
"Active", | ||
} | ||
formatter := cliui.NewOutputFormatter( | ||
cliui.TableFormat([]templateVersionRow{}, nil), | ||
cliui.TableFormat([]templateVersionRow{}, defaultColumns), | ||
cliui.JSONFormat(), | ||
) | ||
client := new(codersdk.Client) | ||
|
||
var includeArchived clibase.Bool | ||
|
||
cmd := &clibase.Cmd{ | ||
Use: "list <template>", | ||
Middleware: clibase.Chain( | ||
clibase.RequireNArgs(1), | ||
r.InitClient(client), | ||
func(next clibase.HandlerFunc) clibase.HandlerFunc { | ||
return func(i *clibase.Invocation) error { | ||
// 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" { | ||
if opt.ValueSource == clibase.ValueSourceDefault { | ||
v, ok := opt.Value.(*clibase.StringArray) | ||
if ok { | ||
// Add the extra new default column. | ||
*v = append(*v, "Archived") | ||
} | ||
} | ||
break | ||
} | ||
} | ||
} | ||
return next(i) | ||
} | ||
}, | ||
Comment on lines
+62
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👀 That's cool, didn't know you could do this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, it's a little janky imo, but the value is tied to the option 😢. If we have to do this more often, I can create a helper Middleware to do this more generally. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only adds the column if the user is using the default columns |
||
), | ||
Short: "List all the versions of the specified template", | ||
Options: clibase.OptionSet{ | ||
{ | ||
Name: "include-archived", | ||
Description: "Include archived versions in the result list.", | ||
Flag: "include-archived", | ||
Value: &includeArchived, | ||
}, | ||
}, | ||
Handler: func(inv *clibase.Invocation) error { | ||
organization, err := CurrentOrganization(inv, client) | ||
if err != nil { | ||
|
@@ -59,7 +102,8 @@ func (r *RootCmd) templateVersionsList() *clibase.Cmd { | |
return xerrors.Errorf("get template by name: %w", err) | ||
} | ||
req := codersdk.TemplateVersionsByTemplateRequest{ | ||
TemplateID: template.ID, | ||
TemplateID: template.ID, | ||
IncludeArchived: includeArchived.Value(), | ||
} | ||
|
||
versions, err := client.TemplateVersionsByTemplate(inv.Context(), req) | ||
|
@@ -92,6 +136,7 @@ type templateVersionRow struct { | |
CreatedBy string `json:"-" table:"created by"` | ||
Status string `json:"-" table:"status"` | ||
Active string `json:"-" table:"active"` | ||
Archived string `json:"-" table:"archived"` | ||
} | ||
|
||
// templateVersionsToRows converts a list of template versions to a list of rows | ||
|
@@ -104,13 +149,19 @@ func templateVersionsToRows(activeVersionID uuid.UUID, templateVersions ...coder | |
activeStatus = cliui.Keyword("Active") | ||
} | ||
|
||
archivedStatus := "" | ||
if templateVersion.Archived { | ||
archivedStatus = pretty.Sprint(cliui.DefaultStyles.Warn, "Archived") | ||
} | ||
|
||
rows[i] = templateVersionRow{ | ||
TemplateVersion: templateVersion, | ||
Name: templateVersion.Name, | ||
CreatedAt: templateVersion.CreatedAt, | ||
CreatedBy: templateVersion.CreatedBy.Username, | ||
Status: strings.Title(string(templateVersion.Job.Status)), | ||
Active: activeStatus, | ||
Archived: archivedStatus, | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.