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,7 +2,6 @@ package cmd
import ( import (
opaal "davidallendj/opaal/internal" opaal "davidallendj/opaal/internal"
"davidallendj/opaal/internal/util"
"fmt" "fmt"
"os" "os"
@ -13,22 +12,14 @@ var loginCmd = &cobra.Command{
Use: "login", Use: "login",
Short: "Start the login flow", Short: "Start the login flow",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
// load config if found for {
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()
}
}
err := opaal.Login(&config) err := opaal.Login(&config)
if err != nil { if err != nil {
fmt.Print(err) fmt.Printf("%v\n", err)
os.Exit(1) os.Exit(1)
} else if config.RunOnce {
break
}
} }
}, },
} }
@ -45,5 +36,6 @@ func init() {
loginCmd.Flags().BoolVar(&config.OpenBrowser, "open-browser", config.OpenBrowser, "automatically open link in browser") loginCmd.Flags().BoolVar(&config.OpenBrowser, "open-browser", config.OpenBrowser, "automatically open link in browser")
loginCmd.Flags().BoolVar(&config.DecodeIdToken, "decode-id-token", config.DecodeIdToken, "decode and print ID token from identity provider") loginCmd.Flags().BoolVar(&config.DecodeIdToken, "decode-id-token", config.DecodeIdToken, "decode and print ID token from identity provider")
loginCmd.Flags().BoolVar(&config.DecodeAccessToken, "decore-access-token", config.DecodeAccessToken, "decode and print access token from authorization server") loginCmd.Flags().BoolVar(&config.DecodeAccessToken, "decore-access-token", config.DecodeAccessToken, "decode and print access token from authorization server")
loginCmd.Flags().BoolVar(&config.RunOnce, "once", config.RunOnce, "set whether to run login once and exit")
rootCmd.AddCommand(loginCmd) rootCmd.AddCommand(loginCmd)
} }

View file

@ -2,6 +2,7 @@ package cmd
import ( import (
opaal "davidallendj/opaal/internal" opaal "davidallendj/opaal/internal"
"davidallendj/opaal/internal/util"
"fmt" "fmt"
"os" "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() { func Execute() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "failed to start CLI: %s", err) fmt.Fprintf(os.Stderr, "failed to start CLI: %s", err)
@ -28,5 +44,6 @@ func Execute() {
} }
func init() { func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "set the config path") rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "set the config path")
} }