-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
get.go
100 lines (81 loc) · 2.71 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package identities
import (
"fmt"
kratos "github.com/ory/kratos/internal/httpclient"
"github.com/ory/kratos/x"
"github.com/ory/x/cmdx"
"github.com/ory/x/stringsx"
"github.com/ory/kratos/internal/clihelpers"
"github.com/spf13/cobra"
"github.com/ory/kratos/cmd/cliclient"
)
const (
FlagIncludeCreds = "include-credentials"
)
func NewGetCmd() *cobra.Command {
var cmd = &cobra.Command{
Use: "get",
Short: "Get resources",
}
cmd.AddCommand(NewGetIdentityCmd())
cliclient.RegisterClientFlags(cmd.PersistentFlags())
cmdx.RegisterFormatFlags(cmd.PersistentFlags())
return cmd
}
func NewGetIdentityCmd() *cobra.Command {
var (
includeCreds []string
)
cmd := &cobra.Command{
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:
{{ .CommandPath }} $({{ .Root.Name }} ls identities --format json | jq -r 'map(select(.recovery_addresses[].value | endswith("@ory.sh"))) | .[].id')`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := cliclient.NewClient(cmd)
if err != nil {
return err
}
// we check includeCreds argument is valid
for _, opt := range includeCreds {
e := stringsx.SwitchExact(opt)
if !e.AddCase("oidc") {
cmd.PrintErrln(`You have to put a valid value of credentials type to be included, try --help for details.`)
return cmdx.FailSilently(cmd)
}
}
identities := make([]kratos.Identity, 0, len(args))
failed := make(map[string]error)
for _, id := range args {
identity, _, err := c.IdentityApi.
GetIdentity(cmd.Context(), id).
IncludeCredential(includeCreds).
Execute()
if x.SDKError(err) != nil {
failed[id] = cmdx.PrintOpenAPIError(cmd, err)
continue
}
identities = append(identities, *identity)
}
if len(identities) == 1 {
cmdx.PrintRow(cmd, (*outputIdentity)(&identities[0]))
} else if len(identities) > 1 {
cmdx.PrintTable(cmd, &outputIdentityCollection{identities})
}
cmdx.PrintErrors(cmd, failed)
if len(failed) != 0 {
return cmdx.FailSilently(cmd)
}
return nil
},
}
flags := cmd.Flags()
// include credential flag to add third party tokens in returned data
flags.StringArrayVarP(&includeCreds, FlagIncludeCreds, "i", []string{}, `Include third party tokens (only "oidc" supported) `)
return cmd
}