|
| 1 | +package cluster |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/spf13/cobra" |
| 5 | + "os" |
| 6 | + "sigs.k8s.io/yaml" |
| 7 | +) |
| 8 | + |
| 9 | +type Cluster struct { |
| 10 | + //ID string `json:"id,omitempty"` |
| 11 | + Name string `json:"name"` |
| 12 | + Addr string `json:"addr"` |
| 13 | +} |
| 14 | + |
| 15 | +func NewClusterCmd() *cobra.Command { |
| 16 | + clusterCmd := &cobra.Command{ |
| 17 | + Use: "cluster", |
| 18 | + Short: "kafka cluster", |
| 19 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 20 | + return nil |
| 21 | + }, |
| 22 | + } |
| 23 | + clusterCmd.AddCommand(NewAddCmd()) |
| 24 | + clusterCmd.AddCommand(NewListCmd()) |
| 25 | + clusterCmd.AddCommand(NewRemoveCmd()) |
| 26 | + clusterCmd.AddCommand(NewSwitchCmd()) |
| 27 | + clusterCmd.AddCommand(NewCurrentCmd()) |
| 28 | + clusterCmd.AddCommand(NewRenameCmd()) |
| 29 | + return clusterCmd |
| 30 | +} |
| 31 | + |
| 32 | +func switchClusterFromName(fileName string, name string, config *Config) { |
| 33 | + // 将文件序列成对象 |
| 34 | + file, err := os.ReadFile(fileName) |
| 35 | + if err != nil { |
| 36 | + panic(err) |
| 37 | + } |
| 38 | + err = yaml.Unmarshal(file, config) |
| 39 | + if err != nil { |
| 40 | + panic(err) |
| 41 | + } |
| 42 | + // 替换当前的上下文 |
| 43 | + config.CurrentContext = name |
| 44 | + // 重新写入yaml |
| 45 | + yamlData, err := yaml.Marshal(config) |
| 46 | + err = os.WriteFile(fileName, yamlData, 0644) |
| 47 | + if err != nil { |
| 48 | + panic(err) |
| 49 | + } |
| 50 | + |
| 51 | +} |
| 52 | + |
| 53 | +func renameClusterFromName(oldName, newName string, config *Config) { |
| 54 | + |
| 55 | + // 更新current-context |
| 56 | + if config.CurrentContext == oldName { |
| 57 | + config.CurrentContext = newName |
| 58 | + } |
| 59 | + |
| 60 | + // 更新名称 |
| 61 | + for _, cluster := range config.Clusters { |
| 62 | + if oldName == cluster.Name { |
| 63 | + cluster.Name = newName |
| 64 | + println(cluster.Name) |
| 65 | + } |
| 66 | + } |
| 67 | + // 回写 |
| 68 | + yamlData, err := yaml.Marshal(config) |
| 69 | + err = os.WriteFile(KfFile, yamlData, 0644) |
| 70 | + if err != nil { |
| 71 | + panic(err) |
| 72 | + } |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +func removeClusterFromName(fileName, name string, config *Config) { |
| 77 | + // 将文件序列成对象 |
| 78 | + file, err := os.ReadFile(fileName) |
| 79 | + if err != nil { |
| 80 | + panic(err) |
| 81 | + } |
| 82 | + err = yaml.Unmarshal(file, config) |
| 83 | + if err != nil { |
| 84 | + panic(err) |
| 85 | + } |
| 86 | + // 删除记录 |
| 87 | + for i, cluster := range config.Clusters { |
| 88 | + if cluster.Name == name { |
| 89 | + config.Clusters = append(config.Clusters[:i], config.Clusters[i+1:]...) |
| 90 | + } |
| 91 | + } |
| 92 | + // 重置当前的上下文 |
| 93 | + if name == config.CurrentContext { |
| 94 | + if len(config.Clusters) == 0 { |
| 95 | + config.CurrentContext = "" |
| 96 | + } else { |
| 97 | + config.CurrentContext = config.Clusters[0].Name |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // 重新写入yaml |
| 102 | + yamlData, err := yaml.Marshal(config) |
| 103 | + err = os.WriteFile(fileName, yamlData, 0644) |
| 104 | + if err != nil { |
| 105 | + panic(err) |
| 106 | + } |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +func parse2Struct(fileName string) (*Config, error) { |
| 111 | + config := &Config{} |
| 112 | + file, err := os.ReadFile(fileName) |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + err = yaml.Unmarshal(file, config) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + return config, nil |
| 121 | +} |
0 commit comments