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:
parent
3d1456d438
commit
bcb2b3806d
2 changed files with 38 additions and 22 deletions
51
config.go
51
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
|
||||
|
|
9
main.go
9
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
|
||||
|
|
Loading…
Reference in a new issue