|
| 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/turso" |
| 9 | +) |
| 10 | + |
| 11 | +func init() { |
| 12 | + groupCmd.AddCommand(groupConfigCmd) |
| 13 | + groupConfigCmd.AddCommand(groupDeleteProtectionCmd) |
| 14 | + groupDeleteProtectionCmd.AddCommand(groupEnableDeleteProtectionCmd) |
| 15 | + groupDeleteProtectionCmd.AddCommand(groupDisableDeleteProtectionCmd) |
| 16 | + groupDeleteProtectionCmd.AddCommand(groupShowDeleteProtectionCmd) |
| 17 | +} |
| 18 | + |
| 19 | +var groupConfigCmd = &cobra.Command{ |
| 20 | + Use: "config", |
| 21 | + Short: "Manage group config", |
| 22 | + ValidArgsFunction: noSpaceArg, |
| 23 | +} |
| 24 | + |
| 25 | +var groupDeleteProtectionCmd = &cobra.Command{ |
| 26 | + Use: "delete-protection", |
| 27 | + Short: "Manage delete-protection config of a group", |
| 28 | + ValidArgsFunction: noSpaceArg, |
| 29 | +} |
| 30 | + |
| 31 | +var groupEnableDeleteProtectionCmd = &cobra.Command{ |
| 32 | + Use: "enable <group-name>", |
| 33 | + Short: "Disables delete protection for this group", |
| 34 | + Args: cobra.ExactArgs(1), |
| 35 | + ValidArgsFunction: dbNameArg, |
| 36 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 37 | + cmd.SilenceUsage = true |
| 38 | + return updateGroupDeleteProtection(args[0], true) |
| 39 | + }, |
| 40 | +} |
| 41 | + |
| 42 | +var groupDisableDeleteProtectionCmd = &cobra.Command{ |
| 43 | + Use: "disable <group-name>", |
| 44 | + Short: "Disables delete protection for this group", |
| 45 | + Args: cobra.ExactArgs(1), |
| 46 | + ValidArgsFunction: dbNameArg, |
| 47 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 48 | + cmd.SilenceUsage = true |
| 49 | + return updateGroupDeleteProtection(args[0], false) |
| 50 | + }, |
| 51 | +} |
| 52 | + |
| 53 | +var groupShowDeleteProtectionCmd = &cobra.Command{ |
| 54 | + Use: "show <group-name>", |
| 55 | + Short: "Shows the delete protection status of a group", |
| 56 | + Args: cobra.ExactArgs(1), |
| 57 | + ValidArgsFunction: dbNameArg, |
| 58 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 59 | + cmd.SilenceUsage = true |
| 60 | + client, err := authedTursoClient() |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + name := args[0] |
| 65 | + |
| 66 | + group, err := getGroup(client, name) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + config, err := client.Groups.GetConfig(group.Name) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + fmt.Print(groupDeleteProtectionMessage(config.IsDeleteProtected())) |
| 75 | + return err |
| 76 | + }, |
| 77 | +} |
| 78 | + |
| 79 | +func updateGroupDeleteProtection(name string, deleteProtection bool) error { |
| 80 | + client, err := authedTursoClient() |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + group, err := getGroup(client, name) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + return client.Groups.UpdateConfig(group.Name, turso.GroupConfig{DeleteProtection: &deleteProtection}) |
| 89 | +} |
| 90 | + |
| 91 | +func groupDeleteProtectionMessage(status bool) string { |
| 92 | + msg := "off" |
| 93 | + if status { |
| 94 | + msg = "on" |
| 95 | + } |
| 96 | + return fmt.Sprintf("Delete Protection %s\n", internal.Emph(msg)) |
| 97 | +} |
0 commit comments