@@ -8,13 +8,15 @@ import (
8
8
)
9
9
10
10
type Config struct {
11
- ApiVersion string `json:"apiVersion"`
12
- Kind string `json:"kind"`
13
- Clusters []cluster `json:"clusters"`
14
- CurrentContext string `json:"current-context"`
11
+ ApiVersion string `json:"apiVersion"`
12
+ Kind string `json:"kind"`
13
+ // 如果是值类型,无法修改cluster中的值,如果需要修改,需要定义成指针类型
14
+ Clusters []* cluster `json:"clusters"`
15
+ CurrentContext string `json:"current-context"`
15
16
}
16
17
17
18
type cluster struct {
19
+ ID string `json:"id,omitempty"`
18
20
Name string `json:"name"`
19
21
Addr string `json:"addr"`
20
22
}
@@ -136,17 +138,65 @@ func main() {
136
138
Use : "rename " ,
137
139
Short : "rename kafka cluster" ,
138
140
Aliases : []string {"re" },
141
+ RunE : func (cmd * cobra.Command , args []string ) error {
142
+ config , err := parse2Struct (kfFile )
143
+ oldName := cmd .Flags ().Lookup ("oldName" ).Value .String ()
144
+ newName := cmd .Flags ().Lookup ("newName" ).Value .String ()
145
+ renameClusterFromName (oldName , newName , config )
146
+ return err
147
+ },
148
+ }
149
+ var oldName string
150
+ var newName string
151
+ renameCmd .Flags ().StringVarP (& oldName , "oldName" , "o" , "" , "switch cluster oldName" )
152
+ err = renameCmd .MarkFlagRequired ("oldName" )
153
+ if err != nil {
154
+ panic (err )
155
+ }
156
+ renameCmd .Flags ().StringVarP (& newName , "newName" , "n" , "" , "switch cluster newName" )
157
+ err = renameCmd .MarkFlagRequired ("newName" )
158
+ if err != nil {
159
+ panic (err )
160
+ }
161
+
162
+ // switch
163
+ switchCmd := & cobra.Command {
164
+ Use : "switch " ,
165
+ Short : "switch kafka cluster" ,
166
+ Aliases : []string {"s" },
139
167
RunE : func (cmd * cobra.Command , args []string ) error {
140
168
config , err := parse2Struct (kfFile )
141
169
name := cmd .Flags ().Lookup ("name" ).Value .String ()
142
- renameClusterFromName (kfFile , name , config )
170
+ switchClusterFromName (kfFile , name , config )
143
171
return err
144
172
},
145
173
}
174
+ currentCmd := & cobra.Command {
175
+ Use : "current " ,
176
+ Short : "current kafka cluster" ,
177
+ Aliases : []string {"c" },
178
+ RunE : func (cmd * cobra.Command , args []string ) error {
179
+ config , err := parse2Struct (kfFile )
180
+ for _ , currentCluster := range config .Clusters {
181
+ if currentCluster .Name == config .CurrentContext {
182
+ printCurrentClusterInfo (* currentCluster )
183
+ }
184
+ }
185
+ return err
186
+ },
187
+ }
188
+ var switchClusterName string
189
+ switchCmd .Flags ().StringVarP (& switchClusterName , "name" , "n" , "" , "switch cluster name" )
190
+ err = switchCmd .MarkFlagRequired ("name" )
191
+ if err != nil {
192
+ panic (err )
193
+ }
146
194
147
195
clusterCmd .AddCommand (addCmd )
148
196
clusterCmd .AddCommand (listCmd )
149
197
clusterCmd .AddCommand (removeCmd )
198
+ clusterCmd .AddCommand (switchCmd )
199
+ clusterCmd .AddCommand (currentCmd )
150
200
clusterCmd .AddCommand (renameCmd )
151
201
152
202
kfCmd .AddCommand (clusterCmd )
@@ -157,7 +207,48 @@ func main() {
157
207
158
208
}
159
209
160
- func renameClusterFromName (file string , name string , config * Config ) {
210
+ func switchClusterFromName (fileName string , name string , config * Config ) {
211
+ // 将文件序列成对象
212
+ file , err := os .ReadFile (fileName )
213
+ if err != nil {
214
+ panic (err )
215
+ }
216
+ err = yaml .Unmarshal (file , config )
217
+ if err != nil {
218
+ panic (err )
219
+ }
220
+ // 替换当前的上下文
221
+ config .CurrentContext = name
222
+ // 重新写入yaml
223
+ yamlData , err := yaml .Marshal (config )
224
+ err = os .WriteFile (fileName , yamlData , 0644 )
225
+ if err != nil {
226
+ panic (err )
227
+ }
228
+
229
+ }
230
+
231
+ func renameClusterFromName (oldName , newName string , config * Config ) {
232
+
233
+ // 更新current-context
234
+ if config .CurrentContext == oldName {
235
+ config .CurrentContext = newName
236
+ }
237
+
238
+ // 更新名称
239
+ for _ , cluster := range config .Clusters {
240
+ if oldName == cluster .Name {
241
+ cluster .Name = newName
242
+ println (cluster .Name )
243
+ }
244
+ }
245
+ // 回写
246
+ yamlData , err := yaml .Marshal (config )
247
+ fmt .Println (string (yamlData ))
248
+ err = os .WriteFile (kfFile , yamlData , 0644 )
249
+ if err != nil {
250
+ panic (err )
251
+ }
161
252
162
253
}
163
254
@@ -196,8 +287,8 @@ func removeClusterFromName(fileName, name string, config *Config) {
196
287
}
197
288
198
289
func printCurrentClusterInfo (currentCluster cluster ) {
199
- println ( currentCluster .Name )
200
- println ( currentCluster .Addr )
290
+ fmt . Println ( fmt . Sprintf ( "name:%s" , currentCluster .Name ) )
291
+ fmt . Println ( fmt . Sprintf ( "addr:%s" , currentCluster .Addr ) )
201
292
}
202
293
203
294
func parse2Struct (fileName string ) (* Config , error ) {
0 commit comments