diff --git a/cmd/cmd.go b/cmd/cmd.go index b1a0717..b4c19af 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -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) + } } } diff --git a/docs/yampl.md b/docs/yampl.md index 924da24..59eb924 100644 --- a/docs/yampl.md +++ b/docs/yampl.md @@ -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 diff --git a/internal/config/config.go b/internal/config/config.go index 411af17..381b513 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 diff --git a/internal/config/flags.go b/internal/config/flags.go index e587860..cb27851 100644 --- a/internal/config/flags.go +++ b/internal/config/flags.go @@ -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" @@ -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")