/
group_update.go
87 lines (70 loc) · 2.5 KB
/
group_update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/tursodatabase/turso-cli/internal"
"github.com/tursodatabase/turso-cli/internal/flags"
"github.com/tursodatabase/turso-cli/internal/prompt"
"github.com/tursodatabase/turso-cli/internal/turso"
)
func init() {
groupCmd.AddCommand(groupUpdateCmd)
flags.AddYes(groupUpdateCmd, "Confirms the update")
flags.AddVersion(groupUpdateCmd, "Version to update to. Valid values: 'latest' or 'canary'")
flags.AddExtensions(groupUpdateCmd, "Extensions to enable. Valid values: 'all' or 'none'")
}
var groupUpdateCmd = &cobra.Command{
Use: "update <group-name>",
Short: "Updates the group",
Args: cobra.ExactArgs(1),
ValidArgsFunction: groupArg,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
client, err := authedTursoClient()
if err != nil {
return err
}
name := args[0]
if _, err := getGroup(client, name); err != nil {
return err
}
version := flags.Version()
extensions := flags.Extensions()
if yesFlag {
return updateGroup(client, name, version, extensions)
}
fmt.Printf("To update group %s, all its locations and databases must be updated.\n", internal.Emph(name))
fmt.Printf("All your active connections to that group will be dropped and there will be a short downtime.\n\n")
ok, err := promptConfirmation("Are you sure you want to do this?")
if err != nil {
return fmt.Errorf("could not get prompt confirmed by user: %w", err)
}
if !ok {
fmt.Println("Group update skipped by the user.")
return nil
}
return updateGroup(client, name, version, extensions)
},
}
func updateGroup(client *turso.Client, name, version, extensions string) error {
msg := fmt.Sprintf("Updating group %s", internal.Emph(name))
s := prompt.Spinner(msg)
defer s.Stop()
if err := client.Groups.Update(name, version, extensions); err != nil {
return fmt.Errorf("error updating group")
}
s.Stop()
fmt.Printf("✔ Success! Group %s was updated successfully\n", internal.Emph(name))
return nil
}
func groupArg(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
}
client, err := authedTursoClient()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
}
groups, _ := groupNames(client)
return groups, cobra.ShellCompDirectiveNoFileComp | cobra.ShellCompDirectiveNoSpace
}