From 512bf7394da33af02d9e9655660f2a5fd9381d9f Mon Sep 17 00:00:00 2001 From: jolheiser Date: Thu, 17 Aug 2023 04:02:19 +0000 Subject: [PATCH] Derive branch name (#27) Reviewed-on: https://git.jojodev.com/jolheiser/tmpl/pulls/27 Co-authored-by: jolheiser Co-committed-by: jolheiser --- FAQ.md | 2 +- cmd/download.go | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/FAQ.md b/FAQ.md index 6ec6f61..3b6bcfa 100644 --- a/FAQ.md +++ b/FAQ.md @@ -113,7 +113,7 @@ I realize that many users will be using GitHub, and most will likely still be us ## Backup and Restore -1. The simplest solution is to make a copy of your `registry.toml` (default: `~/.tmpl/registry.toml`). +1. The simplest solution is to make a copy of your `registry.yaml` (default: `~/.tmpl/registry.yaml`). * Once in the new location, you will need to use `tmpl restore`. 2. Alternatively, you can copy/paste the entire registry (default: `~/.tmpl`) and skip the restore step. diff --git a/cmd/download.go b/cmd/download.go index f5ce256..9338733 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] ", Flags: []cli.Flag{ &cli.StringFlag{ Name: "branch", @@ -29,7 +31,7 @@ var Download = &cli.Command{ } 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 @@ func runDownload(ctx *cli.Context) error { 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 @@ func runDownload(ctx *cli.Context) error { 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()) +}