Skip to content

Commit

Permalink
fix: also support .yaml config (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
xoxys authored Oct 20, 2024
1 parent 4732ce1 commit b605085
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 30 deletions.
2 changes: 0 additions & 2 deletions .gitsv/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
version: "1.1"

versioning:
update-major: []
update-minor: [feat]
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ make build
The configuration is loaded from a YAML file in the following order (last wins):

- built-in default
- `.gitsv/config.yml` in repository root
- `.gitsv/config.yaml` or `.gitsv/config.yml` in repository root (first found)

To check the default configuration, run:

Expand All @@ -42,8 +42,6 @@ git sv cfg default
```

```Yaml
version: "1.1" # Configuration version.

versioning:
update-major: [] # Commit types used to bump major.
update-minor: [feat] # Commit types used to bump minor.
Expand Down
8 changes: 4 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (
const (
logSeparator = "###"
endLine = "~~~"

configFilename = "config.yml"
configDir = ".gitsv"
)

var errUnknownGitError = errors.New("git command failed")
Expand Down Expand Up @@ -68,9 +65,12 @@ type GitSV struct {

// New constructor.
func New() GitSV {
configDir := ".gitsv"
configFilenames := []string{"config.yaml", "config.yml"}

g := GitSV{
Settings: &Settings{},
Config: NewConfig(configDir, configFilename),
Config: NewConfig(configDir, configFilenames),
}

g.MessageProcessor = sv.NewMessageProcessor(g.Config.CommitMessage, g.Config.Branches)
Expand Down
21 changes: 9 additions & 12 deletions app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ type TagSettings struct {

// Config cli yaml config.
type Config struct {
Version string `yaml:"version"`
LogLevel string `yaml:"log-level"`
Versioning sv.VersioningConfig `yaml:"versioning"`
Tag TagConfig `yaml:"tag"`
Expand All @@ -71,14 +70,18 @@ type TagConfig struct {
Filter *string `yaml:"filter"`
}

func NewConfig(configDir, configFilename string) *Config {
func NewConfig(configDir string, configFilenames []string) *Config {
workDir, _ := os.Getwd()
cfg := GetDefault()

repoCfgFilepath := filepath.Join(workDir, configDir, configFilename)
if repoCfg, err := readFile(repoCfgFilepath); err == nil {
if merr := merge(cfg, migrate(repoCfg, repoCfgFilepath)); merr != nil {
log.Fatal().Err(merr).Msg("failed to merge repo config")
for _, filename := range configFilenames {
repoCfgFilepath := filepath.Join(workDir, configDir, filename)
if repoCfg, err := readFile(repoCfgFilepath); err == nil {
if merr := merge(cfg, repoCfg); merr != nil {
log.Fatal().Err(merr).Msg("failed to merge repo config")
}

break
}
}

Expand Down Expand Up @@ -107,7 +110,6 @@ func GetDefault() *Config {
filter := ""

return &Config{
Version: "1.1",
Versioning: sv.VersioningConfig{
UpdateMajor: []string{},
UpdateMinor: []string{"feat"},
Expand Down Expand Up @@ -173,8 +175,3 @@ func (t *mergeTransformer) Transformer(typ reflect.Type) func(dst, src reflect.V

return nil
}

//nolint:revive
func migrate(cfg Config, filename string) Config {
return cfg
}
18 changes: 9 additions & 9 deletions app/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ func Test_merge(t *testing.T) {
}{
{
"overwrite string",
Config{Version: "a"},
Config{Version: "b"},
Config{Version: "b"},
Config{LogLevel: "info"},
Config{LogLevel: "warn"},
Config{LogLevel: "warn"},
false,
},
{
"default string",
Config{Version: "a"},
Config{Version: ""},
Config{Version: "a"},
Config{LogLevel: "info"},
Config{LogLevel: ""},
Config{LogLevel: "info"},
false,
},
{
Expand Down Expand Up @@ -120,21 +120,21 @@ func Test_merge(t *testing.T) {
{
"overwrite tag config",
Config{
Version: "a",
LogLevel: "info",
Tag: TagConfig{
Pattern: &nonEmptyStr,
Filter: &nonEmptyStr,
},
},
Config{
Version: "",
LogLevel: "",
Tag: TagConfig{
Pattern: &emptyStr,
Filter: &emptyStr,
},
},
Config{
Version: "a",
LogLevel: "info",
Tag: TagConfig{
Pattern: &emptyStr,
Filter: &emptyStr,
Expand Down

0 comments on commit b605085

Please sign in to comment.