From e8ea2e5ed25e055414ba0b87dea0f114a14055c2 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Thu, 12 May 2022 12:19:34 -0500 Subject: [PATCH] :loud_sound: Add filepath to logs --- cmd/cmd.go | 6 ++++++ internal/config/config.go | 4 ++++ internal/config/config_test.go | 2 ++ internal/template/line_comment.go | 4 ++-- internal/template/line_comment_test.go | 12 ++++++++++-- 5 files changed, 24 insertions(+), 4 deletions(-) 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},