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
33
config.go
33
config.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
@ -11,37 +12,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"`
|
MatchMajor []string `yaml:"match_major"`
|
||||||
MatchPatch []string `yaml:"match_patch"`
|
MatchPatch []string `yaml:"match_patch"`
|
||||||
|
|
||||||
ReleaseCommitMessage string `yaml:"release_commit_message"`
|
ReleaseCommitMessage string `yaml:"release_commit_message"`
|
||||||
|
|
||||||
IgnoreMessages []string `yaml:"ignore_messages"`
|
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 = 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("opening config file: %w", err)
|
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)")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if err = yaml.NewDecoder(dataFile).Decode(c); err != nil {
|
if err = yaml.NewDecoder(dataFile).Decode(c); err != nil {
|
||||||
return c, fmt.Errorf("decoding config file: %w", err)
|
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
|
||||||
}
|
}
|
||||||
|
|
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")
|
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
|
||||||
|
|
Loading…
Reference in a new issue