1
0
Fork 0
mirror of https://github.com/Luzifer/git-changerelease.git synced 2024-12-20 19:11:17 +00:00

Add support for project-specific configs

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-11-11 16:15:41 +01:00
parent 3d1456d438
commit bcb2b3806d
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
2 changed files with 38 additions and 22 deletions

View file

@ -3,6 +3,7 @@ package main
import ( import (
"errors" "errors"
"fmt" "fmt"
"io/fs"
"os" "os"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -10,37 +11,47 @@ import (
) )
type configFile struct { type configFile struct {
DiableTagSigning bool `yaml:"disable_signed_tags"` DiableTagSigning bool `yaml:"disable_signed_tags"`
MatchMajor []string `yaml:"match_major"`
MatchPatch []string `yaml:"match_patch"` MatchMajor []string `yaml:"match_major"`
ReleaseCommitMessage string `yaml:"release_commit_message"` MatchPatch []string `yaml:"match_patch"`
IgnoreMessages []string `yaml:"ignore_messages"`
ReleaseCommitMessage string `yaml:"release_commit_message"`
IgnoreMessages []string `yaml:"ignore_messages"`
} }
func loadConfig() (*configFile, error) { func loadConfig(configFiles ...string) (*configFile, error) {
var err 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{} c := &configFile{}
if err = yaml.Unmarshal(mustAsset("assets/git_changerelease.yaml"), c); err != nil { if err = yaml.Unmarshal(mustAsset("assets/git_changerelease.yaml"), c); err != nil {
return nil, fmt.Errorf("unmarshalling default config: %w", err) return nil, fmt.Errorf("unmarshalling default config: %w", err)
} }
dataFile, err := os.Open(cfg.ConfigFile) for _, fn := range configFiles {
if err != nil { if _, err = os.Stat(fn); err != nil {
return nil, fmt.Errorf("opening config file: %w", err) if errors.Is(err, fs.ErrNotExist) {
} logrus.WithField("path", fn).Debug("config-file does not exist, skipping")
defer func() { continue
if err := dataFile.Close(); err != nil { }
logrus.WithError(err).Debug("closing config file (leaked fd)") return nil, fmt.Errorf("getting config-file stat for %q: %w", fn, err)
} }
}()
if err = yaml.NewDecoder(dataFile).Decode(c); err != nil { logrus.WithField("path", fn).Debug("loading config-file")
return c, fmt.Errorf("decoding config file: %w", err)
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 return c, nil

View file

@ -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") 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 { projectConfig, err := filenameInGitRoot(".git_changerelease.yaml")
return fmt.Errorf("loading config file: %w", err) 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 // Collect matchers