diff --git a/cli/templatepull.go b/cli/templatepull.go index 72a69de39e49b..efcaa559e9595 100644 --- a/cli/templatepull.go +++ b/cli/templatepull.go @@ -1,11 +1,12 @@ package cli import ( + "bytes" "fmt" - "io/fs" "os" "sort" + "github.com/codeclysm/extract" "github.com/spf13/cobra" "golang.org/x/xerrors" @@ -14,6 +15,7 @@ import ( ) func templatePull() *cobra.Command { + var tarMode bool cmd := &cobra.Command{ Use: "pull [destination]", Short: "Download the latest version of a template to a path.", @@ -75,48 +77,44 @@ func templatePull() *cobra.Command { return xerrors.Errorf("unexpected Content-Type %q, expecting %q", ctype, codersdk.ContentTypeTar) } - // If the destination is empty then we write to stdout - // and bail early. - if dest == "" { + if tarMode { _, err = cmd.OutOrStdout().Write(raw) - if err != nil { - return xerrors.Errorf("write stdout: %w", err) - } - return nil + return err } - // Stat the destination to ensure nothing exists already. - fi, err := os.Stat(dest) - if err != nil && !xerrors.Is(err, fs.ErrNotExist) { - return xerrors.Errorf("stat destination: %w", err) + if dest == "" { + dest = templateName + "/" } - if fi != nil && fi.IsDir() { - // If the destination is a directory we just bail. - return xerrors.Errorf("%q already exists.", dest) + err = os.MkdirAll(dest, 0o750) + if err != nil { + return xerrors.Errorf("mkdirall %q: %w", dest, err) } - // If a file exists at the destination prompt the user - // to ensure we don't overwrite something valuable. - if fi != nil { + ents, err := os.ReadDir(dest) + if err != nil { + return xerrors.Errorf("read dir %q: %w", dest, err) + } + + if len(ents) > 0 { _, err = cliui.Prompt(cmd, cliui.PromptOptions{ - Text: fmt.Sprintf("%q already exists, do you want to overwrite it?", dest), + Text: fmt.Sprintf("Directory %q is not empty, existing files may be overwritten.\nContinue extracting?", dest), + Default: "No", + Secret: false, IsConfirm: true, }) if err != nil { - return xerrors.Errorf("parse prompt: %w", err) + return err } } - err = os.WriteFile(dest, raw, 0o600) - if err != nil { - return xerrors.Errorf("write to path: %w", err) - } - - return nil + _, _ = fmt.Fprintf(cmd.OutOrStderr(), "Extracting template to %q\n", dest) + err = extract.Tar(ctx, bytes.NewReader(raw), dest, nil) + return err }, } + cmd.Flags().BoolVar(&tarMode, "tar", false, "output the template as a tar archive to stdout") cliui.AllowSkipPrompt(cmd) return cmd diff --git a/cli/templatepull_test.go b/cli/templatepull_test.go index f19725c21fe8a..d586b95a4593c 100644 --- a/cli/templatepull_test.go +++ b/cli/templatepull_test.go @@ -2,11 +2,15 @@ package cli_test import ( "bytes" + "context" + "io" "os" "path/filepath" "testing" + "github.com/codeclysm/extract" "github.com/google/uuid" + "github.com/ory/dockertest/v3/docker/pkg/archive" "github.com/stretchr/testify/require" "github.com/coder/coder/cli/clitest" @@ -53,7 +57,7 @@ func TestTemplatePull(t *testing.T) { // are being sorted correctly. _ = coderdtest.UpdateTemplateVersion(t, client, user.OrganizationID, source2, template.ID) - cmd, root := clitest.New(t, "templates", "pull", template.Name) + cmd, root := clitest.New(t, "templates", "pull", "--tar", template.Name) clitest.SetupConfig(t, client, root) var buf bytes.Buffer @@ -65,9 +69,9 @@ func TestTemplatePull(t *testing.T) { require.True(t, bytes.Equal(expected, buf.Bytes()), "tar files differ") }) - // ToFile tests that 'templates pull' pulls down the latest template + // ToDir tests that 'templates pull' pulls down the latest template // and writes it to the correct directory. - t.Run("ToFile", func(t *testing.T) { + t.Run("ToDir", func(t *testing.T) { t.Parallel() client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) @@ -93,15 +97,14 @@ func TestTemplatePull(t *testing.T) { dir := t.TempDir() - dest := filepath.Join(dir, "actual.tar") + expectedDest := filepath.Join(dir, "expected") + actualDest := filepath.Join(dir, "actual") + ctx := context.Background() - // Create the file so that we can test that the command - // warns the user before overwriting a preexisting file. - fi, err := os.OpenFile(dest, os.O_CREATE|os.O_RDONLY, 0o600) + err = extract.Tar(ctx, bytes.NewReader(expected), expectedDest, nil) require.NoError(t, err) - _ = fi.Close() - cmd, root := clitest.New(t, "templates", "pull", template.Name, dest) + cmd, root := clitest.New(t, "templates", "pull", template.Name, actualDest) clitest.SetupConfig(t, client, root) pty := ptytest.New(t) @@ -114,16 +117,89 @@ func TestTemplatePull(t *testing.T) { errChan <- cmd.Execute() }() - // We expect to be prompted that a file already exists. - pty.ExpectMatch("already exists") - pty.WriteLine("yes") - require.NoError(t, <-errChan) - actual, err := os.ReadFile(dest) + expectedTarRd, err := archive.Tar(expectedDest, archive.Uncompressed) + require.NoError(t, err) + expectedTar, err := io.ReadAll(expectedTarRd) + require.NoError(t, err) + + actualTarRd, err := archive.Tar(actualDest, archive.Uncompressed) + require.NoError(t, err) + + actualTar, err := io.ReadAll(actualTarRd) + require.NoError(t, err) + + require.True(t, bytes.Equal(expectedTar, actualTar), "tar files differ") + }) + + // FolderConflict tests that 'templates pull' fails when a folder with has + // existing + t.Run("FolderConflict", func(t *testing.T) { + t.Parallel() + + client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true}) + user := coderdtest.CreateFirstUser(t, client) + + // Create an initial template bundle. + source1 := genTemplateVersionSource() + // Create an updated template bundle. This will be used to ensure + // that templates are correctly returned in order from latest to oldest. + source2 := genTemplateVersionSource() + + expected, err := echo.Tar(source2) + require.NoError(t, err) + + version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, source1) + _ = coderdtest.AwaitTemplateVersionJob(t, client, version1.ID) + + template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version1.ID) + + // Update the template version so that we can assert that templates + // are being sorted correctly. + _ = coderdtest.UpdateTemplateVersion(t, client, user.OrganizationID, source2, template.ID) + + dir := t.TempDir() + + expectedDest := filepath.Join(dir, "expected") + conflictDest := filepath.Join(dir, "conflict") + + err = os.MkdirAll(conflictDest, 0o700) + require.NoError(t, err) + + err = os.WriteFile( + filepath.Join(conflictDest, "conflict-file"), + []byte("conflict"), 0o600, + ) + require.NoError(t, err) + + ctx := context.Background() + + err = extract.Tar(ctx, bytes.NewReader(expected), expectedDest, nil) + require.NoError(t, err) + + cmd, root := clitest.New(t, "templates", "pull", template.Name, conflictDest) + clitest.SetupConfig(t, client, root) + + pty := ptytest.New(t) + cmd.SetIn(pty.Input()) + cmd.SetOut(pty.Output()) + + errChan := make(chan error) + go func() { + defer close(errChan) + errChan <- cmd.Execute() + }() + + pty.ExpectMatch("not empty") + pty.WriteLine("no") + + require.Error(t, <-errChan) + + ents, err := os.ReadDir(conflictDest) require.NoError(t, err) - require.True(t, bytes.Equal(actual, expected), "tar files differ") + require.Len(t, ents, 1, "conflict folder should have single conflict file") }) } diff --git a/cli/testdata/coder_templates_pull_--help.golden b/cli/testdata/coder_templates_pull_--help.golden index af214b70ed954..b3c6df36ca9f8 100644 --- a/cli/testdata/coder_templates_pull_--help.golden +++ b/cli/testdata/coder_templates_pull_--help.golden @@ -5,6 +5,7 @@ Usage: Flags: -h, --help help for pull + --tar output the template as a tar archive to stdout -y, --yes Bypass prompts Global Flags: diff --git a/docs/cli/coder_templates_pull.md b/docs/cli/coder_templates_pull.md index f43ad6a2f3aea..909695ec5a9c1 100644 --- a/docs/cli/coder_templates_pull.md +++ b/docs/cli/coder_templates_pull.md @@ -10,6 +10,7 @@ coder templates pull [destination] [flags] ``` -h, --help help for pull + --tar output the template as a tar archive to stdout -y, --yes Bypass prompts ``` diff --git a/go.mod b/go.mod index 2aa083602c8da..9e1782a267b55 100644 --- a/go.mod +++ b/go.mod @@ -71,6 +71,7 @@ require ( github.com/charmbracelet/glamour v0.6.0 github.com/charmbracelet/lipgloss v0.6.0 github.com/cli/safeexec v1.0.0 + github.com/codeclysm/extract v2.2.0+incompatible github.com/coder/retry v1.3.1-0.20230210155434-e90a2e1e091d github.com/coder/terraform-provider-coder v0.6.11 github.com/coreos/go-oidc/v3 v3.4.0 @@ -180,7 +181,9 @@ require ( github.com/dgraph-io/badger/v3 v3.2103.5 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/google/flatbuffers v23.1.21+incompatible // indirect + github.com/h2non/filetype v1.1.3 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/juju/errors v1.0.0 // indirect github.com/mattn/go-localereader v0.0.1 // indirect github.com/mattn/go-sqlite3 v1.14.15 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect diff --git a/go.sum b/go.sum index a6107fb7fc866..2b346c6506e78 100644 --- a/go.sum +++ b/go.sum @@ -363,6 +363,8 @@ github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:z github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h6jFvWxBdQXxjopDMZyH2UVceIRfR84bdzbkoKrsWNo= github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA= github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI= +github.com/codeclysm/extract v2.2.0+incompatible h1:q3wyckoA30bhUSiwdQezMqVhwd8+WGE64/GL//LtUhI= +github.com/codeclysm/extract v2.2.0+incompatible/go.mod h1:2nhFMPHiU9At61hz+12bfrlpXSUrOnK+wR+KlGO4Uks= github.com/coder/glog v1.0.1-0.20220322161911-7365fe7f2cd1 h1:UqBrPWSYvRI2s5RtOul20JukUEpu4ip9u7biBL+ntgk= github.com/coder/glog v1.0.1-0.20220322161911-7365fe7f2cd1/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4= github.com/coder/go-scim/pkg/v2 v2.0.0-20230221055123-1d63c1222136 h1:0RgB61LcNs24WOxc3PBvygSNTQurm0PYPujJjLLOzs0= @@ -993,6 +995,8 @@ github.com/grpc-ecosystem/grpc-gateway v1.12.1/go.mod h1:8XEsbTttt/W+VvjtQhLACqC github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 h1:BZHcxBETFHIdVyhyEfOvn/RdU/QGdLI4y34qQGjGWO0= github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks= +github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= +github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= github.com/hashicorp/consul/api v1.10.1/go.mod h1:XjsvQN+RJGWI2TWy1/kqaE16HrR2J/FWgkYjdZQsX9M= @@ -1197,6 +1201,8 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/juju/errors v1.0.0 h1:yiq7kjCLll1BiaRuNY53MGI0+EQ3rF6GB+wvboZDefM= +github.com/juju/errors v1.0.0/go.mod h1:B5x9thDqx0wIMH3+aLIMP9HjItInYWObRovoCFM5Qe8= github.com/juju/ratelimit v1.0.1/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= diff --git a/provisioner/echo/serve.go b/provisioner/echo/serve.go index 705fb0a3be2ae..774873f53d93c 100644 --- a/provisioner/echo/serve.go +++ b/provisioner/echo/serve.go @@ -207,6 +207,7 @@ func Tar(responses *Responses) ([]byte, error) { err = writer.WriteHeader(&tar.Header{ Name: fmt.Sprintf("%d.parse.protobuf", index), Size: int64(len(data)), + Mode: 0o644, }) if err != nil { return nil, err