-
Notifications
You must be signed in to change notification settings - Fork 21
/
delete.go
50 lines (46 loc) · 1.45 KB
/
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
package cmd
import (
"fmt"
"os"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)
// deleteCmd represents delete command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "delete file from blobbers",
Long: `delete file from blobbers`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if fflags.Changed("allocation") == false { // check if the flag "path" is set
PrintError("Error: allocation flag is missing") // If not, we'll let the user know
os.Exit(1) // and return
}
if fflags.Changed("remotepath") == false {
PrintError("Error: remotepath flag is missing")
os.Exit(1)
}
allocationID := cmd.Flag("allocation").Value.String()
allocationObj, err := sdk.GetAllocation(allocationID)
if err != nil {
PrintError("Error fetching the allocation", err)
os.Exit(1)
}
remotepath := cmd.Flag("remotepath").Value.String()
err = allocationObj.DeleteFile(remotepath)
if err != nil {
PrintError(err.Error())
os.Exit(1)
}
fmt.Println(remotepath + " deleted")
return
},
}
func init() {
rootCmd.AddCommand(deleteCmd)
deleteCmd.PersistentFlags().String("allocation", "", "Allocation ID")
deleteCmd.PersistentFlags().String("remotepath", "", "Remote path of the object to delete")
deleteCmd.MarkFlagRequired("allocation")
deleteCmd.MarkFlagRequired("remotepath")
}