-
Notifications
You must be signed in to change notification settings - Fork 20
/
sos_delete.go
81 lines (65 loc) · 1.62 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
package cmd
import (
"fmt"
"log"
"os"
"github.com/spf13/cobra"
)
// sosDeleteCmd represents the delete command
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
}
minioClient, err := newMinioClient(sosZone)
if err != nil {
log.Fatal(err)
}
zone, err := minioClient.GetBucketLocation(bucket)
if err != nil {
return err
}
minioClient, err = newMinioClient(zone)
if 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 minioClient.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 minioClient.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 = minioClient.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)
}