Skip to content

Commit

Permalink
fix output selection (#411)
Browse files Browse the repository at this point in the history
the output selection did not honor config file settings
  • Loading branch information
bitte-ein-bit authored Jun 7, 2024
1 parent 6b55ad1 commit bc7105b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
17 changes: 12 additions & 5 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -79,33 +81,38 @@ 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
}

out, err = cmd.Flags().GetString("write-to-file")
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":
Expand Down
43 changes: 43 additions & 0 deletions cmd/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
}

0 comments on commit bc7105b

Please sign in to comment.