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 Apr 5, 2022
1 parent aee2b1e commit 73910a3
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 124 deletions.
24 changes: 17 additions & 7 deletions cmd/identities/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ import (
"github.com/ory/x/cmdx"
)

func NewDeleteCmd() *cobra.Command {
func NewDeleteCmd(root *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Delete resources",
}
cmd.AddCommand(NewDeleteIdentityCmd(root))
cliclient.RegisterClientFlags(cmd.PersistentFlags())
cmdx.RegisterFormatFlags(cmd.PersistentFlags())
return cmd
}

func NewDeleteIdentityCmd(root *cobra.Command) *cobra.Command {
return &cobra.Command{
Use: "delete <id-0 [id-1 ...]>",
Short: "Delete identities by ID",
Use: "identity id-0 [id-1] [id-2] [id-n]",
Short: "Delete one or more identities by their ID(s)",
Long: fmt.Sprintf(`This command deletes one or more identities by ID. To delete an identity by some selector, e.g. the recovery email address, use the list command in combination with jq.
%s
`, clihelpers.WarningJQIsComplicated),
Example: `To delete the identity with the recovery email address "foo@bar.com", run:
%s`, clihelpers.WarningJQIsComplicated),
Example: fmt.Sprintf(`To delete the identity with the recovery email address "foo@bar.com", run:
$ kratos identities delete $(kratos identities list --format json | jq -r 'map(select(.recovery_addresses[].value == "foo@bar.com")) | .[].id')`,
%[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)
Expand Down
4 changes: 3 additions & 1 deletion 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/spf13/cobra"

"github.com/ory/kratos/cmd/identities"

"github.com/stretchr/testify/assert"
Expand All @@ -18,7 +20,7 @@ import (
)

func TestDeleteCmd(t *testing.T) {
c := identities.NewDeleteCmd()
c := identities.NewDeleteIdentityCmd(new(cobra.Command))
reg := setup(t, c)

t.Run("case=deletes successfully", func(t *testing.T) {
Expand Down
24 changes: 17 additions & 7 deletions cmd/identities/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,31 @@ const (
FlagIncludeCreds = "include-credentials"
)

func NewGetCmd() *cobra.Command {
func NewGetCmd(root *cobra.Command) *cobra.Command {
var cmd = &cobra.Command{
Use: "get",
Short: "Get resources",
}
cmd.AddCommand(NewGetIdentityCmd(root))
cliclient.RegisterClientFlags(cmd.PersistentFlags())
cmdx.RegisterFormatFlags(cmd.PersistentFlags())
return cmd
}

func NewGetIdentityCmd(root *cobra.Command) *cobra.Command {
var (
includeCreds []string
)

cmd := &cobra.Command{
Use: "get <id-0 [id-1 ...]>",
Short: "Get one or more identities by ID",
Use: "identity [id-1] [id-2] [id-n]",
Short: "Get one or more identities by their ID(s)",
Long: fmt.Sprintf(`This command gets all the details about an identity. To get an identity by some selector, e.g. the recovery email address, use the list command in combination with jq.
%s
`, clihelpers.WarningJQIsComplicated),
Example: `To get the identities with the recovery email address at the domain "ory.sh", run:
%s`, clihelpers.WarningJQIsComplicated),
Example: fmt.Sprintf(`To get the identities with the recovery email address at the domain "ory.sh", run:
$ kratos identities get $(kratos identities list --format json | jq -r 'map(select(.recovery_addresses[].value | endswith("@ory.sh"))) | .[].id')`,
%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)
Expand Down
4 changes: 3 additions & 1 deletion cmd/identities/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"encoding/json"
"testing"

"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/identities"
"github.com/ory/x/assertx"

Expand All @@ -19,7 +21,7 @@ import (
)

func TestGetCmd(t *testing.T) {
c := identities.NewGetCmd()
c := identities.NewGetIdentityCmd(new(cobra.Command))
reg := setup(t, c)

t.Run("case=gets a single identity", func(t *testing.T) {
Expand Down
45 changes: 30 additions & 15 deletions cmd/identities/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,38 @@ import (
"github.com/ory/kratos/cmd/cliclient"
)

// NewImportCmd represents the import command
func NewImportCmd() *cobra.Command {
return &cobra.Command{
Use: "import <file.json [file-2.json [file-3.json] ...]>",
Short: "Import identities from files or STD_IN",
Example: `$ cat > ./file.json <<EOF
{
"schema_id": "default",
"traits": {
"email": "foo@example.com"
}
func NewImportCmd(root *cobra.Command) *cobra.Command {
var cmd = &cobra.Command{
Use: "import",
Short: "Import resources",
}
cmd.AddCommand(NewImportIdentitiesCmd(root))
cliclient.RegisterClientFlags(cmd.PersistentFlags())
cmdx.RegisterFormatFlags(cmd.PersistentFlags())
return cmd
}
EOF

$ kratos identities import file.json
# Alternatively:
$ cat file.json | kratos identities import`,
// NewImportIdentitiesCmd represents the import command
func NewImportIdentitiesCmd(root *cobra.Command) *cobra.Command {
return &cobra.Command{
Use: "identities file-1.json [file-2.json] [file-3.json] [file-n.json]",
Short: "Import one or more identities from files or STD_IN",
Example: fmt.Sprintf(`Create an example identity:
cat > ./file.json <<EOF
{
"schema_id": "default",
"traits": {
"email": "foo@example.com"
}
}
EOF
%[1]s import identities file.json
Alternatively:
cat file.json | %[1]s import identities`, root.Use),
Long: `Import identities from files or STD_IN.
Files can contain only a single or an array of identities. The validity of files can be tested beforehand using "... identities validate".
Expand Down
4 changes: 3 additions & 1 deletion cmd/identities/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io/ioutil"
"testing"

"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/identities"

"github.com/gofrs/uuid"
Expand All @@ -21,7 +23,7 @@ import (
)

func TestImportCmd(t *testing.T) {
c := identities.NewImportCmd()
c := identities.NewImportIdentitiesCmd(new(cobra.Command))
reg := setup(t, c)

t.Run("case=imports a new identity from file", func(t *testing.T) {
Expand Down
21 changes: 17 additions & 4 deletions cmd/identities/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ import (
"github.com/ory/kratos/cmd/cliclient"
)

func NewListCmd() *cobra.Command {
func NewListCmd(root *cobra.Command) *cobra.Command {
c := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "List resources",
}
c.AddCommand(NewListIdentitiesCmd(root))
cliclient.RegisterClientFlags(c.PersistentFlags())
cmdx.RegisterFormatFlags(c.PersistentFlags())
return c
}

func NewListIdentitiesCmd(root *cobra.Command) *cobra.Command {
return &cobra.Command{
Use: "list [<page> <per-page>]",
Short: "List identities",
Long: "List identities (paginated)",
Use: "identities [<page> <per-page>]",
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 {
Expand Down
4 changes: 3 additions & 1 deletion cmd/identities/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"testing"

"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/identities"

"github.com/ory/x/cmdx"
Expand All @@ -16,7 +18,7 @@ import (
)

func TestListCmd(t *testing.T) {
c := identities.NewListCmd()
c := identities.NewListIdentitiesCmd(new(cobra.Command))
reg := setup(t, c)
require.NoError(t, c.Flags().Set(cmdx.FlagQuiet, "true"))

Expand Down
21 changes: 0 additions & 21 deletions cmd/identities/patch.go

This file was deleted.

33 changes: 0 additions & 33 deletions cmd/identities/root.go

This file was deleted.

17 changes: 14 additions & 3 deletions cmd/identities/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@ import (
)

func NewValidateCmd() *cobra.Command {
var cmd = &cobra.Command{
Use: "validate",
Short: "Validate resources",
}
cmd.AddCommand(NewValidateIdentityCmd())
cliclient.RegisterClientFlags(cmd.PersistentFlags())
cmdx.RegisterFormatFlags(cmd.PersistentFlags())
return cmd

}

func NewValidateIdentityCmd() *cobra.Command {
var c = &cobra.Command{
Use: "validate <file.json [file-2.json [file-3.json] ...]>",
Use: "identity file.json [file-2.json] [file-3.json] [file-n.json]",
Short: "Validate local identity files",
Long: `This command allows validation of identity files.
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.
`,
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)

Expand Down
11 changes: 10 additions & 1 deletion cmd/jsonnet/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ import (
"github.com/ory/x/flagx"
)

func NewFormatCmd() *cobra.Command {
c := &cobra.Command{
Use: "format",
Short: "Helpers for formatting code",
}
c.AddCommand(NewJsonnetFormatCmd())
return c
}

func NewJsonnetFormatCmd() *cobra.Command {
c := &cobra.Command{
Use: "format path/to/files/*.jsonnet [more/files.jsonnet, [supports/**/{foo,bar}.jsonnet]]",
Use: "jsonnet path/to/files/*.jsonnet [more/files.jsonnet] [supports/**/{foo,bar}.jsonnet]",
Long: `Formats JSONNet files using the official JSONNet formatter.
Use -w or --write to write output back to files instead of stdout.
Expand Down
11 changes: 10 additions & 1 deletion cmd/jsonnet/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ import (
"github.com/ory/x/cmdx"
)

func NewLintCmd() *cobra.Command {
c := &cobra.Command{
Use: "lint",
Short: "Helpers for linting code",
}
c.AddCommand(NewJsonnetLintCmd())
return c
}

func NewJsonnetLintCmd() *cobra.Command {
return &cobra.Command{
Use: "lint path/to/files/*.jsonnet [more/files.jsonnet, [supports/**/{foo,bar}.jsonnet]]",
Use: "lint path/to/files/*.jsonnet [more/files.jsonnet] [supports/**/{foo,bar}.jsonnet]",
Long: `Lints JSONNet files using the official JSONNet linter and exits with a status code of 1 when issues are detected.
` + GlobHelp,
Expand Down
21 changes: 0 additions & 21 deletions cmd/jsonnet/root.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package jsonnet

import (
"github.com/spf13/cobra"
)

const GlobHelp = `Glob Syntax:
pattern:
Expand All @@ -28,20 +24,3 @@ const GlobHelp = `Glob Syntax:
pattern-list:
pattern { ',' pattern }
comma-separated (without spaces) patterns`

func NewJsonnetCmd() *cobra.Command {
c := &cobra.Command{
Use: "jsonnet",
Short: "Helpers for linting and formatting JSONNet code",
}

return c
}

func RegisterCommandRecursive(parent *cobra.Command) {
c := NewJsonnetCmd()
parent.AddCommand(c)

c.AddCommand(NewJsonnetFormatCmd())
c.AddCommand(NewJsonnetLintCmd())
}
Loading

0 comments on commit 73910a3

Please sign in to comment.