Skip to content

Commit 1c7b39a

Browse files
authored
add group rename CLI command (#992)
2 parents f80b706 + 409d60f commit 1c7b39a

2 files changed

Lines changed: 93 additions & 1 deletion

File tree

internal/cmd/group_rename.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}

internal/turso/groups.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func (d *GroupsClient) Update(group string, version, extensions string) error {
336336
url := d.URL(fmt.Sprintf("/%s/update", group))
337337
r, err := d.client.Post(url, body)
338338
if err != nil {
339-
return fmt.Errorf("failed to rotate database keys: %w", err)
339+
return fmt.Errorf("failed to update group: %w", err)
340340
}
341341
defer r.Body.Close()
342342

@@ -352,6 +352,32 @@ func (d *GroupsClient) Update(group string, version, extensions string) error {
352352
return nil
353353
}
354354

355+
func (d *GroupsClient) Rename(oldName, newName string) error {
356+
type Body struct{ Name string }
357+
body, err := marshal(Body{Name: newName})
358+
if err != nil {
359+
return fmt.Errorf("could not serialize request body: %w", err)
360+
}
361+
362+
url := d.URL(fmt.Sprintf("/%s/rename", oldName))
363+
r, err := d.client.Post(url, body)
364+
if err != nil {
365+
return fmt.Errorf("failed to rename group: %w", err)
366+
}
367+
defer r.Body.Close()
368+
369+
org := d.client.Org
370+
if isNotMemberErr(r.StatusCode, org) {
371+
return notMemberErr(org)
372+
}
373+
374+
if r.StatusCode != http.StatusOK {
375+
return fmt.Errorf("failed to rename group: %w", parseResponseError(r))
376+
}
377+
378+
return nil
379+
}
380+
355381
func (d *GroupsClient) Transfer(group string, to string) error {
356382
type Body struct {
357383
Organization string `json:"organization"`

0 commit comments

Comments
 (0)