Skip to content

Commit

Permalink
refactor: moved clihelpers to ory/x (#756)
Browse files Browse the repository at this point in the history
Contributes to ory/hydra#2124.

Co-authored-by: aeneasr <3372410+aeneasr@users.noreply.github.com>
  • Loading branch information
zepatrik and aeneasr committed Oct 13, 2020
1 parent e53289c commit 6ccffa8
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 213 deletions.
19 changes: 10 additions & 9 deletions cmd/identities/definitions.go
Expand Up @@ -3,7 +3,8 @@ package identities
import (
"strings"

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

"github.com/ory/kratos/internal/httpclient/models"
)

Expand All @@ -21,10 +22,10 @@ func (_ *outputIdentity) Header() []string {
func (i *outputIdentity) Fields() []string {
data := [5]string{
string(i.ID),
clihelpers.None,
clihelpers.None,
clihelpers.None,
clihelpers.None,
cmdx.None,
cmdx.None,
cmdx.None,
cmdx.None,
}

addresses := make([]string, 0, len(i.VerifiableAddresses))
Expand Down Expand Up @@ -67,10 +68,10 @@ func (c *outputIdentityCollection) Table() [][]string {
for i, ident := range c.identities {
data := [5]string{
string(ident.ID),
clihelpers.None,
clihelpers.None,
clihelpers.None,
clihelpers.None,
cmdx.None,
cmdx.None,
cmdx.None,
cmdx.None,
}

if len(ident.VerifiableAddresses) != 0 && ident.VerifiableAddresses[0].Value != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/identities/delete.go
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"time"

"github.com/ory/x/cmdx"

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

"github.com/spf13/cobra"
Expand Down Expand Up @@ -49,7 +51,7 @@ var deleteCmd = &cobra.Command{
}

if len(errs) != 0 {
return clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}
return nil
},
Expand Down
10 changes: 6 additions & 4 deletions cmd/identities/get.go
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"time"

"github.com/ory/x/cmdx"

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

Expand Down Expand Up @@ -40,14 +42,14 @@ var getCmd = &cobra.Command{
}

if len(identities) == 1 {
clihelpers.PrintRow(cmd, (*outputIdentity)(identities[0]))
cmdx.PrintRow(cmd, (*outputIdentity)(identities[0]))
} else {
clihelpers.PrintCollection(cmd, &outputIdentityCollection{identities})
cmdx.PrintCollection(cmd, &outputIdentityCollection{identities})
}
clihelpers.PrintErrors(cmd, failed)
cmdx.PrintErrors(cmd, failed)

if len(failed) != 0 {
return clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}
return nil
},
Expand Down
13 changes: 7 additions & 6 deletions cmd/identities/helpers.go
Expand Up @@ -8,6 +8,8 @@ import (
"io/ioutil"
"testing"

"github.com/ory/x/cmdx"

"github.com/pkg/errors"
"github.com/tidwall/gjson"

Expand All @@ -20,7 +22,6 @@ import (
"github.com/ory/kratos/driver"
"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/internal"
"github.com/ory/kratos/internal/clihelpers"
"github.com/ory/kratos/internal/testhelpers"
"github.com/ory/viper"
)
Expand All @@ -43,7 +44,7 @@ func readIdentities(cmd *cobra.Command, args []string) (map[string]string, error
fc, err := ioutil.ReadAll(cmd.InOrStdin())
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "STD_IN: Could not read: %s\n", err)
return nil, clihelpers.FailSilently(cmd)
return nil, cmdx.FailSilently(cmd)
}
for i, id := range parseIdentities(fc) {
rawIdentities[fmt.Sprintf("STD_IN[%d]", i)] = id
Expand All @@ -54,7 +55,7 @@ func readIdentities(cmd *cobra.Command, args []string) (map[string]string, error
fc, err := ioutil.ReadFile(fn)
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%s: Could not open identity file: %s\n", fn, err)
return nil, clihelpers.FailSilently(cmd)
return nil, cmdx.FailSilently(cmd)
}
for i, id := range parseIdentities(fc) {
rawIdentities[fmt.Sprintf("%s[%d]", fn, i)] = id
Expand All @@ -69,9 +70,9 @@ func setup(t *testing.T, cmd *cobra.Command) driver.Registry {
viper.Set(configuration.ViperKeyDefaultIdentitySchemaURL, "file://./stubs/identity.schema.json")
// setup command
cliclient.RegisterClientFlags(cmd.Flags())
clihelpers.RegisterFormatFlags(cmd.Flags())
cmdx.RegisterFormatFlags(cmd.Flags())
require.NoError(t, cmd.Flags().Set(cliclient.FlagEndpoint, admin.URL))
require.NoError(t, cmd.Flags().Set(clihelpers.FlagFormat, string(clihelpers.FormatJSON)))
require.NoError(t, cmd.Flags().Set(cmdx.FlagFormat, string(cmdx.FormatJSON)))
return reg
}

Expand All @@ -98,7 +99,7 @@ func execNoErr(t *testing.T, cmd *cobra.Command, args ...string) string {

func execErr(t *testing.T, cmd *cobra.Command, args ...string) string {
stdOut, stdErr, err := exec(cmd, nil, args...)
require.True(t, errors.Is(err, clihelpers.NoPrintButFailError))
require.True(t, errors.Is(err, cmdx.ErrNoPrintButFail))
require.Len(t, stdOut, 0, stdErr)
return stdErr
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/identities/import.go
Expand Up @@ -5,9 +5,9 @@ import (
"encoding/json"
"fmt"

"github.com/spf13/cobra"
"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"
Expand Down Expand Up @@ -46,7 +46,7 @@ WARNING: Importing credentials is not yet supported.`,
err = json.Unmarshal([]byte(i), &params)
if err != nil {
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "STD_IN: Could not parse identity")
return clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}

resp, err := c.Admin.CreateIdentity(&admin.CreateIdentityParams{
Expand All @@ -60,14 +60,14 @@ WARNING: Importing credentials is not yet supported.`,
}
}
if len(imported) == 1 {
clihelpers.PrintRow(cmd, (*outputIdentity)(imported[0]))
cmdx.PrintRow(cmd, (*outputIdentity)(imported[0]))
} else {
clihelpers.PrintCollection(cmd, &outputIdentityCollection{identities: imported})
cmdx.PrintCollection(cmd, &outputIdentityCollection{identities: imported})
}
clihelpers.PrintErrors(cmd, failed)
cmdx.PrintErrors(cmd, failed)

if len(failed) != 0 {
return clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}

return nil
Expand Down
5 changes: 3 additions & 2 deletions cmd/identities/import_test.go
Expand Up @@ -8,13 +8,14 @@ import (
"io/ioutil"
"testing"

"github.com/ory/x/cmdx"

"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"

"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/internal/clihelpers"
"github.com/ory/kratos/internal/httpclient/models"
"github.com/ory/x/pointerx"
)
Expand Down Expand Up @@ -123,7 +124,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("{}"))
assert.True(t, errors.Is(err, clihelpers.NoPrintButFailError))
assert.True(t, errors.Is(err, cmdx.ErrNoPrintButFail))
assert.Contains(t, stdErr, "STD_IN[0]: not valid")
assert.Len(t, stdOut, 0)
})
Expand Down
10 changes: 5 additions & 5 deletions cmd/identities/list.go
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strconv"

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

"github.com/spf13/cobra"

Expand Down Expand Up @@ -36,25 +36,25 @@ var listCmd = &cobra.Command{
page, err := strconv.ParseInt(args[0], 0, 64)
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not parse page argument\"%s\": %s", args[0], err)
return clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}
params.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 clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}
params.PerPage = &perPage
}

