-
Notifications
You must be signed in to change notification settings - Fork 23
/
rename.go
72 lines (65 loc) · 2.12 KB
/
rename.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
package cmd
import (
"fmt"
"os"
"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/core/pathutil"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)
// renameCmd represents rename command
var renameCmd = &cobra.Command{
Use: "rename",
Short: "rename an object(file/folder) on blobbers",
Long: `rename an object on blobbers`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
fflags := cmd.Flags() // fflags is a *flag.FlagSet
if !fflags.Changed("allocation") { // 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 os.Exit(1)
}
if !fflags.Changed("remotepath") {
PrintError("Error: remotepath flag is missing")
os.Exit(1)
}
if !fflags.Changed("destname") {
PrintError("Error: destname 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()
destName := cmd.Flag("destname").Value.String()
oldName := pathutil.Dir(remotePath)
if oldName == destName {
fmt.Println(remotePath + " renamed")
return
}
err = allocationObj.DoMultiOperation([]sdk.OperationRequest{
{
OperationType: constants.FileOperationRename,
RemotePath: remotePath,
DestName: destName,
},
})
if err != nil {
PrintError("Rename failed.", err)
os.Exit(1)
}
fmt.Println(remotePath + " renamed")
},
}
func init() {
rootCmd.AddCommand(renameCmd)
renameCmd.PersistentFlags().String("allocation", "", "Allocation ID")
renameCmd.PersistentFlags().String("remotepath", "", "Remote path of object to rename")
renameCmd.PersistentFlags().String("destname", "", "New Name for the object (Only the name and not the path). Include the file extension if applicable")
renameCmd.MarkFlagRequired("allocation")
renameCmd.MarkFlagRequired("remotepath")
renameCmd.MarkFlagRequired("destname")
}