Skip to content

Commit

Permalink
feat: allow again to use alternative configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
beliven-fabrizio-gortani committed Jan 11, 2023
1 parent 6686f21 commit b319b6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 14 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,26 @@ func initConfig() {
os.Exit(1)
}

var configFolder = "/.config/opsi/"
var configName = "config"

helpers.ConfigInit(ConfigTemplate, "/.config/opsi/config.yml")

// Check if the user just use exist
if _, err := os.Stat(home + "/.opsi.yml"); err == nil {
configFolder = ""
configName = ".opsi"
}

// Search config in home directory with name ".cobra" (without extension).
viper.AddConfigPath(home + "/.config/opsi/")
viper.SetConfigName("config")
viper.AddConfigPath(home + configFolder)
viper.SetConfigName(configName)
viper.SetConfigType("yml")

// Read config
err = viper.ReadInConfig()
if err != nil {
err := helpers.ConfigInit(ConfigTemplate)
if err != nil {
fmt.Println("Cannot Initialize configuration file")
os.Exit(1)
}

fmt.Println("Configuration file created successfully! Please relaunch now the command")

fmt.Println("Config file error", err.Error())
os.Exit(0)
}

Expand Down
4 changes: 2 additions & 2 deletions helpers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
)

func ConfigInit(template embed.FS) error {
func ConfigInit(template embed.FS, path string) error {
content, err := template.ReadFile("config/template.yml")
if err != nil {
return err
Expand All @@ -18,7 +18,7 @@ func ConfigInit(template embed.FS) error {
return err
}

configTargetPath := userHomeDir + "/.config/opsi/config.yml"
configTargetPath := userHomeDir + path

err = os.MkdirAll(filepath.Dir(configTargetPath), 0755)
if err != nil {
Expand Down

0 comments on commit b319b6d

Please sign in to comment.