diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2018-08-28 13:04:13 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-08-28 13:04:13 -0400 |
| commit | e418e7e57cfff30aef67d2cb08b58675204d5e91 (patch) | |
| tree | a74acb61aa5fe155af0fe03dc9710d66f8dd8de0 | |
| parent | c0201760ff6cd9acd992eb7e96f447c401fd3178 (diff) | |
| parent | 92aabdb1e7796a136675ab110ae81a2cdab02179 (diff) | |
Merge pull request #385 from hairyhenderson/verbose-mode
Adding new --verbose flag
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | cmd/gomplate/main.go | 13 | ||||
| -rw-r--r-- | gomplate.go | 43 | ||||
| -rw-r--r-- | gomplate_test.go | 22 | ||||
| -rw-r--r-- | version/version.go | 2 |
5 files changed, 81 insertions, 2 deletions
@@ -16,6 +16,7 @@ BUILD_DATE ?= `date -u +"%Y-%m-%dT%H:%M:%SZ"` COMMIT_FLAG := -X `go list ./version`.GitCommit=$(COMMIT) VERSION_FLAG := -X `go list ./version`.Version=$(VERSION) +BUILD_DATE_FLAG := -X `go list ./version`.BuildDate=$(BUILD_DATE) GOOS ?= $(shell go version | sed 's/^.*\ \([a-z0-9]*\)\/\([a-z0-9]*\)/\1/') GOARCH ?= $(shell go version | sed 's/^.*\ \([a-z0-9]*\)\/\([a-z0-9]*\)/\2/') @@ -70,7 +71,7 @@ docker-images: gomplate.iid gomplate-slim.iid $(PREFIX)/bin/$(PKG_NAME)_%: $(shell find $(PREFIX) -type f -name '*.go') GOOS=$(shell echo $* | cut -f1 -d-) GOARCH=$(shell echo $* | cut -f2 -d- | cut -f1 -d.) CGO_ENABLED=0 \ $(GO) build \ - -ldflags "-w -s $(COMMIT_FLAG) $(VERSION_FLAG)" \ + -ldflags "-w -s $(COMMIT_FLAG) $(VERSION_FLAG) $(BUILD_DATE_FLAG)" \ -o $@ \ ./cmd/gomplate diff --git a/cmd/gomplate/main.go b/cmd/gomplate/main.go index 5aa0727c..db12d4fc 100644 --- a/cmd/gomplate/main.go +++ b/cmd/gomplate/main.go @@ -14,6 +14,7 @@ import ( var ( printVer bool + verbose bool opts gomplate.Config ) @@ -42,7 +43,6 @@ func validateOpts(cmd *cobra.Command, args []string) error { } func printVersion(name string) { - // fmt.Printf("%s version %s, build %s\n", name, version.Version, version.GitCommit) fmt.Printf("%s version %s\n", name, version.Version) } @@ -80,10 +80,19 @@ func newGomplateCmd() *cobra.Command { printVersion(cmd.Name()) return nil } + if verbose { + fmt.Fprintf(os.Stderr, "%s version %s, build %s (%v)\nconfig is:\n%s\n\n", + cmd.Name(), version.Version, version.GitCommit, version.BuildDate, + &opts) + } err := gomplate.RunTemplates(&opts) cmd.SilenceErrors = true cmd.SilenceUsage = true + if verbose { + fmt.Fprintf(os.Stderr, "rendered %d template(s) with %d error(s) in %v\n", + gomplate.Metrics.TemplatesProcessed, gomplate.Metrics.Errors, gomplate.Metrics.TotalRenderDuration) + } return err }, PostRunE: postRunExec, @@ -113,6 +122,8 @@ func initFlags(command *cobra.Command) { command.Flags().StringVar(&opts.LDelim, "left-delim", ldDefault, "override the default left-`delimiter` [$GOMPLATE_LEFT_DELIM]") command.Flags().StringVar(&opts.RDelim, "right-delim", rdDefault, "override the default right-`delimiter` [$GOMPLATE_RIGHT_DELIM]") + command.Flags().BoolVarP(&verbose, "verbose", "V", false, "output extra information about what gomplate is doing") + command.Flags().BoolVarP(&printVer, "version", "v", false, "print the version") } diff --git a/gomplate.go b/gomplate.go index bca66f75..184af240 100644 --- a/gomplate.go +++ b/gomplate.go @@ -4,6 +4,7 @@ import ( "io" "os" "strconv" + "strings" "text/template" "time" @@ -39,6 +40,48 @@ func (o *Config) getMode() (os.FileMode, bool, error) { return mode, modeOverride, nil } +// nolint: gocyclo +func (o *Config) String() string { + c := "input: " + if o.Input != "" { + c += "<arg>" + } else if o.InputDir != "" { + c += o.InputDir + } else if len(o.InputFiles) > 0 { + c += strings.Join(o.InputFiles, ", ") + } + + if len(o.ExcludeGlob) > 0 { + c += "\nexclude: " + strings.Join(o.ExcludeGlob, ", ") + } + + c += "\noutput: " + if o.InputDir != "" && o.OutputDir != "." { + c += o.OutputDir + } else if len(o.OutputFiles) > 0 { + c += strings.Join(o.OutputFiles, ", ") + } + + if o.OutMode != "" { + c += "\nchmod: " + o.OutMode + } + + if len(o.DataSources) > 0 { + c += "\ndatasources: " + strings.Join(o.DataSources, ", ") + } + if len(o.DataSourceHeaders) > 0 { + c += "\ndatasourceheaders: " + strings.Join(o.DataSourceHeaders, ", ") + } + + if o.LDelim != "{{" { + c += "\nleft_delim: " + o.LDelim + } + if o.RDelim != "}}" { + c += "\nright_delim: " + o.RDelim + } + return c +} + // gomplate - type gomplate struct { funcMap template.FuncMap diff --git a/gomplate_test.go b/gomplate_test.go index 5dc81ad9..9d2d1223 100644 --- a/gomplate_test.go +++ b/gomplate_test.go @@ -171,3 +171,25 @@ func TestRunTemplates(t *testing.T) { assert.Equal(t, 1, Metrics.TemplatesProcessed) assert.Equal(t, 0, Metrics.Errors) } + +func TestConfigString(t *testing.T) { + c := &Config{} + + expected := `input: +output: +left_delim: +right_delim: ` + + assert.Equal(t, expected, c.String()) + + c = &Config{ + LDelim: "{{", + RDelim: "}}", + Input: "{{ foo }}", + OutputFiles: []string{"-"}, + } + expected = `input: <arg> +output: -` + + assert.Equal(t, expected, c.String()) +} diff --git a/version/version.go b/version/version.go index cf5075c2..4b391036 100644 --- a/version/version.go +++ b/version/version.go @@ -5,4 +5,6 @@ var ( Version = "0.0.0" // GitCommit will be overwritten automatically by the build GitCommit = "HEAD" + // BuildDate will be overwritten automatically by the build + BuildDate = "" ) |
