add(cli): allow project local configuration

The local configuration is prefered over the global configuration.
This commit is contained in:
2023-10-26 16:50:06 +02:00
parent 84bf78d8a9
commit 973338baff

View File

@@ -10,6 +10,7 @@ import (
const VERSION string = "0.1" const VERSION string = "0.1"
const CONFIG_DIR string = "/.config/gitw" const CONFIG_DIR string = "/.config/gitw"
const LOCAL_CONFIG_DIR string = "./.gitw"
var ( var (
version bool version bool
@@ -37,6 +38,7 @@ func initConfig() {
home := os.Getenv("HOME") home := os.Getenv("HOME")
viper.SetConfigName("config") viper.SetConfigName("config")
viper.SetConfigType("yaml") viper.SetConfigType("yaml")
viper.AddConfigPath(LOCAL_CONFIG_DIR)
viper.AddConfigPath(home + CONFIG_DIR) viper.AddConfigPath(home + CONFIG_DIR)
viper.AutomaticEnv() viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
@@ -45,11 +47,12 @@ func initConfig() {
viper.Set("base_url", "<git-http-base-url>") viper.Set("base_url", "<git-http-base-url>")
viper.Set("user_name", "<git-user-name>") viper.Set("user_name", "<git-user-name>")
viper.Set("access_code", "<git-access-code>") viper.Set("access_code", "<git-access-code>")
// write default configuration in configuration directory inside of HOME directory
if err = os.MkdirAll(home+CONFIG_DIR, os.ModePerm); err != nil { 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) fmt.Fprintf(os.Stderr, "Could not create directories for configuration file: %s\nError: %s\n", home+CONFIG_DIR, err)
os.Exit(-1) 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) fmt.Fprintf(os.Stderr, "Failed to write starting configuration file: %s\n", err)
os.Exit(-1) os.Exit(-1)
} }