@@ -14,11 +14,83 @@ import (
14
14
"github.com/coder/coder/v2/codersdk"
15
15
)
16
16
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
+
17
89
func (r * RootCmd ) archiveTemplateVersions () * clibase.Cmd {
18
90
var all clibase.Bool
19
91
client := new (codersdk.Client )
20
92
cmd := & clibase.Cmd {
21
- Use : "archive [ name...] " ,
93
+ Use : "archive-template [template- name...] " ,
22
94
Short : "Archive unused failed template versions from a given template(s)" ,
23
95
Middleware : clibase .Chain (
24
96
r .InitClient (client ),
0 commit comments