Skip to content

Commit

Permalink
fix(config): Fix GoReleaser files not being detected if path is hardc…
Browse files Browse the repository at this point in the history
…oded
  • Loading branch information
gabe565 committed Mar 26, 2024
1 parent e477903 commit a40950a
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package config
import (
"errors"
"os"
"path/filepath"
"regexp"

"github.com/knadh/koanf/parsers/yaml"
Expand All @@ -15,11 +16,6 @@ import (
"github.com/spf13/cobra"
)

type configFile struct {
path string
isGoReleaser bool
}

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

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

// Parse config file
parser := yaml.Parser()
for _, cfgFile := range cfgFiles {
subK := k
if cfgFile.isGoReleaser {
if isGoReleaser(cfgFile) {
subK = koanf.New(".")
}

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

if cfgFile.isGoReleaser {
if isGoReleaser(cfgFile) {
if changelogConf := subK.Get("changelog"); changelogConf != nil {
if err := k.Load(confmap.Provider(changelogConf.(map[string]any), "."), nil); err != nil {
return nil, err
Expand Down Expand Up @@ -106,3 +102,8 @@ func Load(cmd *cobra.Command) (*Config, error) {

return conf, err
}

func isGoReleaser(path string) bool {
base := filepath.Base(path)
return base == ".goreleaser.yaml" || base == ".goreleaser.yml"
}

0 comments on commit a40950a

Please sign in to comment.