Skip to content

Commit

Permalink
fix(cli): dry up code (#2572)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Jul 16, 2022
1 parent 071c885 commit d1b6b40
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 173 deletions.
36 changes: 0 additions & 36 deletions cmd/identities/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ import (
)

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

func (_ outputIdentity) Header() []string {
Expand Down Expand Up @@ -57,18 +53,6 @@ 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 outputIdentity{}.Header()
}
Expand All @@ -88,23 +72,3 @@ 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)
}
6 changes: 3 additions & 3 deletions cmd/identities/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewDeleteIdentityCmd(root *cobra.Command) *cobra.Command {
}

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

Expand All @@ -49,13 +49,13 @@ func NewDeleteIdentityCmd(root *cobra.Command) *cobra.Command {
failed[a] = cmdx.PrintOpenAPIError(cmd, err)
continue
}
deleted = append(deleted, outputIder(a))
deleted = append(deleted, cmdx.OutputIder(a))
}

if len(deleted) == 1 {
cmdx.PrintRow(cmd, &deleted[0])
} else if len(deleted) > 1 {
cmdx.PrintTable(cmd, &outputIderCollection{deleted})
cmdx.PrintTable(cmd, &cmdx.OutputIderCollection{Items: deleted})
}

cmdx.PrintErrors(cmd, failed)
Expand Down
30 changes: 6 additions & 24 deletions cmd/identities/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package identities

import (
"fmt"
"strconv"

"github.com/ory/x/cmdx"

"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/x/cmdx"
)

func NewListCmd(root *cobra.Command) *cobra.Command {
Expand All @@ -29,13 +27,7 @@ func NewListIdentitiesCmd(root *cobra.Command) *cobra.Command {
Short: "List identities",
Long: "List identities (paginated)",
Example: fmt.Sprintf("%[1]s ls identities 100 1", root.Use),
Args: func(cmd *cobra.Command, args []string) error {
// zero or exactly two args
if len(args) != 0 && len(args) != 2 {
return fmt.Errorf("expected zero or two args, got %d: %+v", len(args), args)
}
return nil
},
Args: cmdx.ZeroOrTwoArgs,
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
c, err := cliclient.NewClient(cmd)
Expand All @@ -44,20 +36,13 @@ func NewListIdentitiesCmd(root *cobra.Command) *cobra.Command {
}

req := c.V0alpha2Api.AdminListIdentities(cmd.Context())

if len(args) == 2 {
page, err := strconv.ParseInt(args[0], 0, 64)
page, perPage, err := cmdx.ParsePaginationArgs(cmd, args[0], args[1])
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not parse page argument\"%s\": %s", args[0], err)
return cmdx.FailSilently(cmd)
return err
}
req = req.Page(page)

perPage, err := strconv.ParseInt(args[1], 0, 64)
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not parse per-page argument\"%s\": %s", args[1], err)
return cmdx.FailSilently(cmd)
}
req = req.Page(page)
req = req.PerPage(perPage)
}

Expand All @@ -66,10 +51,7 @@ func NewListIdentitiesCmd(root *cobra.Command) *cobra.Command {
return cmdx.PrintOpenAPIError(cmd, err)
}

cmdx.PrintTable(cmd, &outputIdentityCollection{
identities: identities,
})

