-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --format option to mfs stat command #2706
Changes from all commits
4993689
94f1922
130c08c
64677e7
fea0b94
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,8 @@ on the files in question, then data may be lost. This also applies to running | |
}, | ||
} | ||
|
||
var formatError = errors.New("Format was set by multiple options. Only one format option is allowed") | ||
|
||
var FilesStatCmd = &cmds.Command{ | ||
Helptext: cmds.HelpText{ | ||
Tagline: "Display file status.", | ||
|
@@ -61,7 +63,24 @@ var FilesStatCmd = &cmds.Command{ | |
Arguments: []cmds.Argument{ | ||
cmds.StringArg("path", true, false, "Path to node to stat."), | ||
}, | ||
Options: []cmds.Option{ | ||
cmds.StringOption("format", "Print statistics in given format. Allowed tokens: "+ | ||
"<hash> <size> <cumulsize> <type> <childs>. Conflicts with other format options.").Default( | ||
`<hash> | ||
Size: <size> | ||
CumulativeSize: <cumulsize> | ||
ChildBlocks: <childs> | ||
Type: <type>`), | ||
cmds.BoolOption("hash", "Print only hash. Implies '--format=<hash>. Conflicts with other format options.").Default(false), | ||
cmds.BoolOption("size", "Print only size. Implies '--format=<cumulsize>. Conflicts with other format options.").Default(false), | ||
}, | ||
Run: func(req cmds.Request, res cmds.Response) { | ||
|
||
_, err := statGetFormatOptions(req) | ||
if err != nil { | ||
res.SetError(err, cmds.ErrClient) | ||
} | ||
|
||
node, err := req.InvocContext().GetNode() | ||
if err != nil { | ||
res.SetError(err, cmds.ErrNormal) | ||
|
@@ -90,19 +109,47 @@ var FilesStatCmd = &cmds.Command{ | |
}, | ||
Marshalers: cmds.MarshalerMap{ | ||
cmds.Text: func(res cmds.Response) (io.Reader, error) { | ||
|
||
out := res.Output().(*Object) | ||
buf := new(bytes.Buffer) | ||
fmt.Fprintln(buf, out.Hash) | ||
fmt.Fprintf(buf, "Size: %d\n", out.Size) | ||
fmt.Fprintf(buf, "CumulativeSize: %d\n", out.CumulativeSize) | ||
fmt.Fprintf(buf, "ChildBlocks: %d\n", out.Blocks) | ||
fmt.Fprintf(buf, "Type: %s\n", out.Type) | ||
|
||
s, _ := statGetFormatOptions(res.Request()) | ||
s = strings.Replace(s, "<hash>", out.Hash, -1) | ||
s = strings.Replace(s, "<size>", fmt.Sprintf("%d", out.Size), -1) | ||
s = strings.Replace(s, "<cumulsize>", fmt.Sprintf("%d", out.CumulativeSize), -1) | ||
s = strings.Replace(s, "<childs>", fmt.Sprintf("%d", out.Blocks), -1) | ||
s = strings.Replace(s, "<type>", out.Type, -1) | ||
|
||
fmt.Fprintln(buf, s) | ||
return buf, nil | ||
}, | ||
}, | ||
Type: Object{}, | ||
} | ||
|
||
func moreThanOne(a, b, c bool) bool { | ||
return a && b || b && c || a && c | ||
} | ||
|
||
func statGetFormatOptions(req cmds.Request) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'd write this function as: hash, _, _ := req.Option("hash").Bool()
size, _, _ := req.Option("size").Bool()
format, found, _ := req.Option("format").Found()
if moreThanOne(hash, size, found) {
return "", formatError
}
if hash {
return "<hash>", nil
} else if size {
return "<cumulsize>", nil
} else {
return format, nil
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do, I was quite sleepy when I was doing it. |
||
|
||
hash, _, _ := req.Option("hash").Bool() | ||
size, _, _ := req.Option("size").Bool() | ||
format, found, _ := req.Option("format").String() | ||
|
||
if moreThanOne(hash, size, found) { | ||
return "", formatError | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am 100% sure there is better pattern to do it. My C tainted mind where I would just add them couldn't find it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think this works: func moreThanOne(a,b,c bool) bool {
return !((a == b == c) && !(a && b && c)) && (a || b || c)
} it can probably be reduced... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, it is so easy 😢 😄 |
||
|
||
if hash { | ||
return "<hash>", nil | ||
} else if size { | ||
return "<cumulsize>", nil | ||
} else { | ||
return format, nil | ||
} | ||
} | ||
|
||
func statNode(ds dag.DAGService, fsn mfs.FSNode) (*Object, error) { | ||
nd, err := fsn.GetNode() | ||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be prefixed with
Hash: <hash>
-- otherwise it only prints the hash in one line and thenKey: Value
format.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this was this way in the past-- it seems like it was a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was always like that, changing it would break CLI compatibility.
It is so you could do
ipfs files stat /file | head -1
to get the hash before format options were a thing.