0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-22 08:20:39 +00:00

fix: add better logs on fatal errors

This commit is contained in:
Beatriz Vieira 2021-07-31 18:20:56 -03:00
parent 10ec6f18c4
commit 721e39feb1
2 changed files with 11 additions and 7 deletions

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -24,7 +23,7 @@ func loadEnvConfig() EnvConfig {
var c EnvConfig var c EnvConfig
err := envconfig.Process("", &c) err := envconfig.Process("", &c)
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal("failed to load env config, error: ", err.Error())
} }
return c return c
} }
@ -43,11 +42,16 @@ func getRepoPath() (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel") cmd := exec.Command("git", "rev-parse", "--show-toplevel")
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return "", errors.New(string(out)) return "", combinedOutputErr(err, out)
} }
return strings.TrimSpace(string(out)), nil return strings.TrimSpace(string(out)), nil
} }
func combinedOutputErr(err error, out []byte) error {
msg := strings.Split(string(out), "\n")
return fmt.Errorf("%v - %s", err, msg[0])
}
func loadConfig(filepath string) (Config, error) { func loadConfig(filepath string) (Config, error) {
content, rerr := ioutil.ReadFile(filepath) content, rerr := ioutil.ReadFile(filepath)
if rerr != nil { if rerr != nil {

View File

@ -27,19 +27,19 @@ func main() {
if envCfg.Home != "" { if envCfg.Home != "" {
if homeCfg, err := loadConfig(filepath.Join(envCfg.Home, configFilename)); err == nil { if homeCfg, err := loadConfig(filepath.Join(envCfg.Home, configFilename)); err == nil {
if merr := merge(&cfg, homeCfg); merr != nil { if merr := merge(&cfg, homeCfg); merr != nil {
log.Fatal(merr) log.Fatal("failed to merge user config, error: ", merr)
} }
} }
} }
repoPath, rerr := getRepoPath() repoPath, rerr := getRepoPath()
if rerr != nil { if rerr != nil {
log.Fatal(rerr) log.Fatal("failed to get repository path, error: ", rerr)
} }
if repoCfg, err := loadConfig(filepath.Join(repoPath, repoConfigFilename)); err == nil { if repoCfg, err := loadConfig(filepath.Join(repoPath, repoConfigFilename)); err == nil {
if merr := merge(&cfg, repoCfg); merr != nil { if merr := merge(&cfg, repoCfg); merr != nil {
log.Fatal(merr) log.Fatal("failed to merge repo config, error: ", merr)
} }
if len(repoCfg.ReleaseNotes.Headers) > 0 { // mergo is merging maps, headers will be overwritten if len(repoCfg.ReleaseNotes.Headers) > 0 { // mergo is merging maps, headers will be overwritten
cfg.ReleaseNotes.Headers = repoCfg.ReleaseNotes.Headers cfg.ReleaseNotes.Headers = repoCfg.ReleaseNotes.Headers
@ -165,6 +165,6 @@ func main() {
} }
if apperr := app.Run(os.Args); apperr != nil { if apperr := app.Run(os.Args); apperr != nil {
log.Fatal(apperr) log.Fatal("failed to run cli, error: ", apperr)
} }
} }