-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
cli_import.go
40 lines (36 loc) · 898 Bytes
/
cli_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
package cmd
import (
"encoding/json"
"os"
"beryju.io/gravity/api"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var importCmd = &cobra.Command{
Use: "import [file [file]]",
Args: cobra.MinimumNArgs(1),
Short: "Import JSON file created with `export` into database",
Run: func(cmd *cobra.Command, args []string) {
for _, path := range args {
cont, err := os.ReadFile(path)
if err != nil {
logger.Error("failed to read import", zap.Error(err))
continue
}
var entries api.ApiAPIImportInput
err = json.Unmarshal(cont, &entries)
if err != nil {
logger.Error("failed to unmarshal", zap.Error(err))
continue
}
_, err = apiClient.RolesApiApi.ApiImport(cmd.Context()).ApiAPIImportInput(entries).Execute()
if err != nil {
logger.Error("failed to import", zap.Error(err))
continue
}
}
},
}
func init() {
cliCmd.AddCommand(importCmd)
}