From 973338baff1c0582d58b94a0b8ef4a0f2bf4ae56 Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Thu, 26 Oct 2023 16:50:06 +0200 Subject: [PATCH] add(cli): allow project local configuration The local configuration is prefered over the global configuration. --- cmd/root.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index 3b402b7..737802e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -10,6 +10,7 @@ import ( const VERSION string = "0.1" const CONFIG_DIR string = "/.config/gitw" +const LOCAL_CONFIG_DIR string = "./.gitw" var ( version bool @@ -37,6 +38,7 @@ func initConfig() { home := os.Getenv("HOME") viper.SetConfigName("config") viper.SetConfigType("yaml") + viper.AddConfigPath(LOCAL_CONFIG_DIR) viper.AddConfigPath(home + CONFIG_DIR) viper.AutomaticEnv() if err := viper.ReadInConfig(); err != nil { @@ -45,11 +47,12 @@ func initConfig() { viper.Set("base_url", "") viper.Set("user_name", "") viper.Set("access_code", "") + // write default configuration in configuration directory inside of HOME directory if err = os.MkdirAll(home+CONFIG_DIR, os.ModePerm); err != nil { fmt.Fprintf(os.Stderr, "Could not create directories for configuration file: %s\nError: %s\n", home+CONFIG_DIR, err) os.Exit(-1) } - if err = viper.SafeWriteConfig(); err != nil { + if err = viper.SafeWriteConfigAs(home + CONFIG_DIR + "config.yaml"); err != nil { fmt.Fprintf(os.Stderr, "Failed to write starting configuration file: %s\n", err) os.Exit(-1) }