Skip to content

Commit

Permalink
chore(config): Remove global var
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 26, 2024
1 parent 50321ce commit e477903
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 30 deletions.
7 changes: 2 additions & 5 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"slices"
)

//nolint:gochecknoglobals
var Default = NewDefault()

type Config struct {
Sort string `yaml:"sort"`
Abbrev int `yaml:"abbrev"`
Expand All @@ -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"
}
Expand Down
10 changes: 5 additions & 5 deletions internal/config/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 ""
}
Expand All @@ -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"
Expand Down
8 changes: 3 additions & 5 deletions internal/config/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
})
}
}
21 changes: 11 additions & 10 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
6 changes: 1 addition & 5 deletions internal/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e477903

Please sign in to comment.