-
Notifications
You must be signed in to change notification settings - Fork 221
/
update.go
105 lines (86 loc) · 2.38 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
package volumes
import (
"context"
"fmt"
"github.com/spf13/cobra"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/flaps"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/internal/command"
"github.com/superfly/flyctl/internal/config"
"github.com/superfly/flyctl/internal/flag"
"github.com/superfly/flyctl/internal/flapsutil"
"github.com/superfly/flyctl/internal/render"
"github.com/superfly/flyctl/iostreams"
)
func newUpdate() *cobra.Command {
const (
short = "Update a volume for an app."
long = short + ` Volumes are persistent storage for
Fly Machines. Learn how to add a volume to
your app: https://fly.io/docs/apps/volume-storage/`
usage = "update <volumename>"
)
cmd := command.New(usage, short, long, runUpdate,
command.RequireSession,
command.RequireAppName,
)
cmd.Args = cobra.ExactArgs(1)
flag.Add(cmd,
flag.App(),
flag.AppConfig(),
flag.Int{
Name: "snapshot-retention",
Description: "Snapshot retention in days (min 5)",
},
flag.Bool{
Name: "scheduled-snapshots",
Description: "Disable/Enable scheduled snapshots",
},
)
flag.Add(cmd, flag.JSONOutput())
return cmd
}
func runUpdate(ctx context.Context) error {
var (
cfg = config.FromContext(ctx)
client = fly.ClientFromContext(ctx)
volumeID = flag.FirstArg(ctx)
)
appName := appconfig.NameFromContext(ctx)
if volumeID == "" && appName == "" {
return fmt.Errorf("volume ID or app required")
}
if appName == "" {
n, err := client.GetAppNameFromVolume(ctx, volumeID)
if err != nil {
return err
}
appName = *n
}
flapsClient, err := flapsutil.NewClientWithOptions(ctx, flaps.NewClientOpts{
AppName: appName,
})
if err != nil {
return err
}
var snapshotRetention *int
if flag.GetInt(ctx, "snapshot-retention") != 0 {
snapshotRetention = fly.Pointer(flag.GetInt(ctx, "snapshot-retention"))
}
out := iostreams.FromContext(ctx).Out
input := fly.UpdateVolumeRequest{
SnapshotRetention: snapshotRetention,
}
if flag.IsSpecified(ctx, "scheduled-snapshots") {
input.AutoBackupEnabled = fly.BoolPointer(flag.GetBool(ctx, "scheduled-snapshots"))
}
updatedVolume, err := flapsClient.UpdateVolume(ctx, volumeID, input)
if err != nil {
return fmt.Errorf("failed updating volume: %w", err)
}
if cfg.JSONOutput {
return render.JSON(out, updatedVolume)
}
return printVolume(out, updatedVolume, appName)
}