Skip to content

Commit

Permalink
Merge pull request #34 from dlorenc/templateflag
Browse files Browse the repository at this point in the history
Add godoc help generation for templateFlag.
  • Loading branch information
dlorenc committed Feb 6, 2018
2 parents 5d45557 + 72cb3d9 commit 7e7389b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cmd/skaffold/app/cmd/docker/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
output flags.TemplateFlag
)

var depsFormatFlag = flags.NewTemplateFlag("{{range .Deps}}{{.}} {{end}}\n")
var depsFormatFlag = flags.NewTemplateFlag("{{range .Deps}}{{.}} {{end}}\n", DepsOutput{})

func NewCmdDeps(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -47,7 +47,7 @@ func NewCmdDeps(out io.Writer) *cobra.Command {
}
cmd.Flags().StringVarP(&filename, "filename", "f", "Dockerfile", "Dockerfile path")
cmd.Flags().StringVarP(&context, "context", "c", ".", "Dockerfile context path")
cmd.Flags().VarP(depsFormatFlag, "output", "o", "Format output with go-template")
cmd.Flags().VarP(depsFormatFlag, "output", "o", depsFormatFlag.Usage())
return cmd
}

Expand Down
15 changes: 14 additions & 1 deletion cmd/skaffold/app/flags/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package flags

import (
"fmt"
"reflect"
"text/template"

"github.com/pkg/errors"
Expand All @@ -26,12 +27,23 @@ import (
type TemplateFlag struct {
rawTemplate string
template *template.Template
context interface{}
}

func (t *TemplateFlag) String() string {
return t.rawTemplate
}

func (t *TemplateFlag) Usage() string {
defaultUsage := "Format output with go-template."
if t.context != nil {
goType := reflect.TypeOf(t.context)
url := fmt.Sprintf("https://godoc.org/%s#%s", goType.PkgPath(), goType.Name())
defaultUsage += fmt.Sprintf(" For full struct documentation, see %s", url)
}
return defaultUsage
}

func (t *TemplateFlag) Set(value string) error {
tmpl, err := template.New("flagtemplate").Parse(value)
if err != nil {
Expand All @@ -50,9 +62,10 @@ func (t *TemplateFlag) Template() *template.Template {
return t.template
}

func NewTemplateFlag(value string) *TemplateFlag {
func NewTemplateFlag(value string, context interface{}) *TemplateFlag {
return &TemplateFlag{
template: template.Must(template.New("flagtemplate").Parse(value)),
rawTemplate: value,
context: context,
}
}
4 changes: 2 additions & 2 deletions cmd/skaffold/app/flags/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestNewTemplateFlag(t *testing.T) {
actual := &bytes.Buffer{}
expected := &bytes.Buffer{}

flag := NewTemplateFlag(rawTemplate)
flag := NewTemplateFlag(rawTemplate, nil)
if err := flag.Template().Execute(actual, &data); err != nil {
t.Errorf("Error parsing template from flag: %s", err)
}
Expand All @@ -61,7 +61,7 @@ func TestTemplateSet(t *testing.T) {
}

func TestTemplateString(t *testing.T) {
flag := NewTemplateFlag(rawTemplate)
flag := NewTemplateFlag(rawTemplate, nil)
if rawTemplate != flag.String() {
t.Errorf("Flag String() does not match. Expected %s, Actual %s", rawTemplate, flag.String())
}
Expand Down

0 comments on commit 7e7389b

Please sign in to comment.