Skip to content

feat(cli): pull templates in zip format #12032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions cli/templatepull.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
func (r *RootCmd) templatePull() *clibase.Cmd {
var (
tarMode bool
zipMode bool
versionName string
)

Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 17 additions & 1 deletion cli/templatepull_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli_test

import (
"archive/tar"
"bytes"
"context"
"crypto/sha256"
Expand All @@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions cli/testdata/coder_templates_pull_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 8 additions & 0 deletions docs/cli/templates_pull.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.