Skip to content

Commit f716c09

Browse files
committed
allow specifying an alternate destination
1 parent cf4fba5 commit f716c09

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

cli/templateexport.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import (
1414
"github.com/coder/coder/cli/cliui"
1515
)
1616

17-
func tarBytesToTree(templateName string, raw []byte) error {
18-
err := os.Mkdir(templateName, 0700)
17+
func tarBytesToTree(destination string, raw []byte) error {
18+
err := os.Mkdir(destination, 0700)
1919

2020
archiveReader := tar.NewReader(bytes.NewReader(raw))
2121
hdr, err := archiveReader.Next()
2222
for err != io.EOF {
2323
if hdr == nil { // some blog post indicated this could happen sometimes
2424
continue
2525
}
26-
filename := filepath.FromSlash(fmt.Sprintf("%s/%s", templateName, hdr.Name))
26+
filename := filepath.FromSlash(fmt.Sprintf("%s/%s", destination, hdr.Name))
2727
switch hdr.Typeflag {
2828
case tar.TypeDir:
2929
err = os.Mkdir(filename, 0700)
@@ -51,26 +51,31 @@ func tarBytesToTree(templateName string, raw []byte) error {
5151

5252
func templateExport() *cobra.Command {
5353
cmd := &cobra.Command{
54-
Use: "export <template name>",
55-
Short: "Create download the named template's contents and extract them into a subdirectory named <template name>.",
56-
Args: cobra.ExactArgs(1),
54+
Use: "export <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),
5758
RunE: func(cmd *cobra.Command, args []string) error {
58-
var (
59-
templateName = args[0]
60-
)
59+
templateName := args[0]
60+
var destination string
61+
if len(args) > 1 {
62+
destination = args[1]
63+
} else {
64+
destination = templateName
65+
}
6166

6267
raw, err := fetchTemplateArchiveBytes(cmd, templateName)
6368
if err != nil {
6469
return err
6570
}
6671

6772
// Stat the destination to ensure nothing exists already.
68-
stat, err := os.Stat(templateName)
73+
stat, err := os.Stat(destination)
6974
if stat != nil {
70-
return xerrors.Errorf("template file/directory already exists: %s", err)
75+
return xerrors.Errorf("template file/directory already exists: %s", destination)
7176
}
7277

73-
return tarBytesToTree(templateName, raw)
78+
return tarBytesToTree(destination, raw)
7479
},
7580
}
7681

0 commit comments

Comments
 (0)