Skip to content

Commit

Permalink
feat(config): Fallback to loading config from .goreleaser.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 12, 2024
1 parent f66f6cf commit f1f3067
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,5 @@ jobs:
## Configuration
Configuration is loaded from `.changelog-generator.yaml` in the git repo root. See the [config example](config_example.yaml) for more details.

If `.changelog-generator.yaml` is not found, Changelog Generator will attempt to load the `changelog` key from `.goreleaser.yaml`.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.1
require (
github.com/go-git/go-git/v5 v5.11.0
github.com/knadh/koanf/parsers/yaml v0.1.0
github.com/knadh/koanf/providers/confmap v0.1.0
github.com/knadh/koanf/providers/file v0.1.0
github.com/knadh/koanf/providers/structs v0.1.0
github.com/knadh/koanf/v2 v2.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NI
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY=
github.com/knadh/koanf/providers/confmap v0.1.0 h1:gOkxhHkemwG4LezxxN8DMOFopOPghxRVp7JbIvdvqzU=
github.com/knadh/koanf/providers/confmap v0.1.0/go.mod h1:2uLhxQzJnyHKfxG927awZC7+fyHFdQkd697K4MdLnIU=
github.com/knadh/koanf/providers/file v0.1.0 h1:fs6U7nrV58d3CFAFh8VTde8TM262ObYf3ODrc//Lp+c=
github.com/knadh/koanf/providers/file v0.1.0/go.mod h1:rjJ/nHQl64iYCtAW2QQnF0eSmDEX/YZ/eNFj5yR6BvA=
github.com/knadh/koanf/providers/structs v0.1.0 h1:wJRteCNn1qvLtE5h8KQBvLJovidSdntfdyIbbCzEyE0=
Expand Down
42 changes: 38 additions & 4 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ import (
"regexp"

"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/structs"
"github.com/knadh/koanf/v2"
"github.com/spf13/cobra"
)

type configFile struct {
path string
isGoReleaser bool
}

func Load(cmd *cobra.Command) (*Config, error) {
conf := NewDefault()
k := koanf.New(".")
Expand All @@ -24,18 +30,46 @@ func Load(cmd *cobra.Command) (*Config, error) {
}

// Find config file
var cfgFiles []configFile
cfgFile, err := cmd.Flags().GetString("config")
if err != nil {
return nil, err
}
if cfgFile == "" {
cfgFile = ".changelog-generator.yaml"
if cfgFile != "" {
cfgFiles = append(cfgFiles, configFile{path: cfgFile})
} else {
cfgFiles = []configFile{
{path: ".changelog-generator.yaml"},
{path: ".changelog-generator.yml"},
{path: ".goreleaser.yaml", isGoReleaser: true},
{path: ".goreleaser.yml", isGoReleaser: true},
}
}

// Parse config file
parser := yaml.Parser()
if err := k.Load(file.Provider(cfgFile), parser); err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
for _, cfgFile := range cfgFiles {
subK := k
if cfgFile.isGoReleaser {
subK = koanf.New(".")
}

if err := subK.Load(file.Provider(cfgFile.path), parser); err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
return nil, err
}

if cfgFile.isGoReleaser {
if changelogConf := subK.Get("changelog"); changelogConf != nil {
if err := k.Load(confmap.Provider(changelogConf.(map[string]any), "."), nil); err != nil {
return nil, err
}
}
}

break
}

if err := k.UnmarshalWithConf("", conf, koanf.UnmarshalConf{Tag: "yaml"}); err != nil {
Expand Down

0 comments on commit f1f3067

Please sign in to comment.