diff --git a/cmd/get.go b/cmd/get.go index bf625fe..d227721 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -28,10 +28,12 @@ var cacheCredentials bool var writeToFile string var cacheToFile string +const defaultOutput = "~/.aws/credentials" + func init() { RootCmd.AddCommand(cmdGet) cmdGet.Flags().StringVarP( - &output, "output", "o", "~/.aws/credentials", "How or where to output credentials. Two special values are supported 'environment' and 'credential_process'. All other values are interpreted as file paths", + &output, "output", "o", defaultOutput, "How or where to output credentials. Two special values are supported 'environment' and 'credential_process'. All other values are interpreted as file paths", ) cmdGet.Flags().BoolVarP( @@ -45,7 +47,7 @@ func init() { // Keep the old flags as is. cmdGet.Flags().StringVarP( - &writeToFile, "write-to-file", "w", "~/.aws/credentials", + &writeToFile, "write-to-file", "w", defaultOutput, "Write credentials to this file instead of the default", ) cmdGet.Flags().BoolVarP( @@ -79,7 +81,8 @@ func preferredOutput(cmd *cobra.Command, app string) string { if err != nil { log.Log.Warnf("Error getting output flag: %v", err) } - if out != "" { + if out != "" && out != defaultOutput { + log.Log.Tracef("output flag sets output to: %s", out) return out } @@ -87,25 +90,29 @@ func preferredOutput(cmd *cobra.Command, app string) string { if err != nil { log.Log.Warnf("Error getting write-to-file flag: %v", err) } - if out != "" { + if out != "" && out != defaultOutput { + log.Log.Tracef("write-to-file flag sets output: %s", out) return out } out = viper.GetString(fmt.Sprintf("apps.%s.output", app)) if out != "" { + log.Log.Tracef("App specific config sets output to: %s", out) return out } out = viper.GetString("global.output") if out != "" { + log.Log.Tracef("Global config sets output to: %s", out) return out } - return "~/.aws/credentials" + return defaultOutput } func setOutput(cmd *cobra.Command, app string) { o := preferredOutput(cmd, app) + log.Log.Tracef("Preferred output: %s", o) writeToFile = "" switch o { case "environment": diff --git a/cmd/get_test.go b/cmd/get_test.go index a6d326e..cb65b2c 100644 --- a/cmd/get_test.go +++ b/cmd/get_test.go @@ -8,9 +8,13 @@ package cmd import ( "testing" + "github.com/allcloud-io/clisso/log" + "github.com/spf13/cobra" "github.com/spf13/viper" ) +var _ = log.NewLogger("panic", "", false) + var testdata = []struct { app int32 provider int32 @@ -33,3 +37,42 @@ func TestSessionDuration(t *testing.T) { } } } + +func TestPreferredOutput(t *testing.T) { + testdata := []struct { + outputFlag string + writeToFileFlag string + appConfig string + globalConfig string + result string + }{ + {"environment", "", "", "", "environment"}, + {"credential_process", "", "", "", "credential_process"}, + {"", "", "~/.aws/test", "", "~/.aws/test"}, + {"", "", "", "test", "test"}, + {defaultOutput, "", "", "", defaultOutput}, + {defaultOutput, "", "credential_process", "", "credential_process"}, + {defaultOutput, "", "credential_process", "~/global", "credential_process"}, + {defaultOutput, "", "", "~/global", "~/global"}, + {"~/test", "", "credential_process", "", "~/test"}, + {"~/test", "", "credential_process", "~/global", "~/test"}, + {"~/test", "", "", "~/global", "~/test"}, + } + for _, tc := range testdata { + viper.Set("apps.test.output", tc.appConfig) + viper.Set("global.output", tc.globalConfig) + + cmd := &cobra.Command{} + cmd.Flags().StringVarP( + &output, "output", "o", tc.outputFlag, "fake", + ) + cmd.Flags().StringVarP( + &output, "write-to-file", "f", tc.outputFlag, "fake legacy flag", + ) + + res := preferredOutput(cmd, "test") + if res != tc.result { + t.Fatalf("Invalid output: got %v, want: %v", res, tc.result) + } + } +}