-
Notifications
You must be signed in to change notification settings - Fork 221
/
update.go
120 lines (89 loc) · 2.57 KB
/
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package redis
import (
"context"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/flyctl/gql"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/prompt"
)
func newUpdate() (cmd *cobra.Command) {
const (
long = `Update an Upstash Redis database`
short = long
usage = "update <name>"
)
cmd = command.New(usage, short, long, runUpdate, command.RequireSession)
flag.Add(cmd,
flag.Org(),
flag.Region(),
flag.ReplicaRegions(),
)
cmd.Args = cobra.ExactArgs(1)
return cmd
}
func runUpdate(ctx context.Context) (err error) {
var (
out = iostreams.FromContext(ctx).Out
client = fly.ClientFromContext(ctx).GenqClient
)
id := flag.FirstArg(ctx)
response, err := gql.GetAddOn(ctx, client, id)
if err != nil {
return
}
addOn := response.AddOn
excludedRegions, err := GetExcludedRegions(ctx)
if err != nil {
return err
}
excludedRegions = append(excludedRegions, addOn.PrimaryRegion)
readRegions, err := prompt.MultiRegion(ctx, "Choose replica regions, or unselect to remove replica regions:", false, addOn.ReadRegions, excludedRegions, "replica-regions")
if err != nil {
return
}
var index int
var promptOptions []string
result, err := gql.ListAddOnPlans(ctx, client, gql.AddOnTypeUpstashRedis)
if err != nil {
return
}
for _, plan := range result.AddOnPlans.Nodes {
promptOptions = append(promptOptions, fmt.Sprintf("%s: %s", plan.DisplayName, plan.Description))
}
err = prompt.Select(ctx, &index, "Select an Upstash Redis plan", "", promptOptions...)
if err != nil {
return fmt.Errorf("failed to select a plan: %w", err)
}
// type Options struct {
// Eviction bool
// }
// options := &Options{}
options, _ := addOn.Options.(map[string]interface{})
if err != nil {
return
}
if options["eviction"] != nil && options["eviction"].(bool) {
if disableEviction, err := prompt.Confirm(ctx, " Would you like to disable eviction?"); disableEviction || err != nil {
options["eviction"] = false
}
} else {
options["eviction"], err = prompt.Confirm(ctx, " Would you like to enable eviction?")
}
if err != nil {
return
}
readRegionCodes := []string{}
for _, region := range *readRegions {
readRegionCodes = append(readRegionCodes, region.Code)
}
_, err = gql.UpdateAddOn(ctx, client, addOn.Id, result.AddOnPlans.Nodes[index].Id, readRegionCodes, options)
if err != nil {
return
}
fmt.Fprintf(out, "Your Upstash Redis database %s was updated.\n", addOn.Name)
return
}