diff --git a/cmd/list.go b/cmd/list.go index c46def1b..8e2b9f81 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -16,6 +16,7 @@ package cmd import ( + "encoding/json" "errors" "fmt" "sort" @@ -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()) @@ -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", @@ -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 { diff --git a/cmd/list_test.go b/cmd/list_test.go new file mode 100644 index 00000000..1db0e2a9 --- /dev/null +++ b/cmd/list_test.go @@ -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")) +}