Skip to content

Commit f7c10ad

Browse files
authored
feat(cli): extract tar in template pull (coder#6289)
1 parent 8231de9 commit f7c10ad

File tree

7 files changed

+127
-41
lines changed

7 files changed

+127
-41
lines changed

cli/templatepull.go

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package cli
22

33
import (
4+
"bytes"
45
"fmt"
5-
"io/fs"
66
"os"
77
"sort"
88

9+
"github.com/codeclysm/extract"
910
"github.com/spf13/cobra"
1011
"golang.org/x/xerrors"
1112

@@ -14,6 +15,7 @@ import (
1415
)
1516

1617
func templatePull() *cobra.Command {
18+
var tarMode bool
1719
cmd := &cobra.Command{
1820
Use: "pull <name> [destination]",
1921
Short: "Download the latest version of a template to a path.",
@@ -75,48 +77,44 @@ func templatePull() *cobra.Command {
7577
return xerrors.Errorf("unexpected Content-Type %q, expecting %q", ctype, codersdk.ContentTypeTar)
7678
}
7779

78-
// If the destination is empty then we write to stdout
79-
// and bail early.
80-
if dest == "" {
80+
if tarMode {
8181
_, err = cmd.OutOrStdout().Write(raw)
82-
if err != nil {
83-
return xerrors.Errorf("write stdout: %w", err)
84-
}
85-
return nil
82+
return err
8683
}
8784

88-
// Stat the destination to ensure nothing exists already.
89-
fi, err := os.Stat(dest)
90-
if err != nil && !xerrors.Is(err, fs.ErrNotExist) {
91-
return xerrors.Errorf("stat destination: %w", err)
85+
if dest == "" {
86+
dest = templateName + "/"
9287
}
9388

94-
if fi != nil && fi.IsDir() {
95-
// If the destination is a directory we just bail.
96-
return xerrors.Errorf("%q already exists.", dest)
89+
err = os.MkdirAll(dest, 0o750)
90+
if err != nil {
91+
return xerrors.Errorf("mkdirall %q: %w", dest, err)
9792
}
9893

99-
// If a file exists at the destination prompt the user
100-
// to ensure we don't overwrite something valuable.
101-
if fi != nil {
94+
ents, err := os.ReadDir(dest)
95+
if err != nil {
96+
return xerrors.Errorf("read dir %q: %w", dest, err)
97+
}
98+
99+
if len(ents) > 0 {
102100
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
103-
Text: fmt.Sprintf("%q already exists, do you want to overwrite it?", dest),
101+
Text: fmt.Sprintf("Directory %q is not empty, existing files may be overwritten.\nContinue extracting?", dest),
102+
Default: "No",
103+
Secret: false,
104104
IsConfirm: true,
105105
})
106106
if err != nil {
107-
return xerrors.Errorf("parse prompt: %w", err)
107+
return err
108108
}
109109
}
110110

111-
err = os.WriteFile(dest, raw, 0o600)
112-
if err != nil {
113-
return xerrors.Errorf("write to path: %w", err)
114-
}
115-
116-
return nil
111+
_, _ = fmt.Fprintf(cmd.OutOrStderr(), "Extracting template to %q\n", dest)
112+
err = extract.Tar(ctx, bytes.NewReader(raw), dest, nil)
113+
return err
117114
},
118115
}
119116

117+
cmd.Flags().BoolVar(&tarMode, "tar", false, "output the template as a tar archive to stdout")
120118
cliui.AllowSkipPrompt(cmd)
121119

122120
return cmd

cli/templatepull_test.go

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ package cli_test
22

33
import (
44
"bytes"
5+
"context"
6+
"io"
57
"os"
68
"path/filepath"
79
"testing"
810

11+
"github.com/codeclysm/extract"
912
"github.com/google/uuid"
13+
"github.com/ory/dockertest/v3/docker/pkg/archive"
1014
"github.com/stretchr/testify/require"
1115

1216
"github.com/coder/coder/cli/clitest"
@@ -53,7 +57,7 @@ func TestTemplatePull(t *testing.T) {
5357
// are being sorted correctly.
5458
_ = coderdtest.UpdateTemplateVersion(t, client, user.OrganizationID, source2, template.ID)
5559

56-
cmd, root := clitest.New(t, "templates", "pull", template.Name)
60+
cmd, root := clitest.New(t, "templates", "pull", "--tar", template.Name)
5761
clitest.SetupConfig(t, client, root)
5862

5963
var buf bytes.Buffer
@@ -65,9 +69,9 @@ func TestTemplatePull(t *testing.T) {
6569
require.True(t, bytes.Equal(expected, buf.Bytes()), "tar files differ")
6670
})
6771

68-
// ToFile tests that 'templates pull' pulls down the latest template
72+
// ToDir tests that 'templates pull' pulls down the latest template
6973
// and writes it to the correct directory.
70-
t.Run("ToFile", func(t *testing.T) {
74+
t.Run("ToDir", func(t *testing.T) {
7175
t.Parallel()
7276

7377
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
@@ -93,15 +97,14 @@ func TestTemplatePull(t *testing.T) {
9397

9498
dir := t.TempDir()
9599

96-
dest := filepath.Join(dir, "actual.tar")
100+
expectedDest := filepath.Join(dir, "expected")
101+
actualDest := filepath.Join(dir, "actual")
102+
ctx := context.Background()
97103

98-
// Create the file so that we can test that the command
99-
// warns the user before overwriting a preexisting file.
100-
fi, err := os.OpenFile(dest, os.O_CREATE|os.O_RDONLY, 0o600)
104+
err = extract.Tar(ctx, bytes.NewReader(expected), expectedDest, nil)
101105
require.NoError(t, err)
102-
_ = fi.Close()
103106

104-
cmd, root := clitest.New(t, "templates", "pull", template.Name, dest)
107+
cmd, root := clitest.New(t, "templates", "pull", template.Name, actualDest)
105108
clitest.SetupConfig(t, client, root)
106109

107110
pty := ptytest.New(t)
@@ -114,16 +117,89 @@ func TestTemplatePull(t *testing.T) {
114117
errChan <- cmd.Execute()
115118
}()
116119

117-
// We expect to be prompted that a file already exists.
118-
pty.ExpectMatch("already exists")
119-
pty.WriteLine("yes")
120-
121120
require.NoError(t, <-errChan)
122121

123-
actual, err := os.ReadFile(dest)
122+
expectedTarRd, err := archive.Tar(expectedDest, archive.Uncompressed)
123+
require.NoError(t, err)
124+
expectedTar, err := io.ReadAll(expectedTarRd)
125+
require.NoError(t, err)
126+
127+
actualTarRd, err := archive.Tar(actualDest, archive.Uncompressed)
128+
require.NoError(t, err)
129+
130+
actualTar, err := io.ReadAll(actualTarRd)
131+
require.NoError(t, err)
132+
133+
require.True(t, bytes.Equal(expectedTar, actualTar), "tar files differ")
134+
})
135+
136+
// FolderConflict tests that 'templates pull' fails when a folder with has
137+
// existing
138+
t.Run("FolderConflict", func(t *testing.T) {
139+
t.Parallel()
140+
141+
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
142+
user := coderdtest.CreateFirstUser(t, client)
143+
144+
// Create an initial template bundle.
145+
source1 := genTemplateVersionSource()
146+
// Create an updated template bundle. This will be used to ensure
147+
// that templates are correctly returned in order from latest to oldest.
148+
source2 := genTemplateVersionSource()
149+
150+
expected, err := echo.Tar(source2)
151+
require.NoError(t, err)
152+
153+
version1 := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, source1)
154+
_ = coderdtest.AwaitTemplateVersionJob(t, client, version1.ID)
155+
156+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version1.ID)
157+
158+
// Update the template version so that we can assert that templates
159+
// are being sorted correctly.
160+
_ = coderdtest.UpdateTemplateVersion(t, client, user.OrganizationID, source2, template.ID)
161+
162+
dir := t.TempDir()
163+
164+
expectedDest := filepath.Join(dir, "expected")
165+
conflictDest := filepath.Join(dir, "conflict")
166+
167+
err = os.MkdirAll(conflictDest, 0o700)
168+
require.NoError(t, err)
169+
170+
err = os.WriteFile(
171+
filepath.Join(conflictDest, "conflict-file"),
172+
[]byte("conflict"), 0o600,
173+
)
174+
require.NoError(t, err)
175+
176+
ctx := context.Background()
177+
178+
err = extract.Tar(ctx, bytes.NewReader(expected), expectedDest, nil)
179+
require.NoError(t, err)
180+
181+
cmd, root := clitest.New(t, "templates", "pull", template.Name, conflictDest)
182+
clitest.SetupConfig(t, client, root)
183+
184+
pty := ptytest.New(t)
185+
cmd.SetIn(pty.Input())
186+
cmd.SetOut(pty.Output())
187+
188+
errChan := make(chan error)
189+
go func() {
190+
defer close(errChan)
191+
errChan <- cmd.Execute()
192+
}()
193+
194+
pty.ExpectMatch("not empty")
195+
pty.WriteLine("no")
196+
197+
require.Error(t, <-errChan)
198+
199+
ents, err := os.ReadDir(conflictDest)
124200
require.NoError(t, err)
125201

126-
require.True(t, bytes.Equal(actual, expected), "tar files differ")
202+
require.Len(t, ents, 1, "conflict folder should have single conflict file")
127203
})
128204
}
129205

cli/testdata/coder_templates_pull_--help.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Usage:
55

66
Flags:
77
-h, --help help for pull
8+
--tar output the template as a tar archive to stdout
89
-y, --yes Bypass prompts
910

1011
Global Flags:

docs/cli/coder_templates_pull.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ coder templates pull <name> [destination] [flags]
1010

1111
```
1212
-h, --help help for pull
13+
--tar output the template as a tar archive to stdout
1314
-y, --yes Bypass prompts
1415
```
1516

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ require (
7171
github.com/charmbracelet/glamour v0.6.0
7272
github.com/charmbracelet/lipgloss v0.6.0
7373
github.com/cli/safeexec v1.0.0
74+
github.com/codeclysm/extract v2.2.0+incompatible
7475
github.com/coder/retry v1.3.1-0.20230210155434-e90a2e1e091d
7576
github.com/coder/terraform-provider-coder v0.6.11
7677
github.com/coreos/go-oidc/v3 v3.4.0
@@ -180,7 +181,9 @@ require (
180181
github.com/dgraph-io/badger/v3 v3.2103.5 // indirect
181182
github.com/dustin/go-humanize v1.0.1 // indirect
182183
github.com/google/flatbuffers v23.1.21+incompatible // indirect
184+
github.com/h2non/filetype v1.1.3 // indirect
183185
github.com/json-iterator/go v1.1.12 // indirect
186+
github.com/juju/errors v1.0.0 // indirect
184187
github.com/mattn/go-localereader v0.0.1 // indirect
185188
github.com/mattn/go-sqlite3 v1.14.15 // indirect
186189
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:z
363363
github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h6jFvWxBdQXxjopDMZyH2UVceIRfR84bdzbkoKrsWNo=
364364
github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA=
365365
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
366+
github.com/codeclysm/extract v2.2.0+incompatible h1:q3wyckoA30bhUSiwdQezMqVhwd8+WGE64/GL//LtUhI=
367+
github.com/codeclysm/extract v2.2.0+incompatible/go.mod h1:2nhFMPHiU9At61hz+12bfrlpXSUrOnK+wR+KlGO4Uks=
366368
github.com/coder/glog v1.0.1-0.20220322161911-7365fe7f2cd1 h1:UqBrPWSYvRI2s5RtOul20JukUEpu4ip9u7biBL+ntgk=
367369
github.com/coder/glog v1.0.1-0.20220322161911-7365fe7f2cd1/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
368370
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
993995
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
994996
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 h1:BZHcxBETFHIdVyhyEfOvn/RdU/QGdLI4y34qQGjGWO0=
995997
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=
998+
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
999+
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
9961000
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4=
9971001
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
9981002
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
11971201
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
11981202
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
11991203
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
1204+
github.com/juju/errors v1.0.0 h1:yiq7kjCLll1BiaRuNY53MGI0+EQ3rF6GB+wvboZDefM=
1205+
github.com/juju/errors v1.0.0/go.mod h1:B5x9thDqx0wIMH3+aLIMP9HjItInYWObRovoCFM5Qe8=
12001206
github.com/juju/ratelimit v1.0.1/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
12011207
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
12021208
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=

provisioner/echo/serve.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func Tar(responses *Responses) ([]byte, error) {
207207
err = writer.WriteHeader(&tar.Header{
208208
Name: fmt.Sprintf("%d.parse.protobuf", index),
209209
Size: int64(len(data)),
210+
Mode: 0o644,
210211
})
211212
if err != nil {
212213
return nil, err

0 commit comments

Comments
 (0)