mirror of https://github.com/coder/coder.git
feat(cli): pull templates in zip format (#12032)
This commit is contained in:
parent
213ae69bee
commit
3f04e98cfa
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -35,3 +35,11 @@ The name of the template version to pull. Use 'active' to pull the active versio
|
|||
| Type | <code>bool</code> |
|
||||
|
||||
Bypass prompts.
|
||||
|
||||
### --zip
|
||||
|
||||
| | |
|
||||
| ---- | ----------------- |
|
||||
| Type | <code>bool</code> |
|
||||
|
||||
Output the template as a zip archive to stdout.
|
||||
|
|
Loading…
Reference in New Issue