-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_bucket_versioning.go
81 lines (64 loc) · 2.29 KB
/
storage_bucket_versioning.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
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/collections"
"github.com/exoscale/cli/pkg/storage/sos"
)
const (
objVersioningOpArgIndex = 0
objVersioningBucketArgIndex = 1
objVersioningStatus = "status"
objVersioningEnable = "enable"
objVersioningSuspend = "suspend"
)
func init() {
storageBucketObjectVersioningCmd.Flags().StringP(zoneFlagLong, zoneFlagShort, "", zoneFlagMsg)
storageBucketCmd.AddCommand(storageBucketObjectVersioningCmd)
}
var storageBucketObjectVersioningCmd = &cobra.Command{
Use: "versioning {" + objVersioningStatus + "," + objVersioningEnable + "," + objVersioningSuspend + "} sos://BUCKET",
Aliases: []string{"v"},
Short: "Manage the Object Versioning setting of a Storage Bucket",
Long: storageBucketObjectVersioningCmdLongHelp(),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
permittedOps := collections.NewSet(objVersioningStatus, objVersioningEnable, objVersioningSuspend)
if !permittedOps.Contains(args[objOwnershipOpArgIndex]) {
cmdExitOnUsageError(cmd, "invalid operation")
}
args[objVersioningBucketArgIndex] = strings.TrimPrefix(args[objVersioningBucketArgIndex], sos.BucketPrefix)
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{zoneFlagLong})
},
RunE: func(cmd *cobra.Command, args []string) error {
versioningCommand := args[objVersioningOpArgIndex]
bucket := args[objVersioningBucketArgIndex]
zone, err := cmd.Flags().GetString(zoneFlagLong)
if err != nil {
return err
}
storage, err := sos.NewStorageClient(
gContext,
sos.ClientOptWithZone(zone),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}
switch versioningCommand {
case objVersioningStatus:
return printOutput(storage.BucketVersioningStatus(cmd.Context(), bucket))
case objVersioningEnable:
return storage.EnableBucketVersioning(cmd.Context(), bucket)
case objVersioningSuspend:
return storage.SuspendBucketVersioning(cmd.Context(), bucket)
}
return fmt.Errorf("invalid operation")
},
}
var storageBucketObjectVersioningCmdLongHelp = func() string {
return "Manage the Object Versioning setting of a Storage Bucket"
}