Skip to content

Commit a6a37a1

Browse files
author
chen.rui
committed
完成 增删改查,重命名已经当前。
1 parent 8385b0b commit a6a37a1

File tree

4 files changed

+109
-11
lines changed

4 files changed

+109
-11
lines changed

kf.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
apiVersion: v1
2-
clusters: []
3-
current-context: ""
2+
clusters:
3+
- addr: localhost:9092
4+
name: local3
5+
current-context: local3
46
kind: Config

local-kafka.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
apiVersion: v1
22
kind: Config
33
clusters:
4-
- name: local2
4+
- name: local
55
addr: localhost:9092

local-kafka2.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: v1
2+
kind: Config
3+
clusters:
4+
- name: local2
5+
addr: localhost:9092

main.go

+99-8
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import (
88
)
99

1010
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"`
1516
}
1617

1718
type cluster struct {
19+
ID string `json:"id,omitempty"`
1820
Name string `json:"name"`
1921
Addr string `json:"addr"`
2022
}
@@ -136,17 +138,65 @@ func main() {
136138
Use: "rename ",
137139
Short: "rename kafka cluster",
138140
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"},
139167
RunE: func(cmd *cobra.Command, args []string) error {
140168
config, err := parse2Struct(kfFile)
141169
name := cmd.Flags().Lookup("name").Value.String()
142-
renameClusterFromName(kfFile, name, config)
170+
switchClusterFromName(kfFile, name, config)
143171
return err
144172
},
145173
}
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+
}
146194

147195
clusterCmd.AddCommand(addCmd)
148196
clusterCmd.AddCommand(listCmd)
149197
clusterCmd.AddCommand(removeCmd)
198+
clusterCmd.AddCommand(switchCmd)
199+
clusterCmd.AddCommand(currentCmd)
150200
clusterCmd.AddCommand(renameCmd)
151201

152202
kfCmd.AddCommand(clusterCmd)
@@ -157,7 +207,48 @@ func main() {
157207

158208
}
159209

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+
}
161252

162253
}
163254

@@ -196,8 +287,8 @@ func removeClusterFromName(fileName, name string, config *Config) {
196287
}
197288

198289
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))
201292
}
202293

203294
func parse2Struct(fileName string) (*Config, error) {

0 commit comments

Comments
 (0)