-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathpreferences.go
191 lines (160 loc) · 4.94 KB
/
preferences.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package cmd
import (
"encoding/json"
"fmt"
"os"
"github.com/MakeNowJust/heredoc"
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1"
"github.com/raystack/salt/printer"
"github.com/spf13/cobra"
cli "github.com/spf13/cobra"
)
func PreferencesCommand(cliConfig *Config) *cobra.Command {
cmd := &cobra.Command{
Use: "preferences",
Aliases: []string{"p"},
Short: "Preferences management",
Long: "Preferences management commands.",
Example: heredoc.Doc(`
$ frontier preferences list
$ frontier preferences set
$ frontier preferences get
`),
}
cmd.AddCommand(preferencesListCommand(cliConfig))
cmd.AddCommand(preferencesSetCommand(cliConfig))
cmd.AddCommand(preferencesGetCommand(cliConfig))
bindFlagsFromClientConfig(cmd)
return cmd
}
func preferencesListCommand(cliConfig *Config) *cobra.Command {
var header string
cmd := &cobra.Command{
Use: "list",
Short: "list preferences",
Long: "List preferences prints the current preferences set in the db. If no preferences are set, it will print the default preferences.",
Args: cobra.NoArgs,
Example: heredoc.Doc(`
$ frontier preferences list
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cli.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
var reqBody frontierv1beta1.ListPreferencesRequest
adminClient, cancel, err := createAdminClient(cmd.Context(), cliConfig.Host)
if err != nil {
return err
}
defer cancel()
ctx := setCtxHeader(cmd.Context(), header)
res, err := adminClient.ListPreferences(ctx, &reqBody)
if err != nil {
return err
}
if len(res.GetPreferences()) == 0 {
spinner.Stop()
fmt.Println("No preferences set")
return nil
}
report := [][]string{}
report = append(report, []string{"Name", "Value", "ResourceType", "ResourceID"})
for _, preference := range res.GetPreferences() {
report = append(report, []string{preference.GetName(), preference.GetValue(), preference.GetResourceType(), preference.GetResourceId()})
}
spinner.Stop()
printer.Table(os.Stdout, report)
return nil
},
}
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
return cmd
}
func preferencesSetCommand(cliConfig *Config) *cobra.Command {
var header, name, value string
cmd := &cobra.Command{
Use: "set",
Short: "Set value for a preference trait",
Args: cobra.NoArgs,
Example: heredoc.Doc(`
$ frontier preferences set --name mail_link_subject --value Your Frontier login link
$ frontier preferences set -n disable_orgs_on_create -v true
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cobra.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
if name == "" || value == "" {
return fmt.Errorf("name and value are required")
}
var reqBody frontierv1beta1.CreatePreferencesRequest
reqBody.Preferences = append(reqBody.GetPreferences(), &frontierv1beta1.PreferenceRequestBody{
Name: name,
Value: value,
})
client, cancel, err := createAdminClient(cmd.Context(), cliConfig.Host)
if err != nil {
return err
}
defer cancel()
ctx := setCtxHeader(cmd.Context(), header)
res, err := client.CreatePreferences(ctx, &reqBody)
if err != nil {
return err
}
spinner.Stop()
fmt.Printf("Preference %s set successfully with value: %s \n", res.GetPreference()[0].GetName(), res.GetPreference()[0].GetValue())
return nil
},
}
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.Flags().StringVarP(&name, "name", "n", "", "Name of the preference")
cmd.Flags().StringVarP(&value, "value", "v", "", "Value of the preference")
cmd.MarkFlagRequired("header")
return cmd
}
func preferencesGetCommand(cliConfig *Config) *cobra.Command {
var header string
cmd := &cobra.Command{
Use: "get",
Short: "Get preference traits list",
Long: "Display the predefined preferences traits used by Frontier for settings at platform, org, group and project levels.",
Args: cobra.NoArgs,
Example: heredoc.Doc(`
$ frontier preferences get
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cli.Command, args []string) error {
spinner := printer.Spin("")
defer spinner.Stop()
var reqBody frontierv1beta1.DescribePreferencesRequest
client, cancel, err := createClient(cmd.Context(), cliConfig.Host)
if err != nil {
return err
}
defer cancel()
ctx := setCtxHeader(cmd.Context(), header)
res, err := client.DescribePreferences(ctx, &reqBody)
if err != nil {
return err
}
spinner.Stop()
fmt.Println(prettyPrint(res.GetTraits()))
return nil
},
}
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value>")
cmd.MarkFlagRequired("header")
return cmd
}
func prettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}