resp, err := c.Admin.ListIdentities(params)
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Could not get the identities: %+v\n", err)
return clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}

clihelpers.PrintCollection(cmd, &outputIdentityCollection{
cmdx.PrintCollection(cmd, &outputIdentityCollection{
identities: resp.Payload,
})

Expand Down
5 changes: 3 additions & 2 deletions cmd/identities/list_test.go
Expand Up @@ -5,16 +5,17 @@ import (
"strings"
"testing"

"github.com/ory/x/cmdx"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

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

var deleteIdentities = func(t *testing.T, is []*identity.Identity) {
for _, i := range is {
Expand Down
4 changes: 2 additions & 2 deletions cmd/identities/root.go
Expand Up @@ -3,7 +3,7 @@ package identities
import (
"github.com/spf13/cobra"

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

"github.com/ory/kratos/cmd/cliclient"
)
Expand All @@ -27,5 +27,5 @@ func RegisterCommandRecursive(parent *cobra.Command) {

func init() {
cliclient.RegisterClientFlags(identitiesCmd.PersistentFlags())
clihelpers.RegisterFormatFlags(identitiesCmd.PersistentFlags())
cmdx.RegisterFormatFlags(identitiesCmd.PersistentFlags())
}
12 changes: 6 additions & 6 deletions cmd/identities/validate.go
Expand Up @@ -6,15 +6,15 @@ import (
"encoding/json"
"fmt"

"github.com/ory/x/cmdx"

"github.com/markbates/pkger"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/tidwall/gjson"

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

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

"github.com/ory/jsonschema/v3"
"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/x/viperx"
Expand Down Expand Up @@ -95,7 +95,7 @@ func validateIdentity(cmd *cobra.Command, src, i string, getRemoteSchema schemaG
sid := gjson.Get(i, "schema_id")
if !sid.Exists() {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), `%s: Expected key "schema_id" to be defined in identity file`, src)
return clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}

customSchema, ok := schemas[sid.String()]
Expand All @@ -104,7 +104,7 @@ func validateIdentity(cmd *cobra.Command, src, i string, getRemoteSchema schemaG
ts, err := getRemoteSchema(&public.GetSchemaParams{ID: sid.String(), Context: context.Background()})
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%s: Could not fetch schema with ID \"%s\": %s\n", src, sid.String(), err)
return clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}
sf, err := json.Marshal(ts.Payload)
if err != nil {
Expand All @@ -115,7 +115,7 @@ func validateIdentity(cmd *cobra.Command, src, i string, getRemoteSchema schemaG
customSchema, err = jsonschema.CompileString("identity_traits.schema.json", string(sf))
if err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%s: Could not compile the traits schema: %s\n", src, err)
return clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}
schemas[sid.String()] = customSchema
}
Expand All @@ -129,7 +129,7 @@ func validateIdentity(cmd *cobra.Command, src, i string, getRemoteSchema schemaG
}

if foundValidationErrors {
return clihelpers.FailSilently(cmd)
return cmdx.FailSilently(cmd)
}
return nil
}
5 changes: 3 additions & 2 deletions cmd/remote/root.go
Expand Up @@ -3,8 +3,9 @@ package remote
import (
"github.com/spf13/cobra"

"github.com/ory/x/cmdx"

"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/kratos/internal/clihelpers"
)

var remoteCmd = &cobra.Command{
Expand All @@ -21,5 +22,5 @@ func RegisterCommandRecursive(parent *cobra.Command) {

func init() {
cliclient.RegisterClientFlags(remoteCmd.PersistentFlags())
clihelpers.RegisterFormatFlags(remoteCmd.PersistentFlags())
cmdx.RegisterFormatFlags(remoteCmd.PersistentFlags())
}
5 changes: 3 additions & 2 deletions cmd/remote/status.go
Expand Up @@ -3,10 +3,11 @@ package remote
import (
"context"

"github.com/ory/x/cmdx"

"github.com/spf13/cobra"

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

Expand Down Expand Up @@ -44,7 +45,7 @@ var statusCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
c := cliclient.NewClient(cmd)
state := &statusState{}
defer clihelpers.PrintRow(cmd, state)
defer cmdx.PrintRow(cmd, state)

_, err := c.Health.IsInstanceAlive(&health.IsInstanceAliveParams{
Context: context.Background(),
Expand Down
3 changes: 1 addition & 2 deletions cmd/remote/version.go
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/kratos/internal/clihelpers"
"github.com/ory/kratos/internal/httpclient/client/version"
"github.com/ory/x/cmdx"
)
Expand Down Expand Up @@ -37,6 +36,6 @@ var versionCmd = &cobra.Command{
resp, err := c.Version.GetVersion(&version.GetVersionParams{Context: context.Background()})
cmdx.Must(err, "Could not get version: %s", err)

clihelpers.PrintRow(cmd, (*versionValue)(resp.Payload))
cmdx.PrintRow(cmd, (*versionValue)(resp.Payload))
},
}

0 comments on commit 6ccffa8

Please sign in to comment.