|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + "github.com/tursodatabase/turso-cli/internal" |
| 8 | + "github.com/tursodatabase/turso-cli/internal/flags" |
| 9 | + "github.com/tursodatabase/turso-cli/internal/prompt" |
| 10 | + "github.com/tursodatabase/turso-cli/internal/turso" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + groupCmd.AddCommand(groupRenameCmd) |
| 15 | + flags.AddYes(groupRenameCmd, "Confirms the update") |
| 16 | +} |
| 17 | + |
| 18 | +var groupRenameCmd = &cobra.Command{ |
| 19 | + Use: "rename <old-group-name> <new-group-name>", |
| 20 | + Short: "Renames the group", |
| 21 | + Args: cobra.ExactArgs(2), |
| 22 | + ValidArgsFunction: groupArg, |
| 23 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 24 | + cmd.SilenceUsage = true |
| 25 | + client, err := authedTursoClient() |
| 26 | + if err != nil { |
| 27 | + return err |
| 28 | + } |
| 29 | + oldName := args[0] |
| 30 | + newName := args[1] |
| 31 | + |
| 32 | + if _, err := getGroup(client, oldName); err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + |
| 36 | + if yesFlag { |
| 37 | + return renameGroup(client, oldName, newName) |
| 38 | + } |
| 39 | + |
| 40 | + ok, err := promptConfirmation(fmt.Sprintf("Are you sure you want to rename group %s to %s?", internal.Emph(oldName), internal.Emph(newName))) |
| 41 | + if err != nil { |
| 42 | + return fmt.Errorf("could not get prompt confirmed by user: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + if !ok { |
| 46 | + fmt.Println("Group rename skipped by the user.") |
| 47 | + return nil |
| 48 | + } |
| 49 | + |
| 50 | + return renameGroup(client, oldName, newName) |
| 51 | + }, |
| 52 | +} |
| 53 | + |
| 54 | +func renameGroup(client *turso.Client, oldName, newName string) error { |
| 55 | + msg := fmt.Sprintf("Renaming group %s to %s", internal.Emph(oldName), internal.Emph(newName)) |
| 56 | + s := prompt.Spinner(msg) |
| 57 | + defer s.Stop() |
| 58 | + |
| 59 | + if err := client.Groups.Rename(oldName, newName); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + s.Stop() |
| 64 | + fmt.Printf("✔ Success! Group %s was renamed to %s successfully\n", internal.Emph(oldName), internal.Emph(newName)) |
| 65 | + return nil |
| 66 | +} |
0 commit comments