-
Notifications
You must be signed in to change notification settings - Fork 578
/
templatepull.go
192 lines (161 loc) · 5.13 KB
/
templatepull.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package cli
import (
"bytes"
"fmt"
"os"
"path/filepath"
"sort"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisionersdk"
"github.com/coder/serpent"
)
func (r *RootCmd) templatePull() *serpent.Command {
var (
tarMode bool
zipMode bool
versionName string
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Use: "pull <name> [destination]",
Short: "Download the active, latest, or specified version of a template to a path.",
Middleware: serpent.Chain(
serpent.RequireRangeArgs(1, 2),
r.InitClient(client),
),
Handler: func(inv *serpent.Invocation) error {
var (
ctx = inv.Context()
templateName = inv.Args[0]
dest string
)
if len(inv.Args) > 1 {
dest = inv.Args[1]
}
if tarMode && zipMode {
return xerrors.Errorf("either tar or zip can be selected")
}
organization, err := CurrentOrganization(r, inv, client)
if err != nil {
return xerrors.Errorf("get current organization: %w", err)
}
template, err := client.TemplateByName(ctx, organization.ID, templateName)
if err != nil {
return xerrors.Errorf("get template by name: %w", err)
}
var latestVersion codersdk.TemplateVersion
{
// Determine the latest template version and compare with the
// active version. If they aren't the same, warn the user.
versions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{
TemplateID: template.ID,
})
if err != nil {
return xerrors.Errorf("template versions by template: %w", err)
}
if len(versions) == 0 {
return xerrors.Errorf("no template versions for template %q", templateName)
}
// Sort the slice from newest to oldest template.
sort.SliceStable(versions, func(i, j int) bool {
return versions[i].CreatedAt.After(versions[j].CreatedAt)
})
latestVersion = versions[0]
}
var templateVersion codersdk.TemplateVersion
switch versionName {
case "", "active":
activeVersion, err := client.TemplateVersion(ctx, template.ActiveVersionID)
if err != nil {
return xerrors.Errorf("get active template version: %w", err)
}
if versionName == "" && activeVersion.ID != latestVersion.ID {
cliui.Warn(inv.Stderr,
"A newer template version than the active version exists. Pulling the active version instead.",
"Use "+cliui.Code("--version latest")+" to pull the latest version.",
)
}
templateVersion = activeVersion
case "latest":
templateVersion = latestVersion
default:
version, err := client.TemplateVersionByName(ctx, template.ID, versionName)
if err != nil {
return xerrors.Errorf("get template version: %w", err)
}
templateVersion = version
}
cliui.Info(inv.Stderr, "Pulling template version "+cliui.Bold(templateVersion.Name)+"...")
var fileFormat string // empty = default, so .tar
if zipMode {
fileFormat = codersdk.FormatZip
}
// Download the tar archive.
raw, ctype, err := client.DownloadWithFormat(ctx, templateVersion.Job.FileID, fileFormat)
if err != nil {
return xerrors.Errorf("download template: %w", err)
}
if fileFormat == "" && ctype != codersdk.ContentTypeTar {
return xerrors.Errorf("unexpected Content-Type %q, expecting %q", ctype, codersdk.ContentTypeTar)
}
if fileFormat == codersdk.FormatZip && ctype != codersdk.ContentTypeZip {
return xerrors.Errorf("unexpected Content-Type %q, expecting %q", ctype, codersdk.ContentTypeZip)
}
if tarMode || zipMode {
_, err = inv.Stdout.Write(raw)
return err
}
if dest == "" {
dest = templateName
}
clean, err := filepath.Abs(filepath.Clean(dest))
if err != nil {
return xerrors.Errorf("cleaning destination path %s failed: %w", dest, err)
}
dest = clean
err = os.MkdirAll(dest, 0o750)
if err != nil {
return xerrors.Errorf("mkdirall %q: %w", dest, err)
}
ents, err := os.ReadDir(dest)
if err != nil {
return xerrors.Errorf("read dir %q: %w", dest, err)
}
if len(ents) > 0 {
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Directory %q is not empty, existing files may be overwritten.\nContinue extracting?", dest),
Default: "No",
Secret: false,
IsConfirm: true,
})
if err != nil {
return err
}
}
_, _ = fmt.Fprintf(inv.Stderr, "Extracting template to %q\n", dest)
err = provisionersdk.Untar(dest, bytes.NewReader(raw))
return err
},
}
cmd.Options = serpent.OptionSet{
{
Description: "Output the template as a tar archive to stdout.",
Flag: "tar",
Value: serpent.BoolOf(&tarMode),
},
{
Description: "Output the template as a zip archive to stdout.",
Flag: "zip",
Value: serpent.BoolOf(&zipMode),
},
{
Description: "The name of the template version to pull. Use 'active' to pull the active version, 'latest' to pull the latest version, or the name of the template version to pull.",
Flag: "version",
Value: serpent.StringOf(&versionName),
},
cliui.SkipPromptOption(),
}
return cmd
}