Skip to content

Commit 685512a

Browse files
committed
Add cli command to unarchive a version
1 parent 7498c73 commit 685512a

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

cli/templates.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func (r *RootCmd) templates() *clibase.Cmd {
4444
r.templateDelete(),
4545
r.templatePull(),
4646
r.archiveTemplateVersions(),
47+
r.archiveTemplateVersion(),
4748
},
4849
}
4950

cli/templateversionarchive.go

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,83 @@ import (
1414
"github.com/coder/coder/v2/codersdk"
1515
)
1616

17+
func (r *RootCmd) archiveTemplateVersion() *clibase.Cmd {
18+
var unarchive clibase.Bool
19+
20+
client := new(codersdk.Client)
21+
cmd := &clibase.Cmd{
22+
Use: "archive-version <template-name> [template-version-names...] ",
23+
Short: "Archive or unarchive a template version(s)",
24+
Middleware: clibase.Chain(
25+
r.InitClient(client),
26+
),
27+
Options: clibase.OptionSet{
28+
cliui.SkipPromptOption(),
29+
clibase.Option{
30+
Name: "unarchive",
31+
Description: "Unarchive the selected template version",
32+
Flag: "unarchive",
33+
Value: &unarchive,
34+
},
35+
},
36+
Handler: func(inv *clibase.Invocation) error {
37+
var (
38+
ctx = inv.Context()
39+
versions []codersdk.TemplateVersion
40+
)
41+
42+
organization, err := CurrentOrganization(inv, client)
43+
if err != nil {
44+
return err
45+
}
46+
47+
if len(inv.Args) == 0 {
48+
return xerrors.Errorf("missing template name")
49+
}
50+
if len(inv.Args) < 2 {
51+
return xerrors.Errorf("missing template version name(s)")
52+
}
53+
54+
templateName := inv.Args[0]
55+
template, err := client.TemplateByName(ctx, organization.ID, templateName)
56+
if err != nil {
57+
return xerrors.Errorf("get template by name: %w", err)
58+
}
59+
for _, versionName := range inv.Args[1:] {
60+
version, err := client.TemplateVersionByOrganizationAndName(ctx, organization.ID, template.Name, versionName)
61+
if err != nil {
62+
return xerrors.Errorf("get template version by name %q: %w", versionName, err)
63+
}
64+
versions = append(versions, version)
65+
}
66+
67+
verb := "archived"
68+
if unarchive {
69+
verb = "unarchived"
70+
}
71+
for _, version := range versions {
72+
err := client.SetArchiveTemplateVersion(ctx, version.ID, !unarchive.Value())
73+
if err != nil {
74+
return xerrors.Errorf("set archive to %t template versions for %q: %w", !unarchive, template.Name, err)
75+
}
76+
77+
_, _ = fmt.Fprintln(
78+
inv.Stdout, fmt.Sprintf("Version "+pretty.Sprint(cliui.DefaultStyles.Keyword, version.Name)+" "+verb+" at "+cliui.Timestamp(time.Now())),
79+
)
80+
}
81+
82+
return nil
83+
},
84+
}
85+
86+
return cmd
87+
}
88+
1789
func (r *RootCmd) archiveTemplateVersions() *clibase.Cmd {
1890
var all clibase.Bool
1991
client := new(codersdk.Client)
2092
cmd := &clibase.Cmd{
21-
Use: "archive [name...] ",
93+
Use: "archive-template [template-name...] ",
2294
Short: "Archive unused failed template versions from a given template(s)",
2395
Middleware: clibase.Chain(
2496
r.InitClient(client),

0 commit comments

Comments
 (0)