Skip to content

Commit

Permalink
feat(config): Add --no-source-comment flag
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Aug 2, 2024
1 parent 87644c4 commit 02964f4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
26 changes: 14 additions & 12 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,21 @@ func openAndTemplateFile(conf *config.Config, w io.Writer, dir, path string, wit
}
}
} else {
rel := path
if !withSrcComment {
if rel, err = filepath.Rel(dir, path); err == nil && rel != "." {
withSrcComment = true
}
}
if withSrcComment {
source := "# Source: " + rel + "\n"
if !strings.HasPrefix(s, "---") {
s = source + s
if !conf.NoSourceComment {
rel := path
if !withSrcComment {
if rel, err = filepath.Rel(dir, path); err == nil && rel != "." {
withSrcComment = true
}
}
if strings.Contains(s, "---") {
s = strings.ReplaceAll(s, "---\n", "---\n"+source)
if withSrcComment {
source := "# Source: " + rel + "\n"
if !strings.HasPrefix(s, "---") {
s = source + s
}
if strings.Contains(s, "---") {
s = strings.ReplaceAll(s, "---\n", "---\n"+source)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions docs/yampl.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ yampl [files | dirs] [-v key=value...] [flags]
--left-delim string Override template left delimiter (default "{{")
--log-format string Log format (auto, color, plain, json) (default "auto")
-l, --log-level string Log level (trace, debug, info, warn, error, fatal, panic) (default "info")
--no-source-comment Disables source path comment when run against multiple files or a dir
-p, --prefix string Template comments must begin with this prefix. The beginning '#' is implied. (default "#yampl")
--right-delim string Override template right delimiter (default "}}")
-s, --strip Strip template comments from output
Expand Down
13 changes: 7 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ type Config struct {
valuesStringToString *flag.StringToString
Vars Vars

Inplace bool
Prefix string
LeftDelim string
RightDelim string
Indent int
Strip bool
Inplace bool
Prefix string
LeftDelim string
RightDelim string
Indent int
Strip bool
NoSourceComment bool

IgnoreUnsetErrors bool
IgnoreTemplateErrors bool
Expand Down
14 changes: 8 additions & 6 deletions internal/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
const (
VarFlag = "var"

InplaceFlag = "inplace"
PrefixFlag = "prefix"
LeftDelimFlag = "left-delim"
RightDelimFlag = "right-delim"
IndentFlag = "indent"
StripFlag = "strip"
InplaceFlag = "inplace"
PrefixFlag = "prefix"
LeftDelimFlag = "left-delim"
RightDelimFlag = "right-delim"
IndentFlag = "indent"
StripFlag = "strip"
NoSourceCommentFlag = "no-source-comment"

IgnoreUnsetErrorsFlag = "ignore-unset-errors"
IgnoreTemplateErrorsFlag = "ignore-template-errors"
Expand All @@ -43,6 +44,7 @@ func (c *Config) RegisterFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&c.RightDelim, RightDelimFlag, c.RightDelim, "Override template right delimiter")
cmd.Flags().IntVarP(&c.Indent, IndentFlag, "I", c.Indent, "Override output indentation")
cmd.Flags().BoolVarP(&c.Strip, StripFlag, "s", c.Strip, "Strip template comments from output")
cmd.Flags().BoolVar(&c.NoSourceComment, NoSourceCommentFlag, c.NoSourceComment, "Disables source path comment when run against multiple files or a dir")

cmd.Flags().BoolVar(&c.IgnoreUnsetErrors, IgnoreUnsetErrorsFlag, c.IgnoreUnsetErrors, "Exit with an error if a template variable is not set")
cmd.Flags().BoolVar(&c.IgnoreTemplateErrors, IgnoreTemplateErrorsFlag, c.IgnoreTemplateErrors, "Continue processing a file even if a template fails")
Expand Down

0 comments on commit 02964f4

Please sign in to comment.