Skip to content

Commit 540975b

Browse files
committed
prefactor templates pull to extract common code for the templates export (#2651)
1 parent 4f1e9da commit 540975b

File tree

2 files changed

+53
-48
lines changed

2 files changed

+53
-48
lines changed

cli/templatepull.go

+52-48
Original file line numberDiff line numberDiff line change
@@ -13,66 +13,70 @@ import (
1313
"github.com/coder/coder/codersdk"
1414
)
1515

16-
func templatePull() *cobra.Command {
17-
cmd := &cobra.Command{
18-
Use: "pull <name> [destination]",
19-
Short: "Download the latest version of a template to a path.",
20-
Args: cobra.RangeArgs(1, 2),
21-
RunE: func(cmd *cobra.Command, args []string) error {
22-
var (
23-
ctx = cmd.Context()
24-
templateName = args[0]
25-
dest string
26-
)
16+
func fetchTemplateArchiveBytes(cmd *cobra.Command, templateName string) ([]byte, error) {
17+
ctx := cmd.Context()
18+
client, err := createClient(cmd)
19+
if err != nil {
20+
return nil, xerrors.Errorf("create client: %w", err)
21+
}
2722

28-
if len(args) > 1 {
29-
dest = args[1]
30-
}
23+
// TODO(JonA): Do we need to add a flag for organization?
24+
organization, err := currentOrganization(cmd, client)
25+
if err != nil {
26+
return nil, xerrors.Errorf("current organization: %w", err)
27+
}
3128

32-
client, err := createClient(cmd)
33-
if err != nil {
34-
return xerrors.Errorf("create client: %w", err)
35-
}
29+
template, err := client.TemplateByName(ctx, organization.ID, templateName)
30+
if err != nil {
31+
return nil, xerrors.Errorf("template by name: %w", err)
32+
}
3633

37-
// TODO(JonA): Do we need to add a flag for organization?
38-
organization, err := currentOrganization(cmd, client)
39-
if err != nil {
40-
return xerrors.Errorf("current organization: %w", err)
41-
}
34+
// Pull the versions for the template. We'll find the latest
35+
// one and download the source.
36+
versions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{
37+
TemplateID: template.ID,
38+
})
39+
if err != nil {
40+
return nil, xerrors.Errorf("template versions by template: %w", err)
41+
}
4242

43-
template, err := client.TemplateByName(ctx, organization.ID, templateName)
44-
if err != nil {
45-
return xerrors.Errorf("template by name: %w", err)
46-
}
43+
if len(versions) == 0 {
44+
return nil, xerrors.Errorf("no template versions for template %q", templateName)
45+
}
4746

48-
// Pull the versions for the template. We'll find the latest
49-
// one and download the source.
50-
versions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{
51-
TemplateID: template.ID,
52-
})
53-
if err != nil {
54-
return xerrors.Errorf("template versions by template: %w", err)
55-
}
47+
// Sort the slice from newest to oldest template.
48+
sort.SliceStable(versions, func(i, j int) bool {
49+
return versions[i].CreatedAt.After(versions[j].CreatedAt)
50+
})
5651

57-
if len(versions) == 0 {
58-
return xerrors.Errorf("no template versions for template %q", templateName)
59-
}
52+
latest := versions[0]
6053

61-
// Sort the slice from newest to oldest template.
62-
sort.SliceStable(versions, func(i, j int) bool {
63-
return versions[i].CreatedAt.After(versions[j].CreatedAt)
64-
})
54+
// Download the tar archive.
55+
raw, ctype, err := client.Download(ctx, latest.Job.StorageSource)
56+
if err != nil {
57+
return nil, xerrors.Errorf("download template: %w", err)
58+
}
6559

66-
latest := versions[0]
60+
if ctype != codersdk.ContentTypeTar {
61+
return nil, xerrors.Errorf("unexpected Content-Type %q, expecting %q", ctype, codersdk.ContentTypeTar)
62+
}
63+
return raw, nil
64+
}
6765

68-
// Download the tar archive.
69-
raw, ctype, err := client.Download(ctx, latest.Job.StorageSource)
66+
func templatePull() *cobra.Command {
67+
cmd := &cobra.Command{
68+
Use: "pull <name> [destination]",
69+
Short: "Download the latest version of a template to a path.",
70+
Args: cobra.RangeArgs(1, 2),
71+
RunE: func(cmd *cobra.Command, args []string) error {
72+
raw, err := fetchTemplateArchiveBytes(cmd, args[0])
7073
if err != nil {
71-
return xerrors.Errorf("download template: %w", err)
74+
return err
7275
}
7376

74-
if ctype != codersdk.ContentTypeTar {
75-
return xerrors.Errorf("unexpected Content-Type %q, expecting %q", ctype, codersdk.ContentTypeTar)
77+
var dest string
78+
if len(args) > 1 {
79+
dest = args[1]
7680
}
7781

7882
// If the destination is empty then we write to stdout

cli/templates.go

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func templates() *cobra.Command {
3232
cmd.AddCommand(
3333
templateCreate(),
3434
templateEdit(),
35+
templateExport(),
3536
templateInit(),
3637
templateList(),
3738
templatePlan(),

0 commit comments

Comments
 (0)