From 1c516ad3db59d1c6a34a11266917f6ca80bb19a6 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Wed, 18 Nov 2020 23:08:49 -0600 Subject: [PATCH] More tests and env variables Signed-off-by: jolheiser --- DOCS.md | 82 ----------------------------------------- cmd/app.go | 17 ++------- cmd/download.go | 6 +-- docs.go | 4 ++ registry/source.go | 2 +- registry/source_test.go | 47 +++++++++++++++++++++++ 6 files changed, 58 insertions(+), 100 deletions(-) delete mode 100644 DOCS.md create mode 100644 registry/source_test.go diff --git a/DOCS.md b/DOCS.md deleted file mode 100644 index 7f9a148..0000000 --- a/DOCS.md +++ /dev/null @@ -1,82 +0,0 @@ -# NAME - -tmpl - Template automation - -# SYNOPSIS - -tmpl - -``` -[--debug|-d] -[--registry|-r]=[value] -[--source|-s]=[value] -``` - -**Usage**: - -``` -tmpl [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...] -``` - -# GLOBAL OPTIONS - -**--debug, -d**: Debug mode - -**--registry, -r**="": Registry directory of tmpl (default: /home/jolheiser/.tmpl) - -**--source, -s**="": Short-name source to use - - -# COMMANDS - -## download - -Download a template - -**--branch, -b**="": Branch to clone (default: main) - -## init - -Initialize a template - -## list - -List templates in the registry - -## remove - -Remove a template - -## save - -Save a local template - -## source - -Commands for working with sources - -### list - -List available sources - -### add - -Add a source - -### remove - -Remove a source - -## test - -Test if a directory is a valid template - -## update - -Update a template - -## use - -Use a template - -**--defaults**: Use template defaults diff --git a/cmd/app.go b/cmd/app.go index ba08026..2c414c1 100644 --- a/cmd/app.go +++ b/cmd/app.go @@ -31,27 +31,23 @@ func NewApp() *cli.App { app.Description = "Template automation" app.Version = Version app.Flags = []cli.Flag{ - &cli.BoolFlag{ - Name: "debug", - Aliases: []string{"d"}, - Usage: "Debug mode", - Destination: &flags.Debug, - }, &cli.StringFlag{ Name: "registry", Aliases: []string{"r"}, Usage: "Registry directory of tmpl", Value: defaultDir, + DefaultText: "~/.tmpl", Destination: &flags.Registry, + EnvVars: []string{"TMPL_REGISTRY"}, }, &cli.StringFlag{ Name: "source", Aliases: []string{"s"}, Usage: "Short-name source to use", Destination: &flags.Source, + EnvVars: []string{"TMPL_SOURCE"}, }, } - app.Before = before app.Commands = []*cli.Command{ Download, @@ -67,10 +63,3 @@ func NewApp() *cli.App { return app } - -func before(ctx *cli.Context) error { - if ctx.Bool("debug") { - beaver.Console.Level = beaver.DEBUG - } - return nil -} diff --git a/cmd/download.go b/cmd/download.go index c173c9e..f88fd7c 100644 --- a/cmd/download.go +++ b/cmd/download.go @@ -52,12 +52,12 @@ func runDownload(ctx *cli.Context) error { } cloneURL := ctx.Args().First() - if !strings.HasSuffix(cloneURL, ".git") { - cloneURL += ".git" - } if source != nil { cloneURL = source.CloneURL(cloneURL) } + if !strings.HasSuffix(cloneURL, ".git") { + cloneURL += ".git" + } t, err := reg.DownloadTemplate(ctx.Args().Get(1), cloneURL, ctx.String("branch")) if err != nil { diff --git a/docs.go b/docs.go index 3f5bd46..5f18aae 100644 --- a/docs.go +++ b/docs.go @@ -4,6 +4,7 @@ package main import ( "os" + "regexp" "strings" "go.jolheiser.com/tmpl/cmd" @@ -26,6 +27,9 @@ func main() { // CLI ToMarkdown issue related to man-pages md = md[strings.Index(md, "#"):] + // CLI is using real default instead of DefaultText + md = regexp.MustCompile(`[\/\w]+\.tmpl`).ReplaceAllString(md, "~/.tmpl") + if _, err := fi.WriteString(md); err != nil { panic(err) } diff --git a/registry/source.go b/registry/source.go index b42a792..7770538 100644 --- a/registry/source.go +++ b/registry/source.go @@ -11,5 +11,5 @@ type Source struct { // CloneURL constructs a URL suitable for cloning a repository func (s *Source) CloneURL(namespace string) string { - return fmt.Sprintf("%s%s", s.URL, namespace) + return fmt.Sprintf("%s%s.git", s.URL, namespace) } diff --git a/registry/source_test.go b/registry/source_test.go new file mode 100644 index 0000000..2613441 --- /dev/null +++ b/registry/source_test.go @@ -0,0 +1,47 @@ +package registry + +import ( + "strings" + "testing" +) + +func TestSource(t *testing.T) { + tt := []struct { + Name string + Source *Source + CloneURL string + }{ + { + Name: "Gitea", + Source: &Source{ + URL: "https://gitea.com/", + }, + CloneURL: "https://gitea.com/user/repo.git", + }, + { + Name: "GitHub", + Source: &Source{ + URL: "https://github.com/", + }, + CloneURL: "https://github.com/user/repo.git", + }, + { + Name: "GitLab", + Source: &Source{ + URL: "https://gitlab.com/", + }, + CloneURL: "https://gitlab.com/user/repo.git", + }, + } + + namespace := "user/repo" + for _, tc := range tt { + t.Run(tc.Name, func(t *testing.T) { + cloneURL := tc.Source.CloneURL(namespace) + if !strings.EqualFold(tc.CloneURL, cloneURL) { + t.Logf("incorrect clone URL:\n\tExpected: %s\n\tGot: %s\n", tc.CloneURL, cloneURL) + t.Fail() + } + }) + } +}