1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
diff --git a/cmd/download.go b/cmd/download.go
index f5ce256caeb12f83e53c629657d0e801ea1c5d9d..93387334ed50ba403b0ba3ca134f9268476323f8 100644
--- a/cmd/download.go
+++ b/cmd/download.go
@@ -2,6 +2,8 @@ package cmd
import (
"fmt"
+ "os"
+ "path"
"strings"
"go.jolheiser.com/tmpl/env"
@@ -15,7 +17,7 @@ var Download = &cli.Command{
Name: "download",
Usage: "Download a template",
Description: "Download a template and save it to the local registry",
- ArgsUsage: "[repository URL] [name]",
+ ArgsUsage: "[repository URL] <name>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "branch",
@@ -29,7 +31,7 @@ Action: runDownload,
}
func runDownload(ctx *cli.Context) error {
- if ctx.NArg() < 2 {
+ if ctx.NArg() < 1 {
return cli.ShowCommandHelp(ctx, ctx.Command.Name)
}
@@ -67,7 +69,7 @@ if !strings.HasSuffix(cloneURL, ".git") {
cloneURL += ".git"
}
- t, err := reg.DownloadTemplate(ctx.Args().Get(1), cloneURL, ctx.String("branch"))
+ t, err := reg.DownloadTemplate(deriveName(ctx), cloneURL, ctx.String("branch"))
if err != nil {
return err
}
@@ -75,3 +77,19 @@
log.Info().Msgf("Added new template %q", t.Name)
return nil
}
+
+func deriveName(ctx *cli.Context) string {
+ if ctx.NArg() > 1 {
+ return ctx.Args().Get(1)
+ }
+
+ envBranch, envSet := os.LookupEnv("TMPL_BRANCH")
+ flagBranch, flagSet := ctx.String("branch"), ctx.IsSet("branch")
+ if flagSet {
+ if !envSet || envBranch != flagBranch {
+ return flagBranch
+ }
+ }
+
+ return path.Base(ctx.Args().First())
+}
|