From 2527563975fc1086d08301dbdb50c7b8337f74cf Mon Sep 17 00:00:00 2001 From: Beatriz Vieira Date: Sat, 31 Jul 2021 19:05:22 -0300 Subject: [PATCH] refactor: isolate load config inside a function --- cmd/git-sv/config.go | 2 +- cmd/git-sv/main.go | 57 ++++++++++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/cmd/git-sv/config.go b/cmd/git-sv/config.go index a7ed36f..d95381b 100644 --- a/cmd/git-sv/config.go +++ b/cmd/git-sv/config.go @@ -52,7 +52,7 @@ func combinedOutputErr(err error, out []byte) error { return fmt.Errorf("%v - %s", err, msg[0]) } -func loadConfig(filepath string) (Config, error) { +func readConfig(filepath string) (Config, error) { content, rerr := ioutil.ReadFile(filepath) if rerr != nil { return Config{}, rerr diff --git a/cmd/git-sv/main.go b/cmd/git-sv/main.go index f446991..313d465 100644 --- a/cmd/git-sv/main.go +++ b/cmd/git-sv/main.go @@ -20,32 +20,7 @@ const ( func main() { log.SetFlags(0) - envCfg := loadEnvConfig() - - cfg := defaultConfig() - - if envCfg.Home != "" { - if homeCfg, err := loadConfig(filepath.Join(envCfg.Home, configFilename)); err == nil { - if merr := merge(&cfg, homeCfg); merr != nil { - log.Fatal("failed to merge user config, error: ", merr) - } - } - } - - repoPath, rerr := getRepoPath() - if rerr != nil { - log.Fatal("failed to get repository path, error: ", rerr) - } - - if repoCfg, err := loadConfig(filepath.Join(repoPath, repoConfigFilename)); err == nil { - if merr := merge(&cfg, repoCfg); merr != nil { - log.Fatal("failed to merge repo config, error: ", merr) - } - if len(repoCfg.ReleaseNotes.Headers) > 0 { // mergo is merging maps, headers will be overwritten - cfg.ReleaseNotes.Headers = repoCfg.ReleaseNotes.Headers - } - } - + cfg := loadCfg() messageProcessor := sv.NewMessageProcessor(cfg.CommitMessage, cfg.Branches) git := sv.NewGit(messageProcessor, cfg.Tag) semverProcessor := sv.NewSemVerCommitsProcessor(cfg.Versioning, cfg.CommitMessage) @@ -168,3 +143,33 @@ func main() { log.Fatal("failed to run cli, error: ", apperr) } } + +func loadCfg() Config { + envCfg := loadEnvConfig() + + cfg := defaultConfig() + + if envCfg.Home != "" { + if homeCfg, err := readConfig(filepath.Join(envCfg.Home, configFilename)); err == nil { + if merr := merge(&cfg, homeCfg); merr != nil { + log.Fatal("failed to merge user config, error: ", merr) + } + } + } + + repoPath, rerr := getRepoPath() + if rerr != nil { + log.Fatal("failed to get repository path, error: ", rerr) + } + + if repoCfg, err := readConfig(filepath.Join(repoPath, repoConfigFilename)); err == nil { + if merr := merge(&cfg, repoCfg); merr != nil { + log.Fatal("failed to merge repo config, error: ", merr) + } + if len(repoCfg.ReleaseNotes.Headers) > 0 { // mergo is merging maps, headers will be overwritten + cfg.ReleaseNotes.Headers = repoCfg.ReleaseNotes.Headers + } + } + + return cfg +}