-
Notifications
You must be signed in to change notification settings - Fork 20
/
sos_delete.go
82 lines (66 loc) · 1.65 KB
/
sos_delete.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
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var sosDeleteCmd = &cobra.Command{
Use: "delete NAME",
Short: "Delete a bucket",
Aliases: gDeleteAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
bucket := args[0]
recursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
certsFile, err := cmd.Parent().Flags().GetString("certs-file")
if err != nil {
return err
}
sosClient, err := newSOSClient(certsFile)
if err != nil {
return err
}
location, err := sosClient.GetBucketLocation(bucket)
if err != nil {
return err
}
if err := sosClient.setZone(location); err != nil {
return err
}
if recursive { // Remove all files stored in the bucket before deleting it
objectsCh := make(chan string)
go func() {
defer close(objectsCh)
for obj := range sosClient.ListObjectsV2(bucket, "", true, gContext.Done()) {
if obj.Err != nil {
fmt.Fprintf(os.Stderr, "error: %s: %s\n", obj.Key, obj.Err)
os.Exit(1)
}
objectsCh <- obj.Key
}
}()
for rmObjErr := range sosClient.RemoveObjectsWithContext(gContext, bucket, objectsCh) {
if rmObjErr.Err != nil {
fmt.Fprintf(os.Stderr, "error: %s: %s\n", rmObjErr.ObjectName, rmObjErr.Err)
os.Exit(1)
}
}
}
if err = sosClient.RemoveBucket(bucket); err != nil {
return err
}
if !gQuiet {
fmt.Printf("Bucket %q deleted successfully\n", bucket)
}
return nil
},
}
func init() {
sosDeleteCmd.Flags().BoolP("recursive", "r", false, "Attempt to empty the bucket before deleting it")
sosCmd.AddCommand(sosDeleteCmd)
}