Skip to content

Commit

Permalink
refactor: move CLI commands to match Ory CLI structure
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This patch moves several CLI command to comply with the Ory CLI command structure:

```patch
- ory identities get ...
+ ory get identity ...

- ory identities delete ...
+ ory delete identity ...

- ory identities import ...
+ ory import identity ...

- ory identities list ...
+ ory list identities ...

- ory identities validate ...
+ ory validate identity ...

- ory jsonnet format ...
+ ory format jsonnet ...

- ory jsonnet lint ...
+ ory lint jsonnet ...
```
  • Loading branch information
aeneasr committed May 2, 2022
1 parent e6b38c2 commit d11a9a9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 23 deletions.
23 changes: 13 additions & 10 deletions cmd/cliclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"os"
"time"

"github.com/pkg/errors"

"github.com/hashicorp/go-retryablehttp"

"github.com/spf13/cobra"

"github.com/spf13/pflag"

kratos "github.com/ory/kratos-client-go"
"github.com/ory/x/cmdx"
)

const (
Expand All @@ -27,34 +28,36 @@ const (
ClientContextKey ContextKey = iota + 1
)

func NewClient(cmd *cobra.Command) *kratos.APIClient {
if f, ok := cmd.Context().Value(ClientContextKey).(func(cmd *cobra.Command) *kratos.APIClient); ok {
func NewClient(cmd *cobra.Command) (*kratos.APIClient, error) {
if f, ok := cmd.Context().Value(ClientContextKey).(func(cmd *cobra.Command) (*kratos.APIClient, error)); ok {
return f(cmd)
} else if f != nil {
panic(fmt.Sprintf("ClientContextKey was expected to be *client.OryKratos but it contained an invalid type %T ", f))
return nil, errors.Errorf("ClientContextKey was expected to be *client.OryKratos but it contained an invalid type %T ", f)
}

endpoint, err := cmd.Flags().GetString(FlagEndpoint)
cmdx.Must(err, "flag access error: %s", err)
if err != nil {
return nil, errors.WithStack(err)
}

if endpoint == "" {
endpoint = os.Getenv(envKeyEndpoint)
}

if endpoint == "" {
// no endpoint is set
_, _ = fmt.Fprintln(os.Stderr, "You have to set the remote endpoint, try --help for details.")
os.Exit(1)
return nil, errors.Errorf("you have to set the remote endpoint, try --help for details")
}

u, err := url.Parse(endpoint)
cmdx.Must(err, `Could not parse the endpoint URL "%s".`, endpoint)
if err != nil {
return nil, errors.Wrapf(err, `could not parse the endpoint URL "%s"`, endpoint)
}

conf := kratos.NewConfiguration()
conf.HTTPClient = retryablehttp.NewClient().StandardClient()
conf.HTTPClient.Timeout = time.Second * 10
conf.Servers = kratos.ServerConfigurations{{URL: u.String()}}
return kratos.NewAPIClient(conf)
return kratos.NewAPIClient(conf), nil
}

func RegisterClientFlags(flags *pflag.FlagSet) {
Expand Down
5 changes: 4 additions & 1 deletion cmd/identities/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func NewDeleteIdentityCmd(root *cobra.Command) *cobra.Command {
%[1]s delete identity $(%[1]s list identities --format json | jq -r 'map(select(.recovery_addresses[].value == "foo@bar.com")) | .[].id')`, root.Use),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c := cliclient.NewClient(cmd)
c, err := cliclient.NewClient(cmd)
if err != nil {
return err
}

var (
deleted = make([]string, 0, len(args))
Expand Down
5 changes: 4 additions & 1 deletion cmd/identities/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func NewGetIdentityCmd(root *cobra.Command) *cobra.Command {
%s get identity $(%[1]s ls identities --format json | jq -r 'map(select(.recovery_addresses[].value | endswith("@ory.sh"))) | .[].id')`, root.Use),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c := cliclient.NewClient(cmd)
c, err := cliclient.NewClient(cmd)
if err != nil {
return err
}

// we check includeCreds argument is valid
for _, opt := range includeCreds {
Expand Down
5 changes: 4 additions & 1 deletion cmd/identities/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ Files can contain only a single or an array of identities. The validity of files
WARNING: Importing credentials is not yet supported.`,
RunE: func(cmd *cobra.Command, args []string) error {
c := cliclient.NewClient(cmd)
c, err := cliclient.NewClient(cmd)
if err != nil {
return err
}

imported := make([]kratos.Identity, 0, len(args))
failed := make(map[string]error)
Expand Down
8 changes: 6 additions & 2 deletions cmd/identities/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ func NewListIdentitiesCmd(root *cobra.Command) *cobra.Command {
},
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
c := cliclient.NewClient(cmd)
c, err := cliclient.NewClient(cmd)
if err != nil {
return err
}

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

if len(args) == 2 {
Expand All @@ -59,7 +63,7 @@ func NewListIdentitiesCmd(root *cobra.Command) *cobra.Command {

identities, _, err := req.Execute()
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not get the identities: %+v\n", err)
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not list identities: %+v\n", err)
return cmdx.FailSilently(cmd)
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/identities/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ func NewValidateIdentityCmd() *cobra.Command {
It validates against the payload of the API and the identity schema as configured in Ory Kratos.
Identities can be supplied via STD_IN or JSON files containing a single or an array of identities.`,
RunE: func(cmd *cobra.Command, args []string) error {
c := cliclient.NewClient(cmd)
c, err := cliclient.NewClient(cmd)
if err != nil {
return err
}

is, err := readIdentities(cmd, args)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions cmd/remote/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,24 @@ var statusCmd = &cobra.Command{
Use: "status",
Short: "Print the alive and readiness status of a Ory Kratos instance",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
c := cliclient.NewClient(cmd)
RunE: func(cmd *cobra.Command, args []string) error {
c, err := cliclient.NewClient(cmd)
state := &statusState{}
defer cmdx.PrintRow(cmd, state)

alive, _, err := c.MetadataApi.IsAlive(cmd.Context()).Execute()
if err != nil {
return
return err
}

state.Alive = alive.Status == "ok"

ready, _, err := c.MetadataApi.IsReady(cmd.Context()).Execute()
if err != nil {
return
return err
}

state.Ready = ready.Status == "ok"
return nil
},
}
9 changes: 6 additions & 3 deletions cmd/remote/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version of an Ory Kratos instance",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
c := cliclient.NewClient(cmd)
RunE: func(cmd *cobra.Command, args []string) error {
c, err := cliclient.NewClient(cmd)

resp, _, err := c.MetadataApi.GetVersion(cmd.Context()).Execute()
cmdx.Must(err, "Could not get version: %s", err)
if err != nil {
return err
}

cmdx.PrintRow(cmd, &versionValue{Version: resp.Version})
return nil
},
}

0 comments on commit d11a9a9

Please sign in to comment.