|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 dingodb.com Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package cluster |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/dingodb/dingoadm/cli/cli" |
| 21 | + "github.com/dingodb/dingoadm/internal/errno" |
| 22 | + tui "github.com/dingodb/dingoadm/internal/tui/common" |
| 23 | + cliutil "github.com/dingodb/dingoadm/internal/utils" |
| 24 | + log "github.com/dingodb/dingoadm/pkg/log/glg" |
| 25 | + "github.com/spf13/cobra" |
| 26 | +) |
| 27 | + |
| 28 | +type renameOptions struct { |
| 29 | + clusterOldName string |
| 30 | + clusterNewName string |
| 31 | + force bool |
| 32 | +} |
| 33 | + |
| 34 | +func NewRenameCommand(dingoadm *cli.DingoAdm) *cobra.Command { |
| 35 | + var options renameOptions |
| 36 | + |
| 37 | + cmd := &cobra.Command{ |
| 38 | + Use: "rename CLUSTER [OPTIONS]", |
| 39 | + Short: "Rename cluster", |
| 40 | + Args: cliutil.ExactArgs(2), |
| 41 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 42 | + options.clusterOldName = args[0] |
| 43 | + options.clusterNewName = args[1] |
| 44 | + |
| 45 | + return runRename(dingoadm, options) |
| 46 | + }, |
| 47 | + DisableFlagsInUseLine: true, |
| 48 | + } |
| 49 | + |
| 50 | + flags := cmd.Flags() |
| 51 | + flags.BoolVarP(&options.force, "force", "f", false, "Remove cluster by force") |
| 52 | + |
| 53 | + return cmd |
| 54 | +} |
| 55 | + |
| 56 | +func runRename(dingoadm *cli.DingoAdm, options renameOptions) error { |
| 57 | + // 1) get cluster by name |
| 58 | + storage := dingoadm.Storage() |
| 59 | + clusterOldName := options.clusterOldName |
| 60 | + clusterNewName := options.clusterNewName |
| 61 | + clusters, err := storage.GetClusters(clusterOldName) // Get all clusters |
| 62 | + if err != nil { |
| 63 | + log.Error("Get cluster failed", |
| 64 | + log.Field("error", err)) |
| 65 | + return errno.ERR_GET_ALL_CLUSTERS_FAILED.E(err) |
| 66 | + } else if len(clusters) == 0 { |
| 67 | + return errno.ERR_CLUSTER_NOT_FOUND. |
| 68 | + F("cluster name: %s", clusterOldName) |
| 69 | + } |
| 70 | + |
| 71 | + // rename cluster name |
| 72 | + if options.force { |
| 73 | + dingoadm.WriteOut(tui.PromptRenameCluster(clusterOldName, clusterNewName)) |
| 74 | + } else { |
| 75 | + if !tui.ConfirmYes(tui.PromptRenameCluster(clusterOldName, clusterNewName)) { |
| 76 | + dingoadm.WriteOut(tui.PromptCancelOpetation("rename cluster")) |
| 77 | + return errno.ERR_CANCEL_OPERATION |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if err := dingoadm.Storage().RenameClusterName(clusterOldName, clusterNewName); err != nil { |
| 82 | + return errno.ERR_RENAME_CLUSTER_FAILED.E(err) |
| 83 | + } |
| 84 | + |
| 85 | + // 3) print success prompt |
| 86 | + dingoadm.WriteOutln("Rename cluster '%s' to '%s'", clusterOldName, clusterNewName) |
| 87 | + return nil |
| 88 | +} |
0 commit comments