cmdx.PrintTable(cmd, &outputIdentityCollection{identities: identities})
return nil
},
}
Expand Down
43 changes: 22 additions & 21 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ require (
github.com/go-swagger/go-swagger v0.26.1
github.com/gobuffalo/fizz v1.14.0
github.com/gobuffalo/httptest v1.0.2
github.com/gobuffalo/pop/v6 v6.0.1
github.com/gofrs/uuid v4.1.0+incompatible
github.com/gobuffalo/pop/v6 v6.0.4-0.20220524160009-195240e4a669
github.com/gofrs/uuid v4.2.0+incompatible
github.com/golang-jwt/jwt/v4 v4.1.0
github.com/golang/gddo v0.0.0-20190904175337-72a348e765d2
github.com/golang/mock v1.6.0
Expand All @@ -68,7 +68,7 @@ require (
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe
github.com/ory/analytics-go/v4 v4.0.3
github.com/ory/dockertest/v3 v3.8.1
github.com/ory/dockertest/v3 v3.9.0
github.com/ory/go-acc v0.2.8
github.com/ory/go-convenience v0.1.0
github.com/ory/graceful v0.1.1
Expand All @@ -77,11 +77,11 @@ require (
github.com/ory/kratos-client-go v0.6.3-alpha.1
github.com/ory/mail/v3 v3.0.0
github.com/ory/nosurf v1.2.7
github.com/ory/x v0.0.392
github.com/ory/x v0.0.435
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2
github.com/pkg/errors v0.9.1
github.com/pquerna/otp v1.3.0
github.com/rs/cors v1.8.0
github.com/rs/cors v1.8.2
github.com/sirupsen/logrus v1.8.1
github.com/slack-go/slack v0.7.4
github.com/spf13/cobra v1.4.0
Expand All @@ -94,8 +94,8 @@ require (
github.com/zmb3/spotify/v2 v2.0.0
go.opentelemetry.io/otel v1.7.0
go.opentelemetry.io/otel/trace v1.7.0
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa
golang.org/x/net v0.0.0-20211020060615-d418f374d309
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/tools v0.1.10
Expand All @@ -107,7 +107,7 @@ require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
Expand All @@ -117,26 +117,27 @@ require (
github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 // indirect
github.com/armon/go-metrics v0.3.10 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/avast/retry-go/v4 v4.0.5 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/bmatcuk/doublestar v1.3.4 // indirect
github.com/boombuler/barcode v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cloudflare/cfssl v1.6.1 // indirect
github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4 // indirect
github.com/cncf/xds/go v0.0.0-20211130200136-a8f946100490 // indirect
github.com/cockroachdb/cockroach-go/v2 v2.2.7 // indirect
github.com/cockroachdb/cockroach-go/v2 v2.2.10 // indirect
github.com/containerd/containerd v1.5.7 // indirect
github.com/containerd/continuity v0.2.1 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/cortesi/moddwatch v0.0.0-20210222043437-a6aaad86a36e // indirect
github.com/cortesi/termlog v0.0.0-20210222042314-a1eec763abec // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/docker/cli v20.10.11+incompatible // indirect
github.com/docker/cli v20.10.14+incompatible // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.9+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
Expand Down Expand Up @@ -169,11 +170,11 @@ require (
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gobuffalo/envy v1.10.1 // indirect
github.com/gobuffalo/flect v0.2.4 // indirect
github.com/gobuffalo/flect v0.2.5 // indirect
github.com/gobuffalo/github_flavored_markdown v1.1.1 // indirect
github.com/gobuffalo/helpers v0.6.4 // indirect
github.com/gobuffalo/nulls v0.4.1 // indirect
github.com/gobuffalo/plush/v4 v4.1.9 // indirect
github.com/gobuffalo/plush/v4 v4.1.11 // indirect
github.com/gobuffalo/tags/v3 v3.1.2 // indirect
github.com/gobuffalo/validate/v3 v3.3.1 // indirect
github.com/goccy/go-yaml v1.9.5 // indirect
Expand Down Expand Up @@ -203,18 +204,18 @@ require (
github.com/huandu/xstrings v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.1 // indirect
github.com/jackc/pgconn v1.12.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.2.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.9.0 // indirect
github.com/jackc/pgx/v4 v4.14.0 // indirect
github.com/jackc/pgtype v1.11.0 // indirect
github.com/jackc/pgx/v4 v4.16.1 // indirect
github.com/jandelgado/gcov2lcov v1.0.5 // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/jhump/protoreflect v1.8.2 // indirect
github.com/jinzhu/copier v0.3.5 // indirect
github.com/jmoiron/sqlx v1.3.4 // indirect
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand All @@ -223,7 +224,7 @@ require (
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/lib/pq v1.10.4 // indirect
github.com/lib/pq v1.10.6 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/markbates/hmax v1.0.0 // indirect
Expand All @@ -245,7 +246,7 @@ require (
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.0.2 // indirect
github.com/opencontainers/runc v1.1.1 // indirect
github.com/openzipkin/zipkin-go v0.4.0 // indirect
github.com/ory/viper v1.7.5 // indirect
github.com/pborman/uuid v1.2.1 // indirect
Expand Down

0 comments on commit d1b6b40

Please sign in to comment.