Skip to content

Commit

Permalink
Fix bug where tool config was not automatically created (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrus2281 authored Nov 3, 2024
1 parent 5edda13 commit a37d473
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGE_LOGS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Version Change Logs

### Version 3.1.1
> Fixed the bug where the tool config was not being automatically created
### Version 3.1.0
> Added `g get` command, (moved getHome to get) - Tool ready for public use milestone
Expand Down
33 changes: 14 additions & 19 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"github.com/spf13/viper"
)

var cfgFile string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "g",
Expand Down Expand Up @@ -54,24 +52,21 @@ func initConfig() {
logger.SetLogLevel(logger.INFO)
}

if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
logger.CheckFatalln(err)
home = filepath.Join(home, internal.HOME_NAME)
if _, err := os.Stat(home); os.IsNotExist(err) {
// Create the directory
err = os.Mkdir(home, 0755)
logger.CheckFatalln(err)
}
// Find home directory.
home, err := os.UserHomeDir()
logger.CheckFatalln(err)
home = filepath.Join(home, internal.HOME_NAME)

// Search config in home directory with name ".gitBranchTool" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName("gitBranchTool.config")
// Search config in home directory with name ".gitBranchTool" (without extension).
viper.AddConfigPath(home)
viper.SetConfigType("yaml")
viper.SetConfigName(internal.CONFIG_NAME)

if _, err := os.Stat(home); os.IsNotExist(err) {
// Create the directory
err = os.Mkdir(home, 0755)
logger.CheckFatalln(err)
viper.SafeWriteConfig()
}

viper.AutomaticEnv() // read in environment variables that match
Expand Down
1 change: 1 addition & 0 deletions internal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func GetHome() string {
home, err := os.UserHomeDir()
logger.CheckFatalln(err)
gHome = filepath.Join(home, HOME_NAME)
viper.SafeWriteConfig()
if err := AddConfig("GIT_BRANCH_TOOL_HOME", gHome); err != nil {
logger.Fatalln(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package internal

const VERSION = "3.1.0"
const VERSION = "3.1.1"

0 comments on commit a37d473

Please sign in to comment.