Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make cli commands consumable in Ory Cloud #926

Merged
merged 2 commits into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion cmd/cliclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/spf13/pflag"

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

Expand All @@ -18,7 +18,17 @@ const (
FlagEndpoint = "endpoint"
)

type ContextKey int

const (
ClientContextKey ContextKey = iota + 1
)

func NewClient(cmd *cobra.Command) *client.OryKratos {
if f, ok := cmd.Context().Value(ClientContextKey).(func(cmd *cobra.Command) *client.OryKratos); ok {
return f(cmd)
}

endpoint, err := cmd.Flags().GetString(FlagEndpoint)
cmdx.Must(err, "flag access error: %s", err)

Expand Down
2 changes: 1 addition & 1 deletion cmd/identities/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/ory/x/cmdx"

"github.com/ory/kratos/internal/httpclient/models"
"github.com/ory/kratos-client-go/models"
)

type (
Expand Down
8 changes: 2 additions & 6 deletions cmd/identities/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ package identities

import (
"fmt"
"github.com/ory/kratos-client-go/client/admin"
"time"

"github.com/ory/x/cmdx"

"github.com/ory/kratos/internal/clihelpers"

"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/kratos/internal/httpclient/client/admin"
)

var deleteCmd = &cobra.Command{
var DeleteCmd = &cobra.Command{
Use: "delete <id-0 [id-1 ...]>",
Short: "Delete identities by ID",
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.
Expand Down
8 changes: 4 additions & 4 deletions cmd/identities/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import (
)

func TestDeleteCmd(t *testing.T) {
reg := setup(t, deleteCmd)
reg := setup(t, DeleteCmd)

t.Run("case=deletes successfully", func(t *testing.T) {
// create identity to delete
i := identity.NewIdentity(config.DefaultIdentityTraitsSchemaID)
require.NoError(t, reg.Persister().CreateIdentity(context.Background(), i))

stdOut := execNoErr(t, deleteCmd, i.ID.String())
stdOut := execNoErr(t, DeleteCmd, i.ID.String())

// expect ID and no error
assert.Equal(t, i.ID.String()+"\n", stdOut)
Expand All @@ -36,7 +36,7 @@ func TestDeleteCmd(t *testing.T) {
t.Run("case=deletes three identities", func(t *testing.T) {
is, ids := makeIdentities(t, reg, 3)

stdOut := execNoErr(t, deleteCmd, ids...)
stdOut := execNoErr(t, DeleteCmd, ids...)

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

Expand All @@ -47,7 +47,7 @@ func TestDeleteCmd(t *testing.T) {
})

t.Run("case=fails with unknown ID", func(t *testing.T) {
stdErr := execErr(t, deleteCmd, x.NewUUID().String())
stdErr := execErr(t, DeleteCmd, x.NewUUID().String())

assert.Contains(t, stdErr, "[DELETE /identities/{id}][404] deleteIdentityNotFound", stdErr)
})
Expand Down
6 changes: 3 additions & 3 deletions cmd/identities/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package identities

import (
"fmt"
"github.com/ory/kratos-client-go/client/admin"
"time"

"github.com/ory/x/cmdx"

"github.com/ory/kratos/internal/clihelpers"
"github.com/ory/kratos/internal/httpclient/models"
"github.com/ory/kratos-client-go/models"

"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/kratos/internal/httpclient/client/admin"
)

var getCmd = &cobra.Command{
var GetCmd = &cobra.Command{
Use: "get <id-0 [id-1 ...]>",
Short: "Get one or more identities by ID",
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.
Expand Down
8 changes: 4 additions & 4 deletions cmd/identities/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
)

func TestGetCmd(t *testing.T) {
reg := setup(t, getCmd)
reg := setup(t, GetCmd)

t.Run("case=gets a single identity", func(t *testing.T) {
i := identity.NewIdentity(config.DefaultIdentityTraitsSchemaID)
require.NoError(t, reg.Persister().CreateIdentity(context.Background(), i))

stdOut := execNoErr(t, getCmd, i.ID.String())
stdOut := execNoErr(t, GetCmd, i.ID.String())

ij, err := json.Marshal(i)
require.NoError(t, err)
Expand All @@ -32,7 +32,7 @@ func TestGetCmd(t *testing.T) {
t.Run("case=gets three identities", func(t *testing.T) {
is, ids := makeIdentities(t, reg, 3)

stdOut := execNoErr(t, getCmd, ids...)
stdOut := execNoErr(t, GetCmd, ids...)

isj, err := json.Marshal(is)
require.NoError(t, err)
Expand All @@ -41,7 +41,7 @@ func TestGetCmd(t *testing.T) {
})

t.Run("case=fails with unknown ID", func(t *testing.T) {
stdErr := execErr(t, getCmd, x.NewUUID().String())
stdErr := execErr(t, GetCmd, x.NewUUID().String())

assert.Contains(t, stdErr, "status 404", stdErr)
})
Expand Down
8 changes: 4 additions & 4 deletions cmd/identities/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/kratos/internal/httpclient/client/admin"
"github.com/ory/kratos/internal/httpclient/models"
"github.com/ory/kratos-client-go/client/admin"
"github.com/ory/kratos-client-go/models"
)

// importCmd represents the import command
var importCmd = &cobra.Command{
// ImportCmd represents the import command
var ImportCmd = &cobra.Command{
Use: "import <file.json [file-2.json [file-3.json] ...]>",
Short: "Import identities from files or STD_IN",
Example: `$ kratos identities import file.json
Expand Down
14 changes: 7 additions & 7 deletions cmd/identities/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"github.com/tidwall/gjson"

"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/internal/httpclient/models"
"github.com/ory/kratos-client-go/models"
"github.com/ory/x/pointerx"
)

func TestImportCmd(t *testing.T) {
reg := setup(t, importCmd)
reg := setup(t, ImportCmd)

t.Run("case=imports a new identity from file", func(t *testing.T) {
i := models.CreateIdentity{
Expand All @@ -36,7 +36,7 @@ func TestImportCmd(t *testing.T) {
require.NoError(t, err)
require.NoError(t, f.Close())

stdOut := execNoErr(t, importCmd, f.Name())
stdOut := execNoErr(t, ImportCmd, f.Name())

id, err := uuid.FromString(gjson.Get(stdOut, "id").String())
require.NoError(t, err)
Expand All @@ -63,7 +63,7 @@ func TestImportCmd(t *testing.T) {
require.NoError(t, err)
require.NoError(t, f.Close())

stdOut := execNoErr(t, importCmd, f.Name())
stdOut := execNoErr(t, ImportCmd, f.Name())

id, err := uuid.FromString(gjson.Get(stdOut, "0.id").String())
require.NoError(t, err)
Expand All @@ -90,7 +90,7 @@ func TestImportCmd(t *testing.T) {
ij, err := json.Marshal(i)
require.NoError(t, err)

stdOut, stdErr, err := exec(importCmd, bytes.NewBuffer(ij))
stdOut, stdErr, err := exec(ImportCmd, bytes.NewBuffer(ij))
require.NoError(t, err, "%s %s", stdOut, stdErr)

id, err := uuid.FromString(gjson.Get(stdOut, "0.id").String())
Expand All @@ -112,7 +112,7 @@ func TestImportCmd(t *testing.T) {
ij, err := json.Marshal(i)
require.NoError(t, err)

stdOut, stdErr, err := exec(importCmd, bytes.NewBuffer(ij))
stdOut, stdErr, err := exec(ImportCmd, bytes.NewBuffer(ij))
require.NoError(t, err, "%s %s", stdOut, stdErr)

id, err := uuid.FromString(gjson.Get(stdOut, "id").String())
Expand All @@ -123,7 +123,7 @@ func TestImportCmd(t *testing.T) {

t.Run("case=fails to import invalid identity", func(t *testing.T) {
// validation is further tested with the validate command
stdOut, stdErr, err := exec(importCmd, bytes.NewBufferString("{}"))
stdOut, stdErr, err := exec(ImportCmd, bytes.NewBufferString("{}"))
assert.True(t, errors.Is(err, cmdx.ErrNoPrintButFail))
assert.Contains(t, stdErr, "STD_IN[0]: not valid")
assert.Len(t, stdOut, 0)
Expand Down
7 changes: 3 additions & 4 deletions cmd/identities/list.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package identities

import (
"context"
"fmt"
"strconv"

Expand All @@ -10,10 +9,10 @@ import (
"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/kratos/internal/httpclient/client/admin"
"github.com/ory/kratos-client-go/client/admin"
)

var listCmd = &cobra.Command{
var ListCmd = &cobra.Command{
Use: "list [<page> <per-page>]",
Short: "List identities",
Long: "List identities (paginated)",
Expand All @@ -29,7 +28,7 @@ var listCmd = &cobra.Command{
c := cliclient.NewClient(cmd)

params := &admin.ListIdentitiesParams{
Context: context.Background(),
Context: cmd.Context(),
}

if len(args) == 2 {
Expand Down
10 changes: 5 additions & 5 deletions cmd/identities/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
)

func TestListCmd(t *testing.T) {
reg := setup(t, listCmd)
require.NoError(t, listCmd.Flags().Set(cmdx.FlagQuiet, "true"))
reg := setup(t, ListCmd)
require.NoError(t, ListCmd.Flags().Set(cmdx.FlagQuiet, "true"))

var deleteIdentities = func(t *testing.T, is []*identity.Identity) {
for _, i := range is {
Expand All @@ -27,7 +27,7 @@ func TestListCmd(t *testing.T) {
is, ids := makeIdentities(t, reg, 5)
defer deleteIdentities(t, is)

stdOut := execNoErr(t, listCmd)
stdOut := execNoErr(t, ListCmd)

for _, i := range ids {
assert.Contains(t, stdOut, i)
Expand All @@ -38,8 +38,8 @@ func TestListCmd(t *testing.T) {
is, ids := makeIdentities(t, reg, 6)
defer deleteIdentities(t, is)

stdoutP1 := execNoErr(t, listCmd, "1", "3")
stdoutP2 := execNoErr(t, listCmd, "2", "3")
stdoutP1 := execNoErr(t, ListCmd, "1", "3")
stdoutP2 := execNoErr(t, ListCmd, "2", "3")

for _, id := range ids {
// exactly one of page 1 and 2 should contain the id
Expand Down
2 changes: 1 addition & 1 deletion cmd/identities/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
)

var patchCmd = &cobra.Command{
var PatchCmd = &cobra.Command{
Use: "patch <file.json [file-2.json [file-3.json] ...]>",
Short: "Patch identities by ID (not yet implemented)",
Args: cobra.MinimumNArgs(1),
Expand Down
14 changes: 7 additions & 7 deletions cmd/identities/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ var identitiesCmd = &cobra.Command{
func RegisterCommandRecursive(parent *cobra.Command) {
parent.AddCommand(identitiesCmd)

identitiesCmd.AddCommand(importCmd)
identitiesCmd.AddCommand(validateCmd)
identitiesCmd.AddCommand(listCmd)
identitiesCmd.AddCommand(getCmd)
identitiesCmd.AddCommand(deleteCmd)
identitiesCmd.AddCommand(patchCmd)
identitiesCmd.AddCommand(ImportCmd)
identitiesCmd.AddCommand(ValidateCmd)
identitiesCmd.AddCommand(ListCmd)
identitiesCmd.AddCommand(GetCmd)
identitiesCmd.AddCommand(DeleteCmd)
identitiesCmd.AddCommand(PatchCmd)
}

func init() {
func RegisterFlags() {
cliclient.RegisterClientFlags(identitiesCmd.PersistentFlags())
cmdx.RegisterFormatFlags(identitiesCmd.PersistentFlags())
}
4 changes: 2 additions & 2 deletions cmd/identities/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
"github.com/spf13/cobra"
"github.com/tidwall/gjson"

"github.com/ory/kratos/internal/httpclient/client/public"
"github.com/ory/kratos-client-go/client/public"

"github.com/ory/jsonschema/v3"
"github.com/ory/kratos/cmd/cliclient"
)

var validateCmd = &cobra.Command{
var ValidateCmd = &cobra.Command{
Use: "validate <file.json [file-2.json [file-3.json] ...]>",
Short: "Validate local identity files",
Long: `This command allows validation of identity files.
Expand Down
2 changes: 1 addition & 1 deletion cmd/identities/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/ory/kratos/internal/httpclient/client/public"
"github.com/ory/kratos-client-go/client/public"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
Expand Down
2 changes: 1 addition & 1 deletion cmd/remote/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/kratos/internal/httpclient/client/health"
"github.com/ory/kratos-client-go/client/health"
)

type statusState struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/remote/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/kratos/internal/httpclient/client/version"
"github.com/ory/kratos-client-go/client/version"
"github.com/ory/x/cmdx"
)

Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func Execute() {

func init() {
identities.RegisterCommandRecursive(RootCmd)
identities.RegisterFlags()

jsonnet.RegisterCommandRecursive(RootCmd)
serve.RegisterCommandRecursive(RootCmd)
migrate.RegisterCommandRecursive(RootCmd)
Expand Down
2 changes: 1 addition & 1 deletion courier/courier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"

"github.com/ory/dockertest"
"github.com/ory/dockertest/v3"

dhelper "github.com/ory/x/sqlcon/dockertest"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/ory/kratos-client-go/client/public"
"github.com/ory/kratos-client-go/client"

"github.com/ory/kratos/internal/httpclient/client/common"
"github.com/ory/kratos-client-go/client/common"
)

func main() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/ory/kratos-client-go/client/public"
"github.com/ory/kratos-client-go/client"

"github.com/ory/kratos/internal/httpclient/client/common"
"github.com/ory/kratos-client-go/client/common"
)

func main() {
Expand Down
Loading