Skip to content

Commit 3f04e98

Browse files
authored
feat(cli): pull templates in zip format (coder#12032)
1 parent 213ae69 commit 3f04e98

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

cli/templatepull.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
func (r *RootCmd) templatePull() *clibase.Cmd {
1818
var (
1919
tarMode bool
20+
zipMode bool
2021
versionName string
2122
)
2223

@@ -39,6 +40,10 @@ func (r *RootCmd) templatePull() *clibase.Cmd {
3940
dest = inv.Args[1]
4041
}
4142

43+
if tarMode && zipMode {
44+
return xerrors.Errorf("either tar or zip can be selected")
45+
}
46+
4247
organization, err := CurrentOrganization(inv, client)
4348
if err != nil {
4449
return xerrors.Errorf("get current organization: %w", err)
@@ -98,17 +103,25 @@ func (r *RootCmd) templatePull() *clibase.Cmd {
98103

99104
cliui.Info(inv.Stderr, "Pulling template version "+cliui.Bold(templateVersion.Name)+"...")
100105

106+
var fileFormat string // empty = default, so .tar
107+
if zipMode {
108+
fileFormat = codersdk.FormatZip
109+
}
110+
101111
// Download the tar archive.
102-
raw, ctype, err := client.Download(ctx, templateVersion.Job.FileID)
112+
raw, ctype, err := client.DownloadWithFormat(ctx, templateVersion.Job.FileID, fileFormat)
103113
if err != nil {
104114
return xerrors.Errorf("download template: %w", err)
105115
}
106116

107-
if ctype != codersdk.ContentTypeTar {
117+
if fileFormat == "" && ctype != codersdk.ContentTypeTar {
108118
return xerrors.Errorf("unexpected Content-Type %q, expecting %q", ctype, codersdk.ContentTypeTar)
109119
}
120+
if fileFormat == codersdk.FormatZip && ctype != codersdk.ContentTypeZip {
121+
return xerrors.Errorf("unexpected Content-Type %q, expecting %q", ctype, codersdk.ContentTypeZip)
122+
}
110123

111-
if tarMode {
124+
if tarMode || zipMode {
112125
_, err = inv.Stdout.Write(raw)
113126
return err
114127
}
@@ -152,6 +165,12 @@ func (r *RootCmd) templatePull() *clibase.Cmd {
152165

153166
Value: clibase.BoolOf(&tarMode),
154167
},
168+
{
169+
Description: "Output the template as a zip archive to stdout.",
170+
Flag: "zip",
171+
172+
Value: clibase.BoolOf(&zipMode),
173+
},
155174
{
156175
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.",
157176
Flag: "version",

cli/templatepull_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli_test
22

33
import (
4+
"archive/tar"
45
"bytes"
56
"context"
67
"crypto/sha256"
@@ -15,6 +16,7 @@ import (
1516
"github.com/stretchr/testify/require"
1617

1718
"github.com/coder/coder/v2/cli/clitest"
19+
"github.com/coder/coder/v2/coderd"
1820
"github.com/coder/coder/v2/coderd/coderdtest"
1921
"github.com/coder/coder/v2/coderd/rbac"
2022
"github.com/coder/coder/v2/provisioner/echo"
@@ -81,6 +83,7 @@ func TestTemplatePull_Stdout(t *testing.T) {
8183
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, updatedVersion.ID)
8284
coderdtest.UpdateActiveTemplateVersion(t, client, template.ID, updatedVersion.ID)
8385

86+
// Verify .tar format
8487
inv, root := clitest.New(t, "templates", "pull", "--tar", template.Name)
8588
clitest.SetupConfig(t, templateAdmin, root)
8689

@@ -89,8 +92,21 @@ func TestTemplatePull_Stdout(t *testing.T) {
8992

9093
err = inv.Run()
9194
require.NoError(t, err)
92-
9395
require.True(t, bytes.Equal(expected, buf.Bytes()), "tar files differ")
96+
97+
// Verify .zip format
98+
tarReader := tar.NewReader(bytes.NewReader(expected))
99+
expectedZip, err := coderd.CreateZipFromTar(tarReader)
100+
require.NoError(t, err)
101+
102+
inv, root = clitest.New(t, "templates", "pull", "--zip", template.Name)
103+
clitest.SetupConfig(t, templateAdmin, root)
104+
buf.Reset()
105+
inv.Stdout = &buf
106+
107+
err = inv.Run()
108+
require.NoError(t, err)
109+
require.True(t, bytes.Equal(expectedZip, buf.Bytes()), "zip files differ")
94110
}
95111

96112
// Stdout tests that 'templates pull' pulls down the non-latest active template

cli/testdata/coder_templates_pull_--help.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ OPTIONS:
1717
-y, --yes bool
1818
Bypass prompts.
1919

20+
--zip bool
21+
Output the template as a zip archive to stdout.
22+
2023
———
2124
Run `coder --help` for a list of global options.

docs/cli/templates_pull.md

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)