From 0005f357a049ecbf94d76a1e73434837753a04ea Mon Sep 17 00:00:00 2001 From: aeneasr <3372410+aeneasr@users.noreply.github.com> Date: Mon, 2 May 2022 13:56:58 +0200 Subject: [PATCH] fix: make delete formattable --- cmd/identities/definitions.go | 36 +++++++++++++++++++++++++++++++++++ cmd/identities/delete.go | 22 ++++++++++----------- cmd/identities/delete_test.go | 6 ++++-- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/cmd/identities/definitions.go b/cmd/identities/definitions.go index f25d0d055926..b48c6c93d30f 100644 --- a/cmd/identities/definitions.go +++ b/cmd/identities/definitions.go @@ -9,10 +9,14 @@ import ( ) type ( + outputIder string outputIdentity kratos.Identity outputIdentityCollection struct { identities []kratos.Identity } + outputIderCollection struct { + ids []outputIder + } ) func (_ *outputIdentity) Header() []string { @@ -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"} } @@ -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) +} diff --git a/cmd/identities/delete.go b/cmd/identities/delete.go index 27beb74ece00..c3bcd33e2b7e 100644 --- a/cmd/identities/delete.go +++ b/cmd/identities/delete.go @@ -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 }, } diff --git a/cmd/identities/delete_test.go b/cmd/identities/delete_test.go index 4596108bb366..034fd280a558 100644 --- a/cmd/identities/delete_test.go +++ b/cmd/identities/delete_test.go @@ -6,6 +6,8 @@ import ( "strings" "testing" + "github.com/tidwall/gjson" + "github.com/spf13/cobra" "github.com/ory/kratos/cmd/identities" @@ -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) @@ -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)