Skip to content

Commit

Permalink
✨ Add strip flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jun 1, 2022
1 parent 1691f8e commit 2753640
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {
Command.Flags().StringVar(&conf.RightDelim, "right-delim", conf.RightDelim, "Override the right delimiter")
Command.Flags().IntVarP(&conf.Indent, "indent", "I", conf.Indent, "Override output indentation")
Command.Flags().BoolVarP(&conf.Fail, "fail", "f", conf.Fail, `Trigger an error if a template variable is missing`)
Command.Flags().BoolVarP(&conf.Strip, "strip", "s", conf.Strip, "Strip comments in generated template")
}

func validArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
1 change: 1 addition & 0 deletions docs/yampl.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ yampl [-i] [-p prefix] [-v key=value ...] [file ...]
-l, --log-level string Log level (trace, debug, info, warning, error, fatal, panic) (default "info")
-p, --prefix string Line-comments are ignored unless this prefix is found. Prefix must begin with '#' (default "#yampl")
--right-delim string Override the right delimiter (default "}}")
-s, --strip Strip comments in generated template
-v, --value stringToString Define a template variable. Can be used more than once. (default [])
```

1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Config struct {
Indent int
Fail bool
Log *log.Entry
Strip bool
}

func New() Config {
Expand Down
6 changes: 6 additions & 0 deletions internal/visitor/template_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ func templateComment(conf config.Config, comment string, n ast.Node) (ast.Node,

logEntry := conf.Log.WithField("yamlpath", n.GetPath())

if conf.Strip {
if err := n.SetComment(nil); err != nil {
return nil, err
}
}

var buf bytes.Buffer
if err = tmpl.Execute(&buf, conf.Values); err != nil {
if !conf.Fail {
Expand Down
4 changes: 4 additions & 0 deletions internal/visitor/template_comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func TestTemplateComments_Visit(t *testing.T) {
delimConf.RightDelim = "}>"
delimConf.Prefix = "#yampl"

stripConf := config.New()
stripConf.Strip = true

type args struct {
conf config.Config
comment string
Expand All @@ -95,6 +98,7 @@ func TestTemplateComments_Visit(t *testing.T) {
{"sequence invalid variable ignore", args{defaultConf, "- a #yampl {{ .z }}"}, "- a #yampl {{ .z }}", false},
{"mapping invalid variable error", args{failConf, "test: a #yampl {{ .z }}"}, "", true},
{"sequence invalid variable error", args{failConf, "- a #yampl {{ .z }}"}, "", true},
{"strip", args{stripConf, "test: a #yampl b"}, "test: b", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 2753640

Please sign in to comment.