|
1 | 1 | package cli
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "archive/tar" |
| 5 | + "bytes" |
4 | 6 | "fmt"
|
5 |
| - "io/fs" |
| 7 | + "io" |
6 | 8 | "os"
|
7 |
| - "sort" |
| 9 | + "path/filepath" |
8 | 10 |
|
9 | 11 | "github.com/spf13/cobra"
|
10 | 12 | "golang.org/x/xerrors"
|
11 | 13 |
|
12 | 14 | "github.com/coder/coder/cli/cliui"
|
13 |
| - "github.com/coder/coder/codersdk" |
14 | 15 | )
|
15 | 16 |
|
16 |
| -func fetchTemplateArchiveBytes(cmd *cobra.Command, templateName string) ([]byte, error) { |
17 |
| - ctx := cmd.Context() |
18 |
| - client, err := createClient(cmd) |
19 |
| - if err != nil { |
20 |
| - return nil, xerrors.Errorf("create client: %w", err) |
21 |
| - } |
22 |
| - |
23 |
| - // TODO(JonA): Do we need to add a flag for organization? |
24 |
| - organization, err := currentOrganization(cmd, client) |
25 |
| - if err != nil { |
26 |
| - return nil, xerrors.Errorf("current organization: %w", err) |
27 |
| - } |
28 |
| - |
29 |
| - template, err := client.TemplateByName(ctx, organization.ID, templateName) |
30 |
| - if err != nil { |
31 |
| - return nil, xerrors.Errorf("template by name: %w", err) |
32 |
| - } |
33 |
| - |
34 |
| - // Pull the versions for the template. We'll find the latest |
35 |
| - // one and download the source. |
36 |
| - versions, err := client.TemplateVersionsByTemplate(ctx, codersdk.TemplateVersionsByTemplateRequest{ |
37 |
| - TemplateID: template.ID, |
38 |
| - }) |
39 |
| - if err != nil { |
40 |
| - return nil, xerrors.Errorf("template versions by template: %w", err) |
41 |
| - } |
42 |
| - |
43 |
| - if len(versions) == 0 { |
44 |
| - return nil, xerrors.Errorf("no template versions for template %q", templateName) |
45 |
| - } |
46 |
| - |
47 |
| - // Sort the slice from newest to oldest template. |
48 |
| - sort.SliceStable(versions, func(i, j int) bool { |
49 |
| - return versions[i].CreatedAt.After(versions[j].CreatedAt) |
50 |
| - }) |
51 |
| - |
52 |
| - latest := versions[0] |
| 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("pulling 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 | + } |
53 | 38 |
|
54 |
| - // Download the tar archive. |
55 |
| - raw, ctype, err := client.Download(ctx, latest.Job.StorageSource) |
56 |
| - if err != nil { |
57 |
| - return nil, xerrors.Errorf("download template: %w", err) |
58 |
| - } |
| 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 | + } |
59 | 46 |
|
60 |
| - if ctype != codersdk.ContentTypeTar { |
61 |
| - return nil, xerrors.Errorf("unexpected Content-Type %q, expecting %q", ctype, codersdk.ContentTypeTar) |
| 47 | + hdr, err = archiveReader.Next() |
62 | 48 | }
|
63 |
| - return raw, nil |
| 49 | + return nil |
64 | 50 | }
|
65 | 51 |
|
66 | 52 | func templatePull() *cobra.Command {
|
67 | 53 | cmd := &cobra.Command{
|
68 |
| - Use: "pull <name> [destination]", |
69 |
| - Short: "Download the latest version of a template to a path.", |
| 54 | + Use: "pull <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.", |
70 | 57 | Args: cobra.RangeArgs(1, 2),
|
71 | 58 | RunE: func(cmd *cobra.Command, args []string) error {
|
72 |
| - raw, err := fetchTemplateArchiveBytes(cmd, args[0]) |
73 |
| - if err != nil { |
74 |
| - return err |
75 |
| - } |
76 |
| - |
77 |
| - var dest string |
| 59 | + templateName := args[0] |
| 60 | + var destination string |
78 | 61 | if len(args) > 1 {
|
79 |
| - dest = args[1] |
| 62 | + destination = args[1] |
| 63 | + } else { |
| 64 | + destination = templateName |
80 | 65 | }
|
81 | 66 |
|
82 |
| - // If the destination is empty then we write to stdout |
83 |
| - // and bail early. |
84 |
| - if dest == "" { |
85 |
| - _, err = cmd.OutOrStdout().Write(raw) |
86 |
| - if err != nil { |
87 |
| - return xerrors.Errorf("write stdout: %w", err) |
88 |
| - } |
89 |
| - return nil |
| 67 | + raw, err := fetchTemplateArchiveBytes(cmd, templateName) |
| 68 | + if err != nil { |
| 69 | + return err |
90 | 70 | }
|
91 | 71 |
|
92 | 72 | // Stat the destination to ensure nothing exists already.
|
93 |
| - fi, err := os.Stat(dest) |
94 |
| - if err != nil && !xerrors.Is(err, fs.ErrNotExist) { |
95 |
| - return xerrors.Errorf("stat destination: %w", err) |
96 |
| - } |
97 |
| - |
98 |
| - if fi != nil && fi.IsDir() { |
99 |
| - // If the destination is a directory we just bail. |
100 |
| - return xerrors.Errorf("%q already exists.", dest) |
101 |
| - } |
102 |
| - |
103 |
| - // If a file exists at the destination prompt the user |
104 |
| - // to ensure we don't overwrite something valuable. |
105 |
| - if fi != nil { |
106 |
| - _, err = cliui.Prompt(cmd, cliui.PromptOptions{ |
107 |
| - Text: fmt.Sprintf("%q already exists, do you want to overwrite it?", dest), |
108 |
| - IsConfirm: true, |
109 |
| - }) |
110 |
| - if err != nil { |
111 |
| - return xerrors.Errorf("parse prompt: %w", err) |
112 |
| - } |
113 |
| - } |
114 |
| - |
115 |
| - err = os.WriteFile(dest, raw, 0600) |
116 |
| - if err != nil { |
117 |
| - return xerrors.Errorf("write to path: %w", err) |
| 73 | + stat, err := os.Stat(destination) |
| 74 | + if stat != nil { |
| 75 | + return xerrors.Errorf("template file/directory already exists: %s", destination) |
118 | 76 | }
|
119 | 77 |
|
120 |
| - return nil |
| 78 | + return TarBytesToTree(destination, raw) |
121 | 79 | },
|
122 | 80 | }
|
123 | 81 |
|
|
0 commit comments