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

chore: moved clihelpers to ory/x #756

Merged
merged 5 commits into from
Oct 13, 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
19 changes: 10 additions & 9 deletions cmd/identities/definitions.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ kratos identities delete $(kratos identities list --format json | jq -r 'map(sel
}

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
Original file line number Diff line number Diff line change
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 @@ kratos identities get $(kratos identities list --format json | jq -r 'map(select
}

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -48,7 +48,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 @@ -62,14 +62,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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))
},
}
Loading