mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 12:45:47 +00:00
refactor: change the way the viper config is stored and add helper functions
This commit is contained in:
+44
-5
@@ -40,10 +40,11 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var v *viper.Viper
|
||||||
|
|
||||||
func LoadConfig() (*Config, error) {
|
func LoadConfig() (*Config, error) {
|
||||||
// TODO: create if and give param as default config location
|
// TODO: create if and give param as default config location
|
||||||
|
v = viper.New()
|
||||||
v := viper.New()
|
|
||||||
v.SetConfigName(configName)
|
v.SetConfigName(configName)
|
||||||
v.SetConfigType(configType)
|
v.SetConfigType(configType)
|
||||||
|
|
||||||
@@ -92,7 +93,7 @@ func (c *Config) SetGlobalConfig() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleMissingConfig(v *viper.Viper) error {
|
func HandleMissingConfig() error {
|
||||||
fmt.Println("Config file not found. Would you like to create one? (y/n)")
|
fmt.Println("Config file not found. Would you like to create one? (y/n)")
|
||||||
var response string
|
var response string
|
||||||
if _, err := fmt.Scanln(&response); err != nil {
|
if _, err := fmt.Scanln(&response); err != nil {
|
||||||
@@ -104,10 +105,48 @@ func handleMissingConfig(v *viper.Viper) error {
|
|||||||
return fmt.Errorf("config file not found")
|
return fmt.Errorf("config file not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return createConfig(v)
|
if v == nil {
|
||||||
|
v = viper.New()
|
||||||
|
|
||||||
|
v.SetConfigName(configName)
|
||||||
|
v.SetConfigType(configType)
|
||||||
|
}
|
||||||
|
|
||||||
|
return CreateConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
func createConfig(v *viper.Viper) error {
|
func CheckConfig() bool {
|
||||||
|
if v == nil {
|
||||||
|
return false
|
||||||
|
} else if v.ConfigFileUsed() == "" {
|
||||||
|
return false
|
||||||
|
} else {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetConfigFilePath() string {
|
||||||
|
if v == nil || v.ConfigFileUsed() == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return v.ConfigFileUsed()
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveConfig() error {
|
||||||
|
if v == nil || v.ConfigFileUsed() == "" {
|
||||||
|
return fmt.Errorf("no config file to remove")
|
||||||
|
}
|
||||||
|
|
||||||
|
configPath := v.ConfigFileUsed()
|
||||||
|
if err := os.Remove(configPath); err != nil {
|
||||||
|
return fmt.Errorf("failed to remove config file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Config file removed: %s\n", configPath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateConfig() error {
|
||||||
fmt.Println("Where would you like to create the config file?")
|
fmt.Println("Where would you like to create the config file?")
|
||||||
for i, path := range defaultConfigLocations {
|
for i, path := range defaultConfigLocations {
|
||||||
fmt.Printf("%d. %s\n", i, path)
|
fmt.Printf("%d. %s\n", i, path)
|
||||||
|
|||||||
Reference in New Issue
Block a user