Add --format option to mfs stats command also add --hash and --size of hash and size only formats
Add --format option to mfs stats command also add --hash and --size of hash and size only formats License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
May 16, 2016 at 21:51 UTC
49936895ff9e6e76f8fc9e22453e919ea090801b
1 file changed
+63
-5
core/commands/files/files.go
+63
-5
@@ -53,6 +53,8 @@ on the files in question, then data may be lost. This also applies to running
53
},
54
}
55
56
+var formatError = errors.New("Format was set by multiple options. Only one format option is allowed")
57
+
58
var FilesStatCmd = &cmds.Command{
59
Helptext: cmds.HelpText{
60
Tagline: "Display file status.",
@@ -61,7 +63,24 @@ var FilesStatCmd = &cmds.Command{
63
Arguments: []cmds.Argument{
64
cmds.StringArg("path", true, false, "Path to node to stat."),
65
},
66
+ Options: []cmds.Option{
67
+ cmds.StringOption("format", "Print statistics in given format. Allowed tokens: "+
68
+ "<hash> <size> <cumulsize> <type> <childs>. Conflicts with other format options.").Default(
69
+ `<hash>
70
+Size: <size>
71
+CumulativeSize: <cumulsize>
72
+ChildBlocks: <childs>
73
+Type: <type>`),
74
+ cmds.BoolOption("hash", "Print only hash. Implies '--format=<hash>. Conflicts with other format options.").Default(false),
75
+ cmds.BoolOption("size", "Print only size. Implies '--format=<cumulsize>. Conflicts with other format options.").Default(false),
76
+ },
77
Run: func(req cmds.Request, res cmds.Response) {
78
+
79
+ _, err := statGetFormatOptions(req)
80
+ if err != nil {
81
+ res.SetError(err, cmds.ErrClient)
82
+ }
83
+
84
node, err := req.InvocContext().GetNode()
85
if err != nil {
86
res.SetError(err, cmds.ErrNormal)
@@ -90,19 +109,58 @@ var FilesStatCmd = &cmds.Command{
109
},
110
Marshalers: cmds.MarshalerMap{
111
cmds.Text: func(res cmds.Response) (io.Reader, error) {
112
+
113
out := res.Output().(*Object)
114
buf := new(bytes.Buffer)
95
- fmt.Fprintln(buf, out.Hash)
96
- fmt.Fprintf(buf, "Size: %d\n", out.Size)
97
- fmt.Fprintf(buf, "CumulativeSize: %d\n", out.CumulativeSize)
98
- fmt.Fprintf(buf, "ChildBlocks: %d\n", out.Blocks)
99
- fmt.Fprintf(buf, "Type: %s\n", out.Type)
115
+
116
+ s, _ := statGetFormatOptions(res.Request())
117
+ s = strings.Replace(s, "<hash>", out.Hash, -1)
118
+ s = strings.Replace(s, "<size>", fmt.Sprintf("%d", out.Size), -1)
119
+ s = strings.Replace(s, "<cumulsize>", fmt.Sprintf("%d", out.CumulativeSize), -1)
120
+ s = strings.Replace(s, "<childs>", fmt.Sprintf("%d", out.Blocks), -1)
121
+ s = strings.Replace(s, "<type>", out.Type, -1)
122
+
123
+ fmt.Fprintln(buf, s)
124
return buf, nil
125
},
126
},
127
Type: Object{},
128
}
129
130
+func statGetFormatOptions(req cmds.Request) (string, error) {
131
+
132
+ hash, _, _ := req.Option("hash").Bool()
133
+ size, _, _ := req.Option("size").Bool()
134
+ formatSpecified := req.Option("format").Found()
135
+
136
+ count := 0
137
+ if hash {
138
+ count++
139
+ }
140
+ if size {
141
+ count++
142
+ }
143
+ if formatSpecified {
144
+ count++
145
+ }
146
+ if count > 1 {
147
+ return "", formatError
148
+ }
149
+
150
+ format := ""
151
+ if hash {
152
+ format = "<hash>"
153
+ }
154
+ if size {
155
+ format = "<cumulsize>"
156
+ }
157
+ if len(format) == 0 {
158
+ format, _, _ = req.Option("format").String()
159
+ }
160
+
161
+ return format, nil
162
+}
163
+
164
func statNode(ds dag.DAGService, fsn mfs.FSNode) (*Object, error) {
165
nd, err := fsn.GetNode()
166
if err != nil {