Skip to content

Commit

Permalink
🔊 Add filepath to logs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed May 12, 2022
1 parent c397e64 commit e8ea2e5
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package config

import log "github.com/sirupsen/logrus"

type Config struct {
Values Values
Inplace bool
Expand All @@ -8,6 +10,7 @@ type Config struct {
RightDelim string
Indent int
Strict bool
Log *log.Entry
}

func New() Config {
Expand All @@ -17,5 +20,6 @@ func New() Config {
LeftDelim: "{{",
RightDelim: "}}",
Indent: 2,
Log: log.NewEntry(log.StandardLogger()),
}
}
2 changes: 2 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
log "github.com/sirupsen/logrus"
"reflect"
"testing"
)
Expand All @@ -18,6 +19,7 @@ func TestNew(t *testing.T) {
LeftDelim: "{{",
RightDelim: "}}",
Indent: 2,
Log: log.NewEntry(log.StandardLogger()),
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions internal/template/line_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
12 changes: 10 additions & 2 deletions internal/template/line_comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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},
Expand Down

0 comments on commit e8ea2e5

Please sign in to comment.