/
save.go
111 lines (89 loc) · 2.85 KB
/
save.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
101
102
103
104
105
106
107
108
109
110
111
package apbr
import (
"errors"
"fmt"
"github.com/apibrew/apibrew/pkg/apbr/flags"
"github.com/apibrew/apibrew/pkg/formats/executor"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func execSave(mode executor.Mode, cmd *cobra.Command, args []string) error {
parseRootFlags(cmd)
inputFilePathArr, err := cmd.Flags().GetStringArray("file")
if err != nil {
return fmt.Errorf("failed to get input file path: %w", err)
}
if len(inputFilePathArr) == 0 {
return errors.New("file must be provided")
}
doMigration, err := cmd.Flags().GetBool("migrate")
if err != nil {
return fmt.Errorf("failed to get migration flag: %w", err)
}
dataOnly, err := cmd.Flags().GetBool("data-only")
if err != nil {
return fmt.Errorf("failed to get data-only flag: %w", err)
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return fmt.Errorf("failed to get force flag: %w", err)
}
format, err := cmd.Flags().GetString("format")
if err != nil {
return fmt.Errorf("failed to get format flag: %w", err)
}
overrideConfig := new(flags.OverrideConfig)
err = overrideFlags.Parse(overrideConfig, cmd, args)
if err != nil {
return fmt.Errorf("failed to parse override flags: %w", err)
}
var typ = ""
if len(args) > 0 {
typ = args[0]
}
applier := executor.NewExecutor(mode, GetClient(), doMigration, dataOnly, force, typ, *overrideConfig)
for _, inputFilePath := range inputFilePathArr {
err = applier.ApplyWithPattern(cmd.Context(), inputFilePath, format)
if err != nil {
log.Error(err)
}
}
log.Info("Done")
return nil
}
var applyCmd = &cobra.Command{
Use: "apply",
Short: "apply - apply resources/records docs: https://apibrew.io/docs/cli#apply",
RunE: func(cmd *cobra.Command, args []string) error {
return execSave(executor.APPLY, cmd, args)
},
}
var createCmd = &cobra.Command{
Use: "create",
Short: "create - update resources/records docs: https://apibrew.io/docs/cli#create",
RunE: func(cmd *cobra.Command, args []string) error {
return execSave(executor.CREATE, cmd, args)
},
}
var updateCmd = &cobra.Command{
Use: "update",
Short: "update - update resources/records docs: https://apibrew.io/docs/cli#update",
RunE: func(cmd *cobra.Command, args []string) error {
return execSave(executor.UPDATE, cmd, args)
},
}
func initCmd(cmd *cobra.Command) {
cmd.PersistentFlags().StringArrayP("file", "f", nil, "path to file or directory to apply")
cmd.PersistentFlags().StringP("namespace", "n", "default", "Namespace")
cmd.PersistentFlags().BoolP("migrate", "m", true, "Migrate")
cmd.PersistentFlags().Bool("force", false, "Force")
cmd.PersistentFlags().Bool("data-only", false, "Data Only")
cmd.PersistentFlags().Bool("schema-only", false, "Schema Only")
cmd.PersistentFlags().String("format", "yaml", "[yaml, json]")
overrideFlags.Declare(cmd)
}
func init() {
initCmd(applyCmd)
initCmd(createCmd)
initCmd(updateCmd)
}