-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
cli_export.go
41 lines (37 loc) · 859 Bytes
/
cli_export.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
package cmd
import (
"encoding/json"
"os"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var exportCmd = &cobra.Command{
Use: "export [file]",
Short: "Output entire database into JSON file",
Run: func(cmd *cobra.Command, args []string) {
exp, _, err := apiClient.RolesApiApi.ApiExport(cmd.Context()).Execute()
if err != nil {
logger.Error("failed to export", zap.Error(err))
return
}
raw, err := json.Marshal(exp)
if err != nil {
logger.Error("failed to json marshal", zap.Error(err))
return
}
if len(args) > 0 {
err = os.WriteFile(args[0], raw, 0o644)
if err != nil {
logger.Error("failed to write export", zap.Error(err))
}
} else {
_, err = os.Stdout.Write(raw)
if err != nil {
logger.Error("failed to write export", zap.Error(err))
}
}
},
}
func init() {
cliCmd.AddCommand(exportCmd)
}