0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-21 12:00:40 +00:00

refactor: isolate load config inside a function

This commit is contained in:
Beatriz Vieira 2021-07-31 19:05:22 -03:00
parent 8597819cfa
commit 2527563975
2 changed files with 32 additions and 27 deletions

View File

@ -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

View File

@ -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
}