From 747ca162edae64670fdb467e70ae0c5261cdedb1 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Mon, 26 Feb 2024 09:18:13 -0700 Subject: [PATCH] Fixed how config file worked --- cmd/login.go | 20 ++++++-------------- cmd/root.go | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/cmd/login.go b/cmd/login.go index 56c4672..1817a7c 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -2,7 +2,6 @@ package cmd import ( opaal "davidallendj/opaal/internal" - "davidallendj/opaal/internal/util" "fmt" "os" @@ -13,23 +12,15 @@ var loginCmd = &cobra.Command{ Use: "login", Short: "Start the login flow", Run: func(cmd *cobra.Command, args []string) { - // load config if found - if configPath != "" { - exists, err := util.PathExists(configPath) + for { + err := opaal.Login(&config) if err != nil { - fmt.Printf("failed to load config") + fmt.Printf("%v\n", err) os.Exit(1) - } else if exists { - config = opaal.LoadConfig(configPath) - } else { - config = opaal.NewConfig() + } else if config.RunOnce { + break } } - err := opaal.Login(&config) - if err != nil { - fmt.Print(err) - os.Exit(1) - } }, } @@ -45,5 +36,6 @@ func init() { 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.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) } diff --git a/cmd/root.go b/cmd/root.go index 19edc8e..ad2cbbd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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") }