diff --git a/cli/templatepull.go b/cli/templatepull.go
index e61d410268c1a..2509cc9df4281 100644
--- a/cli/templatepull.go
+++ b/cli/templatepull.go
@@ -17,6 +17,7 @@ import (
func (r *RootCmd) templatePull() *clibase.Cmd {
var (
tarMode bool
+ zipMode bool
versionName string
)
@@ -39,6 +40,10 @@ func (r *RootCmd) templatePull() *clibase.Cmd {
dest = inv.Args[1]
}
+ if tarMode && zipMode {
+ return xerrors.Errorf("either tar or zip can be selected")
+ }
+
organization, err := CurrentOrganization(inv, client)
if err != nil {
return xerrors.Errorf("get current organization: %w", err)
@@ -98,17 +103,25 @@ func (r *RootCmd) templatePull() *clibase.Cmd {
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.Download(ctx, templateVersion.Job.FileID)
+ raw, ctype, err := client.DownloadWithFormat(ctx, templateVersion.Job.FileID, fileFormat)
if err != nil {
return xerrors.Errorf("download template: %w", err)
}
- if ctype != codersdk.ContentTypeTar {
+ 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 {
+ if tarMode || zipMode {
_, err = inv.Stdout.Write(raw)
return err
}
@@ -152,6 +165,12 @@ func (r *RootCmd) templatePull() *clibase.Cmd {
Value: clibase.BoolOf(&tarMode),
},
+ {
+ Description: "Output the template as a zip archive to stdout.",
+ Flag: "zip",
+
+ Value: clibase.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",
diff --git a/cli/templatepull_test.go b/cli/templatepull_test.go
index 782859c6a93ca..32dd18019dacb 100644
--- a/cli/templatepull_test.go
+++ b/cli/templatepull_test.go
@@ -1,6 +1,7 @@
package cli_test
import (
+ "archive/tar"
"bytes"
"context"
"crypto/sha256"
@@ -15,6 +16,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/cli/clitest"
+ "github.com/coder/coder/v2/coderd"
"github.com/coder/coder/v2/coderd/coderdtest"
"github.com/coder/coder/v2/coderd/rbac"
"github.com/coder/coder/v2/provisioner/echo"
@@ -81,6 +83,7 @@ func TestTemplatePull_Stdout(t *testing.T) {
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, updatedVersion.ID)
coderdtest.UpdateActiveTemplateVersion(t, client, template.ID, updatedVersion.ID)
+ // Verify .tar format
inv, root := clitest.New(t, "templates", "pull", "--tar", template.Name)
clitest.SetupConfig(t, templateAdmin, root)
@@ -89,8 +92,21 @@ func TestTemplatePull_Stdout(t *testing.T) {
err = inv.Run()
require.NoError(t, err)
-
require.True(t, bytes.Equal(expected, buf.Bytes()), "tar files differ")
+
+ // Verify .zip format
+ tarReader := tar.NewReader(bytes.NewReader(expected))
+ expectedZip, err := coderd.CreateZipFromTar(tarReader)
+ require.NoError(t, err)
+
+ inv, root = clitest.New(t, "templates", "pull", "--zip", template.Name)
+ clitest.SetupConfig(t, templateAdmin, root)
+ buf.Reset()
+ inv.Stdout = &buf
+
+ err = inv.Run()
+ require.NoError(t, err)
+ require.True(t, bytes.Equal(expectedZip, buf.Bytes()), "zip files differ")
}
// Stdout tests that 'templates pull' pulls down the non-latest active template
diff --git a/cli/testdata/coder_templates_pull_--help.golden b/cli/testdata/coder_templates_pull_--help.golden
index 65cb302a65a8c..2598e35a303ef 100644
--- a/cli/testdata/coder_templates_pull_--help.golden
+++ b/cli/testdata/coder_templates_pull_--help.golden
@@ -17,5 +17,8 @@ OPTIONS:
-y, --yes bool
Bypass prompts.
+ --zip bool
+ Output the template as a zip archive to stdout.
+
———
Run `coder --help` for a list of global options.
diff --git a/docs/cli/templates_pull.md b/docs/cli/templates_pull.md
index 9ad51ab64c912..b8a36ddcc1898 100644
--- a/docs/cli/templates_pull.md
+++ b/docs/cli/templates_pull.md
@@ -35,3 +35,11 @@ The name of the template version to pull. Use 'active' to pull the active versio
| Type | bool
|
Bypass prompts.
+
+### --zip
+
+| | |
+| ---- | ----------------- |
+| Type | bool
|
+
+Output the template as a zip archive to stdout.