Skip to content

Commit bcb2b38

Browse files
committed
Add support for project-specific configs
Signed-off-by: Knut Ahlers <knut@ahlers.me>
1 parent 3d1456d commit bcb2b38

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

config.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,55 @@ package main
33
import (
44
"errors"
55
"fmt"
6+
"io/fs"
67
"os"
78

89
"github.com/sirupsen/logrus"
910
yaml "gopkg.in/yaml.v2"
1011
)
1112

1213
type configFile struct {
13-
DiableTagSigning bool `yaml:"disable_signed_tags"`
14-
MatchMajor []string `yaml:"match_major"`
15-
MatchPatch []string `yaml:"match_patch"`
16-
ReleaseCommitMessage string `yaml:"release_commit_message"`
17-
IgnoreMessages []string `yaml:"ignore_messages"`
14+
DiableTagSigning bool `yaml:"disable_signed_tags"`
15+
16+
MatchMajor []string `yaml:"match_major"`
17+
MatchPatch []string `yaml:"match_patch"`
18+
19+
ReleaseCommitMessage string `yaml:"release_commit_message"`
20+
21+
IgnoreMessages []string `yaml:"ignore_messages"`
1822
}
1923

20-
func loadConfig() (*configFile, error) {
24+
func loadConfig(configFiles ...string) (*configFile, error) {
2125
var err error
2226

23-
if _, err = os.Stat(cfg.ConfigFile); err != nil {
24-
return nil, errors.New("config file does not exist, use --create-config to create one")
25-
}
26-
2727
c := &configFile{}
2828
if err = yaml.Unmarshal(mustAsset("assets/git_changerelease.yaml"), c); err != nil {
2929
return nil, fmt.Errorf("unmarshalling default config: %w", err)
3030
}
3131

32-
dataFile, err := os.Open(cfg.ConfigFile)
33-
if err != nil {
34-
return nil, fmt.Errorf("opening config file: %w", err)
35-
}
36-
defer func() {
37-
if err := dataFile.Close(); err != nil {
38-
logrus.WithError(err).Debug("closing config file (leaked fd)")
32+
for _, fn := range configFiles {
33+
if _, err = os.Stat(fn); err != nil {
34+
if errors.Is(err, fs.ErrNotExist) {
35+
logrus.WithField("path", fn).Debug("config-file does not exist, skipping")
36+
continue
37+
}
38+
return nil, fmt.Errorf("getting config-file stat for %q: %w", fn, err)
3939
}
40-
}()
4140

42-
if err = yaml.NewDecoder(dataFile).Decode(c); err != nil {
43-
return c, fmt.Errorf("decoding config file: %w", err)
41+
logrus.WithField("path", fn).Debug("loading config-file")
42+
43+
dataFile, err := os.Open(fn) //#nosec:G304 // This is intended to load variable files
44+
if err != nil {
45+
return nil, fmt.Errorf("opening config file: %w", err)
46+
}
47+
48+
if err = yaml.NewDecoder(dataFile).Decode(c); err != nil {
49+
return c, fmt.Errorf("decoding config file: %w", err)
50+
}
51+
52+
if err := dataFile.Close(); err != nil {
53+
logrus.WithError(err).WithField("path", fn).Debug("closing config file (leaked fd)")
54+
}
4455
}
4556

4657
return c, nil

main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,13 @@ func initApp() (err error) {
7979
return errors.New("tried to open the changelog in the editor but there is no $EDITOR in your env")
8080
}
8181

82-
if config, err = loadConfig(); err != nil {
83-
return fmt.Errorf("loading config file: %w", err)
82+
projectConfig, err := filenameInGitRoot(".git_changerelease.yaml")
83+
if err != nil {
84+
return fmt.Errorf("building filename for project config: %w", err)
85+
}
86+
87+
if config, err = loadConfig(cfg.ConfigFile, projectConfig); err != nil {
88+
return fmt.Errorf("loading config file(s): %w", err)
8489
}
8590

8691
// Collect matchers

0 commit comments

Comments
 (0)