From 5dba43f923724a776fb25d8382febe204cb3459d Mon Sep 17 00:00:00 2001 From: Peter Winckles Date: Wed, 9 Jun 2021 13:02:06 -0500 Subject: [PATCH] do not init git config on help and version cmds --- cmd/root.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index b0746d85..21077908 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -25,6 +25,8 @@ const ( configExtensionPattern string = ".*" defaultFilePermission os.FileMode = 0755 defaultDirPermission os.FileMode = 0666 + gitInitMessage string = `This command must be executed within git repository. +Change working directory or initialize new repository with 'git init'.` ) var ( @@ -49,14 +51,13 @@ lefthook install`, return } + initGitConfig() + if gitInitialized, _ := afero.Exists(appFs, filepath.Join(getRootPath(), ".git")); gitInitialized { return } - message := `This command must be executed within git repository. -Change working directory or initialize new repository with 'git init'.` - - log.Fatal(au.Brown(message)) + log.Fatal(au.Brown(gitInitMessage)) }, } @@ -90,9 +91,6 @@ func initAurora() { func initConfig() { log.SetFlags(0) - setRootPath() - setGitHooksPath(getHooksPathFromGitConfig()) - // store original config before merge originConfig = viper.New() originConfig.SetConfigName(configFileName) @@ -123,6 +121,11 @@ func initConfig() { viper.AutomaticEnv() } +func initGitConfig() { + setRootPath() + setGitHooksPath(getHooksPathFromGitConfig()) +} + func getRootPath() string { return rootPath } @@ -131,7 +134,12 @@ func getRootPath() string { func setRootPath() { cmd := exec.Command("git", "rev-parse", "--show-toplevel") - outputBytes, _ := cmd.CombinedOutput() + outputBytes, err := cmd.CombinedOutput() + + if err != nil { + log.Fatal(au.Brown(gitInitMessage)) + } + rootPath = strings.TrimSpace(string(outputBytes)) }