-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathconfig.go
More file actions
204 lines (179 loc) · 5.13 KB
/
config.go
File metadata and controls
204 lines (179 loc) · 5.13 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
package config
import (
"errors"
"fmt"
"net/url"
"text/tabwriter"
survey "github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/cheynewallace/tabby"
"github.com/spf13/cobra"
cfg "github.com/GetStream/stream-cli/pkg/config"
)
func NewRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Manage app configurations",
}
cmd.AddCommand(newAppCmd(), removeAppCmd(), listAppsCmd(), setAppDefaultCmd())
return cmd
}
func newAppCmd() *cobra.Command {
return &cobra.Command{
Use: "new",
Short: "Add a new application",
Long: "Add a new application which can be used for further operations",
Example: heredoc.Doc(`
# Add a new application to the CLI
$ stream-cli config new
? What is the name of your app? (eg. prod, staging, testing) testing
? What is your access key? abcd1234efgh456
? What is your access secret key? ***********************************
? (optional) Which base URL do you want to use for Chat? https://chat.stream-io-api.com
Application successfully added. 🚀
`),
RunE: func(cmd *cobra.Command, args []string) error {
return runQuestionnaire(cmd)
},
}
}
func removeAppCmd() *cobra.Command {
return &cobra.Command{
Use: "remove [app-name-1] [app-name-2] [app-name-n]",
Short: "Remove one or more application.",
Long: "Remove one or more application from the configuration file. This operation is irrevocable.",
Example: heredoc.Doc(`
# Remove a single application from the CLI
$ stream-cli config remove staging
# Remove multiple applications from the CLI
$ stream-cli config remove staging testing
`),
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
config := cfg.GetConfig(cmd)
for _, appName := range args {
if err := config.Remove(appName); err != nil {
return err
}
cmd.Printf("[%s] application successfully removed.\n", appName)
}
return nil
},
}
}
func listAppsCmd() *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List all applications",
Long: "List all applications which are configured in the configuration file",
Example: heredoc.Doc(`
# List all applications
$ stream-cli config list
`),
RunE: func(cmd *cobra.Command, args []string) error {
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
t := tabby.NewCustom(w)
t.AddHeader("", "Name", "Access Key", "Secret Key", "URL")
config := cfg.GetConfig(cmd)
for _, app := range config.Apps {
def := ""
if app.Name == config.Default {
def = "(default)"
}
secret := fmt.Sprintf("**************%v", app.AccessSecretKey[len(app.AccessSecretKey)-4:])
t.AddLine(def, app.Name, app.AccessKey, secret, app.ChatURL)
}
t.Print()
return nil
},
}
}
func setAppDefaultCmd() *cobra.Command {
return &cobra.Command{
Use: "default [app-name]",
Short: "Set an application as the default",
Long: heredoc.Doc(`
Set an application as the default which will be used
for all further operations unless specified otherwise.
`),
Example: heredoc.Doc(`
# Set an application as the default
$ stream-cli config default staging
# All underlying operations will use it if not specified otherwise
$ stream-cli chat get-app
# Prints the settings of staging app
# Specifying other apps during an operation
$ stream-cli chat get-app --app prod
# Prints the settings of prod app
`),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
config := cfg.GetConfig(cmd)
return config.SetDefault(args[0])
},
}
}
func runQuestionnaire(cmd *cobra.Command) error {
var newAppConfig cfg.App
err := survey.Ask(questions(), &newAppConfig)
if err != nil {
return err
}
config := cfg.GetConfig(cmd)
err = config.Add(newAppConfig)
if err != nil {
return err
}
cmd.Println("Application successfully added. 🚀")
return nil
}
func questions() []*survey.Question {
return []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{Message: "What is the name of your app? (eg. prod, staging, testing)"},
Validate: survey.Required,
},
{
Name: "accessKey",
Prompt: &survey.Input{Message: "What is your access key?"},
Validate: survey.ComposeValidators(
survey.Required,
survey.MinLength(10),
survey.MaxLength(30)),
},
{
Name: "accessSecretKey",
Prompt: &survey.Password{Message: "What is your access secret key?"},
Validate: survey.ComposeValidators(
survey.Required,
survey.MinLength(50),
survey.MaxLength(75)),
},
{
Name: "ChatURL",
Prompt: &survey.Input{
Message: "(optional) Which base URL do you want to use for Chat?",
Default: cfg.DefaultChatEdgeURL,
},
Validate: func(ans interface{}) error {
u, ok := ans.(string)
if !ok {
return errors.New("invalid url")
}
_, err := url.ParseRequestURI(u)
if err != nil {
return errors.New("invalid url format make sure it matches <scheme>://<host>")
}
return nil
},
Transform: func(ans interface{}) interface{} {
s, ok := ans.(string)
if !ok || s == "" {
return cfg.DefaultChatEdgeURL
}
return s
},
},
}
}