diff --git a/internal/config/config.go b/internal/config/config.go index e1d4718..7598117 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,9 +4,6 @@ import ( "slices" ) -//nolint:gochecknoglobals -var Default = NewDefault() - type Config struct { Sort string `yaml:"sort"` Abbrev int `yaml:"abbrev"` @@ -31,8 +28,8 @@ func (c *Config) String() string { }) var hasPrinted bool for _, g := range c.Groups { - g.Sort() - if s := g.String(); s != "" { + g.Sort(c.Sort) + if s := g.String(c); s != "" { if hasPrinted && c.Divider != "" { result += c.Divider + "\n" } diff --git a/internal/config/group.go b/internal/config/group.go index 3cfdc24..1468411 100644 --- a/internal/config/group.go +++ b/internal/config/group.go @@ -37,8 +37,8 @@ func (g *Group) AddCommit(c *object.Commit) { g.Commits = append(g.Commits, c) } -func (g *Group) Sort() { - switch Default.Sort { +func (g *Group) Sort(sort string) { + switch sort { case SortAscending: slices.SortStableFunc(g.Commits, func(a, b *object.Commit) int { return strings.Compare(a.Message, b.Message) @@ -50,7 +50,7 @@ func (g *Group) Sort() { } } -func (g *Group) String() string { +func (g *Group) String(conf *Config) string { if len(g.Commits) == 0 { return "" } @@ -62,8 +62,8 @@ func (g *Group) String() string { for _, commit := range g.Commits { entry := "- " - if Default.Abbrev != HashDisabled { - entry += commit.Hash.String()[:Default.Abbrev] + " " + if conf.Abbrev != HashDisabled { + entry += commit.Hash.String()[:conf.Abbrev] + " " } entry += util.ShortMessage(commit) result += entry + "\n" diff --git a/internal/config/group_test.go b/internal/config/group_test.go index bf9b842..f0a13a6 100644 --- a/internal/config/group_test.go +++ b/internal/config/group_test.go @@ -68,10 +68,8 @@ func TestGroup_String(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - defer func(abbrev int) { - Default.Abbrev = abbrev - }(Default.Abbrev) - Default.Abbrev = tt.abbrev + c := NewDefault() + c.Abbrev = tt.abbrev g := &Group{ Title: tt.fields.Title, @@ -80,7 +78,7 @@ func TestGroup_String(t *testing.T) { re: tt.fields.re, Commits: tt.fields.Commits, } - assert.Equal(t, tt.want, g.String()) + assert.Equal(t, tt.want, g.String(c)) }) } } diff --git a/internal/config/load.go b/internal/config/load.go index 8bf1a56..70f29c4 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -22,9 +22,10 @@ type configFile struct { func Load(cmd *cobra.Command) (*Config, error) { k := koanf.New(".") + conf := NewDefault() // Load default config - if err := k.Load(structs.Provider(Default, "yaml"), nil); err != nil { + if err := k.Load(structs.Provider(conf, "yaml"), nil); err != nil { return nil, err } @@ -71,11 +72,11 @@ func Load(cmd *cobra.Command) (*Config, error) { break } - if err := k.UnmarshalWithConf("", Default, koanf.UnmarshalConf{Tag: "yaml"}); err != nil { + if err := k.UnmarshalWithConf("", conf, koanf.UnmarshalConf{Tag: "yaml"}); err != nil { return nil, err } - for _, g := range Default.Groups { + for _, g := range conf.Groups { if g.Regexp != "" { re, err := regexp.Compile(g.Regexp) if err != nil { @@ -84,24 +85,24 @@ func Load(cmd *cobra.Command) (*Config, error) { g.re = re } } - if len(Default.Groups) == 0 { - Default.Groups = append(Default.Groups, &Group{}) + if len(conf.Groups) == 0 { + conf.Groups = append(conf.Groups, &Group{}) } - for _, exclude := range Default.Filters.Exclude { + for _, exclude := range conf.Filters.Exclude { re, err := regexp.Compile(exclude) if err != nil { return nil, err } - Default.Filters.excludeRe = append(Default.Filters.excludeRe, re) + conf.Filters.excludeRe = append(conf.Filters.excludeRe, re) } - for _, exclude := range Default.Filters.Include { + for _, exclude := range conf.Filters.Include { re, err := regexp.Compile(exclude) if err != nil { return nil, err } - Default.Filters.includeRe = append(Default.Filters.includeRe, re) + conf.Filters.includeRe = append(conf.Filters.includeRe, re) } - return Default, err + return conf, err } diff --git a/internal/config/load_test.go b/internal/config/load_test.go index 5a26053..7c860a2 100644 --- a/internal/config/load_test.go +++ b/internal/config/load_test.go @@ -53,7 +53,7 @@ func TestLoad(t *testing.T) { conf, err := Load(cmd.Command) require.NoError(t, err) - assert.Equal(t, Default, conf) + assert.EqualValues(t, NewDefault(), conf) assert.Empty(t, conf.Filters.Include) assert.Empty(t, conf.Filters.Exclude) if assert.Len(t, conf.Groups, 1) { @@ -72,9 +72,6 @@ func TestLoad(t *testing.T) { } for _, tt := range cfgFileTests { t.Run("loads config at "+tt.path, func(t *testing.T) { - defer func() { - Default = NewDefault() - }() cmd := newStubCmd() defer cmd.close() @@ -106,7 +103,6 @@ groups: conf, err := Load(cmd.Command) require.NoError(t, err) - assert.Equal(t, Default, conf) assert.Empty(t, conf.Filters.Include) assert.Len(t, conf.Filters.Exclude, 2) assert.Len(t, conf.Groups, 3)