-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsettings_apply.go
More file actions
100 lines (87 loc) · 3.33 KB
/
settings_apply.go
File metadata and controls
100 lines (87 loc) · 3.33 KB
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
package settings
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
"github.com/stackvista/stackstate-cli/internal/printer"
"github.com/stackvista/stackstate-cli/pkg/pflags"
)
var (
UnlockedStrategyChoices = []string{"fail", "skip", "overwrite"}
LockedStrategyChoices = []string{"fail", "skip", "overwrite"}
)
type ApplyArgs struct {
Filepath string
Namespace string
UnlockedStrategy string
LockedStrategy string
Timeout int64
}
func SettingsApplyCommand(cli *di.Deps) *cobra.Command {
args := &ApplyArgs{}
cmd := &cobra.Command{
Use: "apply",
Short: "Import settings from an STY or STJ file",
Long: "Import settings from an STY (SUSE Observability YAML) or STJ (SUSE Observability JSON) file. Can import to a specific namespace with conflict resolution strategies.",
Example: `# apply settings from file
sts settings apply --file my-settings.sty
# apply to namespace, skip conflicts with existing unlocked settings
sts settings apply --file my-settings.sty --namespace my-namespace --unlocked-strategy skip`,
RunE: cli.CmdRunEWithApi(RunSettingsApplyCommand(args)),
}
common.AddRequiredFileFlagVar(cmd, &args.Filepath, "Path to a .sty or .stj file with the settings to import")
cmd.Flags().StringVar(&args.Namespace, NamespaceFlag, "", "Name of the namespace to overwrite"+
" - WARNING this will overwrite the entire namespace")
pflags.EnumVar(cmd.Flags(), &args.UnlockedStrategy,
UnlockedStrategyFlag,
"",
UnlockedStrategyChoices,
"Strategy to use when encountering unlocked settings when applying settings to a namespace"+
fmt.Sprintf(" (must be { %s })", strings.Join(UnlockedStrategyChoices, " | ")))
pflags.EnumVar(cmd.Flags(), &args.LockedStrategy,
LockedStrategyFlag,
"",
LockedStrategyChoices,
"Strategy to use when encountering locked settings"+
fmt.Sprintf(" (must be { %s })", strings.Join(LockedStrategyChoices, " | ")))
cmd.Flags().Int64VarP(&args.Timeout, TimeoutFlag, TimeoutFlagShort, 0, TimeoutUsage)
return cmd
}
func RunSettingsApplyCommand(args *ApplyArgs) di.CmdWithApiFn {
return func(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError {
fileBytes, err := os.ReadFile(args.Filepath)
if err != nil {
return common.NewReadFileError(err, args.Filepath)
}
nodes, resp, err := doImport(cli.Context, api, string(fileBytes), args.Namespace, args.UnlockedStrategy, args.LockedStrategy, args.Timeout)
if err != nil {
return common.NewResponseError(err, resp)
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"applied-settings": nodes,
})
} else {
if len(nodes) == 0 {
cli.Printer.PrintWarn("Nothing was imported.")
return nil
}
tableData := make([][]interface{}, 0)
for _, node := range nodes {
tableData = append(tableData, []interface{}{node["_type"], node["id"], node["identifier"], node["name"]})
}
cli.Printer.Success(fmt.Sprintf("Applied <bold>%d</> setting node(s).\n", len(nodes)))
if len(nodes) > 0 {
cli.Printer.Table(printer.TableData{
Header: []string{"Type", "Id", "Identifier", "Name"},
Data: tableData,
})
}
}
return nil
}
}