forked from birdayz/kaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
275 lines (241 loc) · 7.23 KB
/
config.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"fmt"
"os"
"strings"
"regexp"
"github.com/Shopify/sarama"
"github.com/birdayz/kaf/pkg/config"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
var (
flagEhConnString string
flagBrokerVersion string
)
func init() {
configCmd.AddCommand(configImportCmd)
configCmd.AddCommand(configUseCmd)
configCmd.AddCommand(configLsCmd)
configCmd.AddCommand(configAddClusterCmd)
configCmd.AddCommand(configRemoveClusterCmd)
configCmd.AddCommand(configSelectCluster)
configCmd.AddCommand(configCurrentContext)
configCmd.AddCommand(configAddEventhub)
rootCmd.AddCommand(configCmd)
configLsCmd.Flags().BoolVar(&noHeaderFlag, "no-headers", false, "Hide table headers")
configAddEventhub.Flags().StringVar(&flagEhConnString, "eh-connstring", "", "EventHub ConnectionString")
configAddClusterCmd.Flags().StringVar(&flagBrokerVersion, "broker-version", "", fmt.Sprintf("Broker Version. Available Versions: %v", sarama.SupportedVersions))
}
var configCmd = &cobra.Command{
Use: "config",
Short: "Handle kaf configuration",
}
var configCurrentContext = &cobra.Command{
Use: "current-context",
Short: "Displays the current context",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(cfg.CurrentCluster)
},
}
func validConfigArgs(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
clusterList := make([]string, 0, len(cfg.Clusters))
for _, cluster := range cfg.Clusters {
clusterList = append(clusterList, cluster.Name)
}
return clusterList, cobra.ShellCompDirectiveNoFileComp
}
var configUseCmd = &cobra.Command{
Use: "use-cluster [NAME]",
Short: "Sets the current cluster in the configuration",
Args: cobra.ExactArgs(1),
ValidArgsFunction: validConfigArgs,
Run: func(cmd *cobra.Command, args []string) {
name := args[0]
if err := cfg.SetCurrentCluster(name); err != nil {
fmt.Printf("Cluster with name %v not found\n", name)
} else {
fmt.Printf("Switched to cluster \"%v\".\n", name)
}
},
}
var configLsCmd = &cobra.Command{
Use: "get-clusters",
Short: "Display clusters in the configuration file",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
if !noHeaderFlag {
fmt.Println("NAME")
}
for _, cluster := range cfg.Clusters {
fmt.Println(cluster.Name)
}
},
}
var configAddEventhub = &cobra.Command{
Use: "add-eventhub [NAME]",
Example: "esp config add-eventhub my-eventhub --eh-connstring 'Endpoint=sb://......AccessKey=....'",
Short: "Add Azure EventHub",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
name := args[0]
for _, cluster := range cfg.Clusters {
if cluster.Name == name {
errorExit("Could not add cluster: cluster with name '%v' exists already.", name)
}
}
// Parse hub name from ConnString
r, _ := regexp.Compile(`^Endpoint=sb://(.*)\.servicebus.*$`)
hubName := r.FindStringSubmatch(flagEhConnString)
if len(hubName) != 2 {
errorExit("Failed to determine EventHub name from Connection String. Check your ConnectionString")
}
cfg.Clusters = append(cfg.Clusters, &config.Cluster{
Name: name,
Brokers: []string{hubName[1] + ".servicebus.windows.net:9093"},
SchemaRegistryURL: schemaRegistryURL,
SASL: &config.SASL{
Mechanism: "PLAIN",
Username: "$ConnectionString",
Password: flagEhConnString,
},
SecurityProtocol: "SASL_SSL",
})
err := cfg.Write()
if err != nil {
errorExit("Unable to write config: %v\n", err)
}
fmt.Println("Added EventHub.")
},
}
var configSelectCluster = &cobra.Command{
Use: "select-cluster",
Short: "Interactively select a cluster",
Run: func(cmd *cobra.Command, args []string) {
var clusterNames []string
for _, cluster := range cfg.Clusters {
clusterNames = append(clusterNames, cluster.Name)
}
searcher := func(input string, index int) bool {
cluster := clusterNames[index]
name := strings.Replace(strings.ToLower(cluster), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
p := promptui.Select{
Label: "Select cluster",
Items: clusterNames,
Searcher: searcher,
Size: 10,
}
_, selected, err := p.Run()
if err != nil {
os.Exit(0)
}
// How to have selection on currently selected cluster?
// TODO copy pasta
if err := cfg.SetCurrentCluster(selected); err != nil {
fmt.Printf("Cluster with selected %v not found\n", selected)
}
},
}
var configAddClusterCmd = &cobra.Command{
Use: "add-cluster [NAME]",
Short: "Add cluster",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
name := args[0]
for _, cluster := range cfg.Clusters {
if cluster.Name == name {
errorExit("Could not add cluster: cluster with name '%v' exists already.", name)
}
}
cfg.Clusters = append(cfg.Clusters, &config.Cluster{
Name: name,
Brokers: brokersFlag,
SchemaRegistryURL: schemaRegistryURL,
Version: flagBrokerVersion,
})
err := cfg.Write()
if err != nil {
errorExit("Unable to write config: %v\n", err)
}
fmt.Println("Added cluster.")
},
}
var configRemoveClusterCmd = &cobra.Command{
Use: "remove-cluster [NAME]",
Short: "remove cluster",
Args: cobra.ExactArgs(1),
ValidArgsFunction: validConfigArgs,
Run: func(cmd *cobra.Command, args []string) {
name := args[0]
var pos = -1
for i, cluster := range cfg.Clusters {
if cluster.Name == name {
pos = i
break
}
}
if pos == -1 {
errorExit("Could not delete cluster: cluster with name '%v' not exists.", name)
}
cfg.Clusters = append(cfg.Clusters[:pos], cfg.Clusters[pos+1:]...)
err := cfg.Write()
if err != nil {
errorExit("Unable to write config: %v\n", err)
}
fmt.Println("Removed cluster.")
},
}
var configImportCmd = &cobra.Command{
Use: "import [ccloud]",
Short: "Import configurations into the $HOME/.kaf/config file",
Run: func(cmd *cobra.Command, args []string) {
if path, err := config.TryFindCcloudConfigFile(); err == nil {
fmt.Printf("Detected Confluent Cloud config in file %v\n", path)
if username, password, broker, err := config.ParseConfluentCloudConfig(path); err == nil {
newCluster := &config.Cluster{
Name: "ccloud",
Brokers: []string{broker},
SASL: &config.SASL{
Username: username,
Password: password,
Mechanism: "PLAIN",
},
SecurityProtocol: "SASL_SSL",
}
var found bool
for i, newCluster := range cfg.Clusters {
if newCluster.Name == "confluent cloud" {
found = true
cfg.Clusters[i] = newCluster
break
}
}
if !found {
fmt.Println("Wrote new entry to config file")
cfg.Clusters = append(cfg.Clusters, newCluster)
}
if cfg.CurrentCluster == "" {
cfg.CurrentCluster = newCluster.Name
}
err = cfg.Write()
if err != nil {
errorExit("Failed to write config: %w", err)
}
}
}
},
ValidArgs: []string{"ccloud"},
Args: func(cmd *cobra.Command, args []string) error {
if err := cobra.OnlyValidArgs(cmd, args); err != nil {
return err
}
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
return err
}
return nil
},
}