Skip to content

Commit

Permalink
[FEAT] allow list operators/accounts/users to show its output as JSON (
Browse files Browse the repository at this point in the history
…#692)

Fixes #660

Signed-off-by: Alberto Ricart <[email protected]>
  • Loading branch information
aricart authored Jan 5, 2025
1 parent 0c8fabe commit e838251
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cmd

import (
"encoding/json"
"errors"
"fmt"
"sort"
Expand All @@ -35,6 +36,8 @@ var listCmd = &cobra.Command{

func init() {
GetRootCmd().AddCommand(listCmd)
listCmd.PersistentFlags().BoolVarP(&Json, "json", "J", false, "describe as JSON")

listCmd.AddCommand(createListOperatorsCmd())
listCmd.AddCommand(createListAccountsCmd())
listCmd.AddCommand(createListUsersCmd())
Expand All @@ -46,6 +49,17 @@ type EntryInfo struct {
Err error
}

func (ei EntryInfo) MarshalJSON() ([]byte, error) {
// Customize the JSON output
return json.Marshal(struct {
Name string `json:"name"`
PublicKey string `json:"public_key"`
}{
Name: ei.Name,
PublicKey: ei.Claims.Claims().Subject,
})
}

func createListOperatorsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "operators",
Expand Down Expand Up @@ -242,7 +256,18 @@ func createListUsersCmd() *cobra.Command {
return cmd
}

func listEntriesAsJSON(infos []*EntryInfo) string {
data, err := json.MarshalIndent(infos, "", " ")
if err != nil {
return "[]"
}
return string(data)
}

func listEntities(title string, infos []*EntryInfo, current string) string {
if Json {
return listEntriesAsJSON(infos)
}
table := tablewriter.CreateTable()
table.AddTitle(title)
if len(infos) == 0 {
Expand Down
79 changes: 79 additions & 0 deletions cmd/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmd

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type entryJSON struct {
Name string `json:"name"`
PublicKey string `json:"public_key"`
}

func Test_ListOperatorsJSON(t *testing.T) {
ts := NewTestStore(t, "O")
defer ts.Done(t)
ts.AddOperator(t, "OO")

cmd := createListOperatorsCmd()
cmd.PersistentFlags().BoolVarP(&Json, "json", "J", false, "describe as JSON")

_, stderr, err := ExecuteCmd(cmd, "--json")
require.NoError(t, err)

var entries []entryJSON
require.NoError(t, json.Unmarshal([]byte(stderr), &entries))
assert.Len(t, entries, 2)
assert.Equal(t, entries[0].Name, "O")
assert.NotEmpty(t, entries[0].PublicKey)
assert.Equal(t, entries[1].Name, "OO")
assert.NotEmpty(t, entries[1].PublicKey)
}

func Test_ListAccountsJSON(t *testing.T) {
ts := NewTestStore(t, "O")
defer ts.Done(t)

ts.AddAccount(t, "A")
ts.AddAccount(t, "B")

cmd := createListAccountsCmd()
cmd.PersistentFlags().BoolVarP(&Json, "json", "J", false, "describe as JSON")

_, stderr, err := ExecuteCmd(cmd, "--json")
require.NoError(t, err)

var entries []entryJSON
require.NoError(t, json.Unmarshal([]byte(stderr), &entries))
assert.Len(t, entries, 2)
assert.Equal(t, entries[0].Name, "A")
assert.Equal(t, entries[0].PublicKey, ts.GetAccountPublicKey(t, "A"))
assert.Equal(t, entries[1].Name, "B")
assert.Equal(t, entries[1].PublicKey, ts.GetAccountPublicKey(t, "B"))
}

func Test_ListUsersJSON(t *testing.T) {
ts := NewTestStore(t, "O")
defer ts.Done(t)

ts.AddAccount(t, "A")
ts.AddUser(t, "A", "U")
ts.AddUser(t, "A", "UU")

cmd := createListUsersCmd()
cmd.PersistentFlags().BoolVarP(&Json, "json", "J", false, "describe as JSON")

_, stderr, err := ExecuteCmd(cmd, "--json")
require.NoError(t, err)

var entries []entryJSON
require.NoError(t, json.Unmarshal([]byte(stderr), &entries))
assert.Len(t, entries, 2)
assert.Equal(t, entries[0].Name, "U")
assert.Equal(t, entries[0].PublicKey, ts.GetUserPublicKey(t, "A", "U"))
assert.Equal(t, entries[1].Name, "UU")
assert.Equal(t, entries[1].PublicKey, ts.GetUserPublicKey(t, "A", "UU"))
}

0 comments on commit e838251

Please sign in to comment.