-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_rb.go
69 lines (54 loc) · 1.47 KB
/
storage_rb.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
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/storage/sos"
)
var storageRbCmd = &cobra.Command{
Use: "rb sos://BUCKET",
Short: "Delete a bucket",
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], sos.BucketPrefix)
},
RunE: func(cmd *cobra.Command, args []string) error {
bucket := args[0]
recursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
if !force {
if !askQuestion(fmt.Sprintf("Are you sure you want to delete %s%s?", sos.BucketPrefix, bucket)) {
return nil
}
}
storage, err := sos.NewStorageClient(
gContext,
sos.ClientOptZoneFromBucket(gContext, bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %w", err)
}
if err := storage.DeleteBucket(gContext, bucket, recursive); err != nil {
return fmt.Errorf("unable to delete bucket: %w", err)
}
if !globalstate.Quiet {
fmt.Printf("Bucket %s%s deleted successfully\n", sos.BucketPrefix, bucket)
}
return nil
},
}
func init() {
storageRbCmd.Flags().BoolP("force", "f", false, cmdFlagForceHelp)
storageRbCmd.Flags().BoolP("recursive", "r", false,
"empty the bucket before deleting it")
storageCmd.AddCommand(storageRbCmd)
}