Skip to content

Commit

Permalink
fix: make delete formattable
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed May 2, 2022
1 parent dd44593 commit 0005f35
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
36 changes: 36 additions & 0 deletions cmd/identities/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import (
)

type (
outputIder string
outputIdentity kratos.Identity
outputIdentityCollection struct {
identities []kratos.Identity
}
outputIderCollection struct {
ids []outputIder
}
)

func (_ *outputIdentity) Header() []string {
Expand Down Expand Up @@ -53,6 +57,18 @@ func (i *outputIdentity) Interface() interface{} {
return i
}

func (_ outputIder) Header() []string {
return []string{"ID"}
}

func (i outputIder) Columns() []string {
return []string{string(i)}
}

func (i outputIder) Interface() interface{} {
return i
}

func (_ *outputIdentityCollection) Header() []string {
return []string{"ID", "VERIFIED ADDRESS 1", "RECOVERY ADDRESS 1", "SCHEMA ID", "SCHEMA URL"}
}
Expand Down Expand Up @@ -91,3 +107,23 @@ func (c *outputIdentityCollection) Interface() interface{} {
func (c *outputIdentityCollection) Len() int {
return len(c.identities)
}

func (_ *outputIderCollection) Header() []string {
return []string{"ID"}
}

func (c *outputIderCollection) Table() [][]string {
rows := make([][]string, len(c.ids))
for i, ident := range c.ids {
rows[i] = []string{string(ident)}
}
return rows
}

func (c *outputIderCollection) Interface() interface{} {
return c.ids
}

func (c *outputIderCollection) Len() int {
return len(c.ids)
}
22 changes: 11 additions & 11 deletions cmd/identities/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,30 @@ func NewDeleteIdentityCmd(root *cobra.Command) *cobra.Command {
}

var (
deleted = make([]string, 0, len(args))
errs []error
deleted = make([]outputIder, 0, len(args))
failed = make(map[string]error)
)

for _, a := range args {
_, err := c.V0alpha2Api.AdminDeleteIdentity(cmd.Context(), a).Execute()
if err != nil {
errs = append(errs, cloudx.PrintOpenAPIError(cmd, err))
failed[a] = cloudx.PrintOpenAPIError(cmd, err)
continue
}
deleted = append(deleted, a)
deleted = append(deleted, outputIder(a))
}

for _, d := range deleted {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), d)
if len(deleted) == 1 {
cmdx.PrintRow(cmd, &deleted[0])
} else if len(deleted) > 1 {
cmdx.PrintTable(cmd, &outputIderCollection{deleted})
}

for _, err := range errs {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%+v\n", err)
}

if len(errs) != 0 {
cmdx.PrintErrors(cmd, failed)
if len(failed) != 0 {
return cmdx.FailSilently(cmd)
}

return nil
},
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/identities/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"testing"

"github.com/tidwall/gjson"

"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/identities"
Expand All @@ -31,7 +33,7 @@ func TestDeleteCmd(t *testing.T) {
stdOut := execNoErr(t, c, i.ID.String())

// expect ID and no error
assert.Equal(t, i.ID.String()+"\n", stdOut)
assert.Equal(t, i.ID.String(), gjson.Parse(stdOut).String())

// expect identity to be deleted
_, err := reg.Persister().GetIdentity(context.Background(), i.ID)
Expand All @@ -43,7 +45,7 @@ func TestDeleteCmd(t *testing.T) {

stdOut := execNoErr(t, c, ids...)

assert.Equal(t, strings.Join(ids, "\n")+"\n", stdOut)
assert.Equal(t, `["`+strings.Join(ids, "\",\"")+"\"]\n", stdOut)

for _, i := range is {
_, err := reg.Persister().GetIdentity(context.Background(), i.ID)
Expand Down

0 comments on commit 0005f35

Please sign in to comment.