diff --git a/cmd/cmd.go b/cmd/cmd.go index 6fb1540..4dfbe66 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -8,6 +8,7 @@ import ( "github.com/clevyr/go-yampl/internal/config" "github.com/clevyr/go-yampl/internal/node" "github.com/clevyr/go-yampl/internal/template" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "gopkg.in/yaml.v3" "io" @@ -93,6 +94,11 @@ func run(cmd *cobra.Command, args []string) error { } func openAndTemplate(conf config.Config, p string) (err error) { + defer func(logger *log.Entry) { + conf.Log = logger + }(conf.Log) + conf.Log = log.WithField("file", p) + var f *os.File if conf.Inplace { stat, err := os.Stat(p) diff --git a/internal/config/config.go b/internal/config/config.go index 2caeae0..36568e6 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -1,5 +1,7 @@ package config +import log "github.com/sirupsen/logrus" + type Config struct { Values Values Inplace bool @@ -8,6 +10,7 @@ type Config struct { RightDelim string Indent int Strict bool + Log *log.Entry } func New() Config { @@ -17,5 +20,6 @@ func New() Config { LeftDelim: "{{", RightDelim: "}}", Indent: 2, + Log: log.NewEntry(log.StandardLogger()), } } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 1ad0411..dcecd0a 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -1,6 +1,7 @@ package config import ( + log "github.com/sirupsen/logrus" "reflect" "testing" ) @@ -18,6 +19,7 @@ func TestNew(t *testing.T) { LeftDelim: "{{", RightDelim: "}}", Indent: 2, + Log: log.NewEntry(log.StandardLogger()), }, }, } diff --git a/internal/template/line_comment.go b/internal/template/line_comment.go index a1fb2a2..7076d6b 100644 --- a/internal/template/line_comment.go +++ b/internal/template/line_comment.go @@ -35,14 +35,14 @@ func LineComment(conf config.Config, node *yaml.Node) error { var buf strings.Builder if err = tmpl.Execute(&buf, conf.Values); err != nil { if !conf.Strict { - log.WithError(err).Warn("skipping value due to template error") + conf.Log.WithError(err).Warn("skipping value due to template error") return nil } return err } if buf.String() != node.Value { - log.WithFields(log.Fields{ + conf.Log.WithFields(log.Fields{ "tmpl": tmplSrc, "from": node.Value, "to": buf.String(), diff --git a/internal/template/line_comment_test.go b/internal/template/line_comment_test.go index 16815eb..6a15331 100644 --- a/internal/template/line_comment_test.go +++ b/internal/template/line_comment_test.go @@ -14,6 +14,14 @@ func TestTemplateLineComment(t *testing.T) { strictConf := config.New() strictConf.Strict = true + prefixConf := config.New() + prefixConf.Prefix = "#tmpl" + + delimConf := config.New() + delimConf.LeftDelim = "<{" + delimConf.RightDelim = "}>" + delimConf.Prefix = "#yampl" + type args struct { conf config.Config comment string @@ -27,8 +35,8 @@ func TestTemplateLineComment(t *testing.T) { {"no comment", args{defaultConf, ""}, "a", false}, {"simple comment", args{defaultConf, "#yampl b"}, "b #yampl b", false}, {"dynamic comment", args{defaultConf, "#yampl {{ .b }}"}, "b #yampl {{ .b }}", false}, - {"prefix", args{config.Config{Prefix: "#tmpl"}, "#tmpl b"}, "b #tmpl b", false}, - {"delimiters", args{config.Config{LeftDelim: "<{", RightDelim: "}>", Prefix: "#yampl"}, `#yampl <{ "b" }>`}, `b #yampl <{ "b" }>`, false}, + {"prefix", args{prefixConf, "#tmpl b"}, "b #tmpl b", false}, + {"delimiters", args{delimConf, `#yampl <{ "b" }>`}, `b #yampl <{ "b" }>`, false}, {"invalid template", args{defaultConf, "#yampl {{"}, "", true}, {"invalid variable ignore", args{defaultConf, "#yampl {{ .z }}"}, "a #yampl {{ .z }}", false}, {"invalid variable error", args{strictConf, "#yampl {{ .z }}"}, "", true},