Skip to content

Commit

Permalink
ipfs files stat: use go-ipfs-cmds
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Michael Muré <batolettre@gmail.com>
  • Loading branch information
MichaelMure committed Feb 4, 2018
1 parent 53c42fc commit d716a93
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
29 changes: 29 additions & 0 deletions core/commands/files/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package commands

import (
"fmt"

"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/repo/config"
)

// GetNode extracts the node from the environment.
func GetNode(env interface{}) (*core.IpfsNode, error) {
ctx, ok := env.(*commands.Context)
if !ok {
return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
}

return ctx.GetNode()
}

// GetConfig extracts the config from the environment.
func GetConfig(env interface{}) (*config.Config, error) {
ctx, ok := env.(*commands.Context)
if !ok {
return nil, fmt.Errorf("expected env to be of type %T, got %T", ctx, env)
}

return ctx.GetConfig()
}
44 changes: 19 additions & 25 deletions core/commands/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ operations.
"cp": lgc.NewCommand(filesCpCmd),
"ls": lgc.NewCommand(filesLsCmd),
"mkdir": lgc.NewCommand(filesMkdirCmd),
"stat": lgc.NewCommand(filesStatCmd),
"stat": filesStatCmd,
"rm": lgc.NewCommand(filesRmCmd),
"flush": lgc.NewCommand(filesFlushCmd),
"chcid": lgc.NewCommand(filesChcidCmd),
Expand All @@ -75,7 +75,7 @@ CumulativeSize: <cumulsize>
ChildBlocks: <childs>
Type: <type>`

var filesStatCmd = &oldcmds.Command{
var filesStatCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Display file status.",
},
Expand All @@ -89,20 +89,20 @@ var filesStatCmd = &oldcmds.Command{
cmdkit.BoolOption("hash", "Print only hash. Implies '--format=<hash>'. Conflicts with other format options."),
cmdkit.BoolOption("size", "Print only size. Implies '--format=<cumulsize>'. Conflicts with other format options."),
},
Run: func(req oldcmds.Request, res oldcmds.Response) {
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {

_, err := statGetFormatOptions(req)
if err != nil {
res.SetError(err, cmdkit.ErrClient)
}

node, err := req.InvocContext().GetNode()
node, err := GetNode(env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

path, err := checkPath(req.Arguments()[0])
path, err := checkPath(req.Arguments[0])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -114,37 +114,31 @@ var filesStatCmd = &oldcmds.Command{
return
}

o, err := statNode(node.DAG, fsn)
o, err := statNode(fsn)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

res.SetOutput(o)
cmds.EmitOnce(res, o)
},
Marshalers: oldcmds.MarshalerMap{
oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}

Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
out, ok := v.(*Object)
if !ok {
return nil, e.TypeErr(out, v)
return e.TypeErr(out, v)
}
buf := new(bytes.Buffer)

s, _ := statGetFormatOptions(res.Request())
s, _ := statGetFormatOptions(req)
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
},
_, err := fmt.Fprintln(w, s)
return err
}),
},
Type: Object{},
}
Expand All @@ -153,11 +147,11 @@ func moreThanOne(a, b, c bool) bool {
return a && b || b && c || a && c
}

func statGetFormatOptions(req oldcmds.Request) (string, error) {
func statGetFormatOptions(req *cmds.Request) (string, error) {

hash, _, _ := req.Option("hash").Bool()
size, _, _ := req.Option("size").Bool()
format, _, _ := req.Option("format").String()
hash, _ := req.Options["hash"].(bool)
size, _ := req.Options["size"].(bool)
format, _ := req.Options["format"].(string)

if moreThanOne(hash, size, format != defaultStatFormat) {
return "", formatError
Expand All @@ -172,7 +166,7 @@ func statGetFormatOptions(req oldcmds.Request) (string, error) {
}
}

func statNode(ds ipld.DAGService, fsn mfs.FSNode) (*Object, error) {
func statNode(fsn mfs.FSNode) (*Object, error) {
nd, err := fsn.GetNode()
if err != nil {
return nil, err
Expand Down

0 comments on commit d716a93

Please sign in to comment.