Fixed how config file worked

This commit is contained in:
David Allen 2024-02-26 09:18:13 -07:00
parent 98b27bab28
commit 747ca162ed
No known key found for this signature in database
GPG key ID: 1D2A29322FBB6FCB
2 changed files with 23 additions and 14 deletions

View file

@ -2,6 +2,7 @@ package cmd
import (
opaal "davidallendj/opaal/internal"
"davidallendj/opaal/internal/util"
"fmt"
"os"
@ -20,6 +21,21 @@ var rootCmd = &cobra.Command{
},
}
func initConfig() {
// load config if found or create a new one
if configPath != "" {
exists, err := util.PathExists(configPath)
if err != nil {
fmt.Printf("failed to load config")
os.Exit(1)
} else if exists {
config = opaal.LoadConfig(configPath)
} else {
config = opaal.NewConfig()
}
}
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "failed to start CLI: %s", err)
@ -28,5 +44,6 @@ func Execute() {
}
func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "set the config path")
}