Skip to content

Commit 7d55db3

Browse files
committed
back out pull/export naming change
old "pull" is "pull" again; new command is "checkout"
1 parent a42b278 commit 7d55db3

File tree

7 files changed

+399
-399
lines changed

7 files changed

+399
-399
lines changed

cli/templatecheckout.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package cli
2+
3+
import (
4+
"archive/tar"
5+
"bytes"
6+
"fmt"
7+
"io"
8+
"os"
9+
"path/filepath"
10+
11+
"github.com/spf13/cobra"
12+
"golang.org/x/xerrors"
13+
14+
"github.com/coder/coder/cli/cliui"
15+
)
16+
17+
func TarBytesToTree(destination string, raw []byte) error {
18+
err := os.Mkdir(destination, 0700)
19+
20+
archiveReader := tar.NewReader(bytes.NewReader(raw))
21+
hdr, err := archiveReader.Next()
22+
for err != io.EOF {
23+
if hdr == nil { // some blog post indicated this could happen sometimes
24+
continue
25+
}
26+
filename := filepath.FromSlash(fmt.Sprintf("%s/%s", destination, hdr.Name))
27+
switch hdr.Typeflag {
28+
case tar.TypeDir:
29+
err = os.Mkdir(filename, 0700)
30+
if err != nil {
31+
return xerrors.Errorf("unable to check out template directory: %w", err)
32+
}
33+
case tar.TypeReg:
34+
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0600)
35+
if err != nil {
36+
return xerrors.Errorf("unable to create template file: %w", err)
37+
}
38+
39+
_, err = io.Copy(f, archiveReader)
40+
if err != nil {
41+
f.Close() // is this necessary?
42+
return xerrors.Errorf("error writing template file: %w", err)
43+
}
44+
f.Close()
45+
}
46+
47+
hdr, err = archiveReader.Next()
48+
}
49+
return nil
50+
}
51+
52+
func templateCheckout() *cobra.Command {
53+
cmd := &cobra.Command{
54+
Use: "checkout <template name> [destination]",
55+
Short: "Download the named template's contents into a subdirectory.",
56+
Long: "Download the named template's contents and extract them into a subdirectory named according to the destination or <template name> if no destination is specified.",
57+
Args: cobra.RangeArgs(1, 2),
58+
RunE: func(cmd *cobra.Command, args []string) error {
59+
templateName := args[0]
60+
var destination string
61+
if len(args) > 1 {
62+
destination = args[1]
63+
} else {
64+
destination = templateName
65+
}
66+
67+
raw, err := fetchTemplateArchiveBytes(cmd, templateName)
68+
if err != nil {
69+
return err
70+
}
71+
72+
// Stat the destination to ensure nothing exists already.
73+
stat, err := os.Stat(destination)
74+
if stat != nil {
75+
return xerrors.Errorf("template file/directory already exists: %s", destination)
76+
}
77+
78+
return TarBytesToTree(destination, raw)
79+
},
80+
}
81+
82+
cliui.AllowSkipPrompt(cmd)
83+
84+
return cmd
85+
}

cli/templatecheckout_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package cli_test
2+
3+
import (
4+
"archive/tar"
5+
"bufio"
6+
"bytes"
7+
"fmt"
8+
"io/ioutil"
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
13+
"github.com/coder/coder/cli"
14+
)
15+
16+
func writeTarArchiveFileEntry(tw *tar.Writer, filename string, content []byte) error {
17+
hdr := &tar.Header{
18+
Name: filename,
19+
Mode: 0600,
20+
Size: int64(len(content)),
21+
}
22+
23+
err := tw.WriteHeader(hdr)
24+
if err != nil {
25+
return err
26+
}
27+
_, err = tw.Write([]byte(content))
28+
if err != nil {
29+
return err
30+
}
31+
return nil
32+
}
33+
34+
func TestTemplateCheckoutExtractArchive(t *testing.T) {
35+
t.Parallel()
36+
37+
t.Run("TestTemplateCheckoutExtractArchive", func(t *testing.T) {
38+
subdirName := "subtle"
39+
expectedNames := []string{
40+
"rat-one", "rat-two", fmt.Sprintf("%s/trouble", subdirName),
41+
}
42+
expectedContents := []string{
43+
"{ 'tar' : 'but is it art?' }\n", "{ 'zap' : 'brannigan' }\n", "{ 'with' : 'a T' }\n",
44+
}
45+
46+
t.Parallel()
47+
48+
var bb bytes.Buffer
49+
w := bufio.NewWriter(&bb)
50+
tw := tar.NewWriter(w)
51+
52+
hdr := &tar.Header{
53+
Name: subdirName,
54+
Mode: 0700,
55+
Typeflag: tar.TypeDir,
56+
}
57+
err := tw.WriteHeader(hdr)
58+
if err != nil {
59+
t.Fatalf(err.Error())
60+
}
61+
62+
for i := 0; i < len(expectedNames); i++ {
63+
err = writeTarArchiveFileEntry(tw, expectedNames[i], []byte(expectedContents[i]))
64+
if err != nil {
65+
t.Fatalf(err.Error())
66+
}
67+
}
68+
69+
tw.Close()
70+
71+
dirname, err := ioutil.TempDir("", "template-checkout-test")
72+
if err != nil {
73+
t.Fatalf(err.Error())
74+
}
75+
76+
cli.TarBytesToTree(dirname, bb.Bytes())
77+
78+
for i := 0; i < len(expectedNames); i++ {
79+
filename := fmt.Sprintf("%s/%s", dirname, expectedNames[i])
80+
actualContents, err := ioutil.ReadFile(filename)
81+
82+
if err != nil {
83+
t.Fatalf(err.Error())
84+
}
85+
86+
require.Equal(t, expectedContents[i], string(actualContents))
87+
}
88+
})
89+
}

cli/templateexport.go

Lines changed: 0 additions & 127 deletions
This file was deleted.

0 commit comments

Comments
 (0)