-
Notifications
You must be signed in to change notification settings - Fork 19
/
updateattributes.go
119 lines (96 loc) · 2.84 KB
/
updateattributes.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package cmd
import (
"log"
"sync"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/zboxcore/sdk"
"github.com/spf13/cobra"
)
// The updateAttributesCmd used to update file attributes.
var updateAttributesCmd = &cobra.Command{
Use: "update-attributes",
Short: "update object attributes on blobbers",
Long: `update object attributes on blobbers`,
Args: cobra.MinimumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
var (
fflags = cmd.Flags()
allocID string
remotePath string
alloc *sdk.Allocation
changed, commit bool
err error
)
if !fflags.Changed("allocation") {
log.Fatal("missing 'allocation' flag")
}
if !fflags.Changed("remotepath") {
log.Fatal("missing 'remotepath' flag")
}
if allocID, err = fflags.GetString("allocation"); err != nil {
log.Fatalf("parsing 'allocation' flag: %v", err)
}
if remotePath, err = fflags.GetString("remotepath"); err != nil {
log.Fatalf("paring 'remotepath' flag: %v", err)
}
if fflags.Changed("commit") {
if commit, err = fflags.GetBool("commit"); err != nil {
log.Fatalf("parsing 'commit' flag: %v'", err)
}
}
if alloc, err = sdk.GetAllocation(allocID); err != nil {
log.Fatal("fetching the allocation: ", err)
}
meta, err := alloc.GetFileMeta(remotePath)
if err != nil {
log.Fatal("fetching the metadata: ", err)
}
var attrs = meta.Attributes
// modify attributes by the flags
if fflags.Changed("who-pays-for-reads") {
var (
wp common.WhoPays
wps string
)
if wps, err = fflags.GetString("who-pays-for-reads"); err != nil {
log.Fatalf("getting 'who-pays-for-reads' flag: %v", err)
}
if err = wp.Parse(wps); err != nil {
log.Fatal(err)
}
if wp != attrs.WhoPaysForReads {
attrs.WhoPaysForReads, changed = wp, true // change
}
}
if !changed {
log.Print("no changes")
return
}
if err = alloc.UpdateObjectAttributes(remotePath, attrs); err != nil {
log.Fatal("updating file attributes: ", err)
}
log.Print("attributes updated")
if !commit {
return
}
log.Print("committing changes...")
var (
wg = &sync.WaitGroup{}
statusBar = &StatusBar{wg: wg}
)
wg.Add(1)
commitMetaTxn(remotePath, "Update attributes", "", "", alloc, meta,
statusBar)
wg.Wait()
},
}
func init() {
rootCmd.AddCommand(updateAttributesCmd)
updateAttributesCmd.PersistentFlags().String("allocation", "", "Allocation ID")
updateAttributesCmd.PersistentFlags().String("remotepath", "", "Remote path of object to rename")
updateAttributesCmd.PersistentFlags().String("who-pays-for-reads", "owner",
"Who pays for reads: owner or 3rd_party")
updateAttributesCmd.Flags().Bool("commit", false, "pass this option to commit the metadata transaction")
updateAttributesCmd.MarkFlagRequired("allocation")
updateAttributesCmd.MarkFlagRequired("remotepath")
}