Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doctl auth list: respect json output flag #1547

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions commands/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ limitations under the License.
package commands

import (
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -218,11 +219,16 @@ func RunAuthList(c *CmdConfig) error {
}
contexts := viper.GetStringMap("auth-contexts")

displayAuthContexts(c.Out, context, contexts)
if viper.GetString("output") == "json" {
displayAuthContextsJSON(c.Out, context, contexts)
} else {
displayAuthContexts(c.Out, context, contexts)
}

return nil
}

func displayAuthContexts(out io.Writer, currentContext string, contexts map[string]any) {
func ensureDefaultContextAndKeysOrder(contexts map[string]any) []string {
// Because the default context isn't present on the auth-contexts field,
// we add it manually so that it's always included in the output, and so
// we can check if it's the current context.
Expand All @@ -236,6 +242,31 @@ func displayAuthContexts(out io.Writer, currentContext string, contexts map[stri
}
sort.Strings(keys)

return keys
}

func displayAuthContextsJSON(out io.Writer, currentContext string, contexts map[string]any) {
type contextJSON struct {
Name string `json:"name"`
Current bool `json:"current"`
}

var contextsJSON []contextJSON

keys := ensureDefaultContextAndKeysOrder(contexts)
for _, ctx := range keys {
contextsJSON = append(contextsJSON, contextJSON{
Name: ctx,
Current: ctx == currentContext,
})
}

jsonData, _ := json.MarshalIndent(contextsJSON, "", " ")
fmt.Fprintln(out, string(jsonData))
}

func displayAuthContexts(out io.Writer, currentContext string, contexts map[string]any) {
keys := ensureDefaultContextAndKeysOrder(contexts)
for _, ctx := range keys {
if ctx == currentContext {
fmt.Fprintln(out, ctx, "(current)")
Expand Down
67 changes: 62 additions & 5 deletions commands/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,12 @@ func TestAuthList(t *testing.T) {

func Test_displayAuthContexts(t *testing.T) {
testCases := []struct {
Name string
Out *bytes.Buffer
Context string
Contexts map[string]any
Expected string
Name string
Out *bytes.Buffer
Context string
Contexts map[string]any
Expected string
ExpectedJSON string
}{
{
Name: "default context only",
Expand Down Expand Up @@ -227,6 +228,62 @@ func Test_displayAuthContexts(t *testing.T) {
}
}

func Test_displayAuthContextsJSON(t *testing.T) {
testCases := []struct {
Name string
Out *bytes.Buffer
Context string
Contexts map[string]any
ExpectedJSON string
}{
{
Name: "default context only",
Out: &bytes.Buffer{},
Context: doctl.ArgDefaultContext,
Contexts: map[string]any{
doctl.ArgDefaultContext: true,
},
ExpectedJSON: "[\n {\n \"name\": \"default\",\n \"current\": true\n }\n]\n",
},
{
Name: "default context and additional context",
Out: &bytes.Buffer{},
Context: doctl.ArgDefaultContext,
Contexts: map[string]any{
doctl.ArgDefaultContext: true,
"test": true,
},
ExpectedJSON: "[\n {\n \"name\": \"default\",\n \"current\": true\n },\n {\n \"name\": \"test\",\n \"current\": false\n }\n]\n",
},
{
Name: "default context and additional context set to additional context",
Out: &bytes.Buffer{},
Context: "test",
Contexts: map[string]any{
doctl.ArgDefaultContext: true,
"test": true,
},
ExpectedJSON: "[\n {\n \"name\": \"default\",\n \"current\": false\n },\n {\n \"name\": \"test\",\n \"current\": true\n }\n]\n",
},
{
Name: "unset context",
Out: &bytes.Buffer{},
Context: "missing",
Contexts: map[string]any{
doctl.ArgDefaultContext: true,
"test": true,
},
ExpectedJSON: "[\n {\n \"name\": \"default\",\n \"current\": false\n },\n {\n \"name\": \"test\",\n \"current\": false\n }\n]\n",
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
displayAuthContextsJSON(tc.Out, tc.Context, tc.Contexts)
assert.Equal(t, tc.ExpectedJSON, tc.Out.String())
})
}
}
func TestTokenInputValidator(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading