-
-
Notifications
You must be signed in to change notification settings - Fork 930
/
import.go
75 lines (61 loc) · 1.87 KB
/
import.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
package identities
import (
"context"
"encoding/json"
"fmt"
"github.com/ory/x/cmdx"
"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"
)
// 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
$ cat file.json | kratos identities import`,
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".
WARNING: Importing credentials is not yet supported.`,
RunE: func(cmd *cobra.Command, args []string) error {
c := cliclient.NewClient(cmd)
imported := make([]*models.Identity, 0, len(args))
failed := make(map[string]error)
is, err := readIdentities(cmd, args)
if err != nil {
return err
}
for src, i := range is {
err = validateIdentity(cmd, src, i, c.Public.GetSchema)
if err != nil {
return err
}
var params models.CreateIdentity
err = json.Unmarshal([]byte(i), ¶ms)
if err != nil {
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "STD_IN: Could not parse identity")
return cmdx.FailSilently(cmd)
}
resp, err := c.Admin.CreateIdentity(&admin.CreateIdentityParams{
Body: ¶ms,
Context: context.Background(),
})
if err != nil {
failed[src] = err
} else {
imported = append(imported, resp.Payload)
}
}
if len(imported) == 1 {
cmdx.PrintRow(cmd, (*outputIdentity)(imported[0]))
} else {
cmdx.PrintCollection(cmd, &outputIdentityCollection{identities: imported})
}
cmdx.PrintErrors(cmd, failed)
if len(failed) != 0 {
return cmdx.FailSilently(cmd)
}
return nil
},
}