More tests and env variables

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/7/head
jolheiser 2020-11-18 23:08:49 -06:00
parent dc19c1b993
commit 1c516ad3db
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
6 changed files with 58 additions and 100 deletions

82
DOCS.md
View File

@ -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

View File

@ -31,27 +31,23 @@ func NewApp() *cli.App {
app.Description = "Template automation" app.Description = "Template automation"
app.Version = Version app.Version = Version
app.Flags = []cli.Flag{ app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "Debug mode",
Destination: &flags.Debug,
},
&cli.StringFlag{ &cli.StringFlag{
Name: "registry", Name: "registry",
Aliases: []string{"r"}, Aliases: []string{"r"},
Usage: "Registry directory of tmpl", Usage: "Registry directory of tmpl",
Value: defaultDir, Value: defaultDir,
DefaultText: "~/.tmpl",
Destination: &flags.Registry, Destination: &flags.Registry,
EnvVars: []string{"TMPL_REGISTRY"},
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "source", Name: "source",
Aliases: []string{"s"}, Aliases: []string{"s"},
Usage: "Short-name source to use", Usage: "Short-name source to use",
Destination: &flags.Source, Destination: &flags.Source,
EnvVars: []string{"TMPL_SOURCE"},
}, },
} }
app.Before = before
app.Commands = []*cli.Command{ app.Commands = []*cli.Command{
Download, Download,
@ -67,10 +63,3 @@ func NewApp() *cli.App {
return app return app
} }
func before(ctx *cli.Context) error {
if ctx.Bool("debug") {
beaver.Console.Level = beaver.DEBUG
}
return nil
}

View File

@ -52,12 +52,12 @@ func runDownload(ctx *cli.Context) error {
} }
cloneURL := ctx.Args().First() cloneURL := ctx.Args().First()
if !strings.HasSuffix(cloneURL, ".git") {
cloneURL += ".git"
}
if source != nil { if source != nil {
cloneURL = source.CloneURL(cloneURL) cloneURL = source.CloneURL(cloneURL)
} }
if !strings.HasSuffix(cloneURL, ".git") {
cloneURL += ".git"
}
t, err := reg.DownloadTemplate(ctx.Args().Get(1), cloneURL, ctx.String("branch")) t, err := reg.DownloadTemplate(ctx.Args().Get(1), cloneURL, ctx.String("branch"))
if err != nil { if err != nil {

View File

@ -4,6 +4,7 @@ package main
import ( import (
"os" "os"
"regexp"
"strings" "strings"
"go.jolheiser.com/tmpl/cmd" "go.jolheiser.com/tmpl/cmd"
@ -26,6 +27,9 @@ func main() {
// CLI ToMarkdown issue related to man-pages // CLI ToMarkdown issue related to man-pages
md = md[strings.Index(md, "#"):] 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 { if _, err := fi.WriteString(md); err != nil {
panic(err) panic(err)
} }

View File

@ -11,5 +11,5 @@ type Source struct {
// CloneURL constructs a URL suitable for cloning a repository // CloneURL constructs a URL suitable for cloning a repository
func (s *Source) CloneURL(namespace string) string { func (s *Source) CloneURL(namespace string) string {
return fmt.Sprintf("%s%s", s.URL, namespace) return fmt.Sprintf("%s%s.git", s.URL, namespace)
} }

View File

@ -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()
}
})
}
}