diff --git a/config.go b/config.go index 50f19d6..08b0d01 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "io/fs" "os" "github.com/sirupsen/logrus" @@ -10,37 +11,47 @@ import ( ) type configFile struct { - DiableTagSigning bool `yaml:"disable_signed_tags"` - MatchMajor []string `yaml:"match_major"` - MatchPatch []string `yaml:"match_patch"` - ReleaseCommitMessage string `yaml:"release_commit_message"` - IgnoreMessages []string `yaml:"ignore_messages"` + DiableTagSigning bool `yaml:"disable_signed_tags"` + + MatchMajor []string `yaml:"match_major"` + MatchPatch []string `yaml:"match_patch"` + + ReleaseCommitMessage string `yaml:"release_commit_message"` + + IgnoreMessages []string `yaml:"ignore_messages"` } -func loadConfig() (*configFile, error) { +func loadConfig(configFiles ...string) (*configFile, error) { var err error - if _, err = os.Stat(cfg.ConfigFile); err != nil { - return nil, errors.New("config file does not exist, use --create-config to create one") - } - c := &configFile{} if err = yaml.Unmarshal(mustAsset("assets/git_changerelease.yaml"), c); err != nil { return nil, fmt.Errorf("unmarshalling default config: %w", err) } - dataFile, err := os.Open(cfg.ConfigFile) - if err != nil { - return nil, fmt.Errorf("opening config file: %w", err) - } - defer func() { - if err := dataFile.Close(); err != nil { - logrus.WithError(err).Debug("closing config file (leaked fd)") + for _, fn := range configFiles { + if _, err = os.Stat(fn); err != nil { + if errors.Is(err, fs.ErrNotExist) { + logrus.WithField("path", fn).Debug("config-file does not exist, skipping") + continue + } + return nil, fmt.Errorf("getting config-file stat for %q: %w", fn, err) } - }() - if err = yaml.NewDecoder(dataFile).Decode(c); err != nil { - return c, fmt.Errorf("decoding config file: %w", err) + logrus.WithField("path", fn).Debug("loading config-file") + + dataFile, err := os.Open(fn) //#nosec:G304 // This is intended to load variable files + if err != nil { + return nil, fmt.Errorf("opening config file: %w", err) + } + + if err = yaml.NewDecoder(dataFile).Decode(c); err != nil { + return c, fmt.Errorf("decoding config file: %w", err) + } + + if err := dataFile.Close(); err != nil { + logrus.WithError(err).WithField("path", fn).Debug("closing config file (leaked fd)") + } } return c, nil diff --git a/main.go b/main.go index 90b4196..5e855c0 100644 --- a/main.go +++ b/main.go @@ -79,8 +79,13 @@ func initApp() (err error) { return errors.New("tried to open the changelog in the editor but there is no $EDITOR in your env") } - if config, err = loadConfig(); err != nil { - return fmt.Errorf("loading config file: %w", err) + projectConfig, err := filenameInGitRoot(".git_changerelease.yaml") + if err != nil { + return fmt.Errorf("building filename for project config: %w", err) + } + + if config, err = loadConfig(cfg.ConfigFile, projectConfig); err != nil { + return fmt.Errorf("loading config file(s): %w", err) } // Collect matchers