Skip to content

Commit

Permalink
Merge pull request #5666 from overbool/refactor/commands/object
Browse files Browse the repository at this point in the history
commands/object: use new cmds
  • Loading branch information
Stebalien authored Nov 2, 2018
2 parents f337acd + e1d1f68 commit 6926812
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 335 deletions.
70 changes: 30 additions & 40 deletions core/commands/object/diff.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package objectcmd

import (
"bytes"
"fmt"
"io"

cmds "github.com/ipfs/go-ipfs/commands"
e "github.com/ipfs/go-ipfs/core/commands/e"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/dagutils"

cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)

const (
verboseOptionName = "verbose"
)

type Changes struct {
Changes []*dagutils.Change
}
Expand Down Expand Up @@ -49,34 +52,30 @@ Example:
cmdkit.StringArg("obj_b", true, false, "Object to diff."),
},
Options: []cmdkit.Option{
cmdkit.BoolOption("verbose", "v", "Print extra information."),
cmdkit.BoolOption(verboseOptionName, "v", "Print extra information."),
},
Run: func(req cmds.Request, res cmds.Response) {
api, err := req.InvocContext().GetApi()
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

a := req.Arguments()[0]
b := req.Arguments()[1]
a := req.Arguments[0]
b := req.Arguments[1]

pa, err := coreiface.ParsePath(a)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

pb, err := coreiface.ParsePath(b)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

changes, err := api.Object().Diff(req.Context(), pa, pb)
changes, err := api.Object().Diff(req.Context, pa, pb)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
return err
}

out := make([]*dagutils.Change, len(changes))
Expand All @@ -95,45 +94,36 @@ Example:
}
}

res.SetOutput(&Changes{out})
return cmds.EmitOnce(res, &Changes{out})
},
Type: Changes{},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Changes) error {
verbose, _ := req.Options[verboseOptionName].(bool)

verbose, _, _ := res.Request().Option("v").Bool()
changes, ok := v.(*Changes)
if !ok {
return nil, e.TypeErr(changes, v)
}

buf := new(bytes.Buffer)
for _, change := range changes.Changes {
for _, change := range out.Changes {
if verbose {
switch change.Type {
case dagutils.Add:
fmt.Fprintf(buf, "Added new link %q pointing to %s.\n", change.Path, change.After)
fmt.Fprintf(w, "Added new link %q pointing to %s.\n", change.Path, change.After)
case dagutils.Mod:
fmt.Fprintf(buf, "Changed %q from %s to %s.\n", change.Path, change.Before, change.After)
fmt.Fprintf(w, "Changed %q from %s to %s.\n", change.Path, change.Before, change.After)
case dagutils.Remove:
fmt.Fprintf(buf, "Removed link %q (was %s).\n", change.Path, change.Before)
fmt.Fprintf(w, "Removed link %q (was %s).\n", change.Path, change.Before)
}
} else {
switch change.Type {
case dagutils.Add:
fmt.Fprintf(buf, "+ %s %q\n", change.After, change.Path)
fmt.Fprintf(w, "+ %s %q\n", change.After, change.Path)
case dagutils.Mod:
fmt.Fprintf(buf, "~ %s %s %q\n", change.Before, change.After, change.Path)
fmt.Fprintf(w, "~ %s %s %q\n", change.Before, change.After, change.Path)
case dagutils.Remove:
fmt.Fprintf(buf, "- %s %q\n", change.Before, change.Path)
fmt.Fprintf(w, "- %s %q\n", change.Before, change.Path)
}
}
}
return buf, nil
},

return nil
}),
},
}
Loading

0 comments on commit 6926812

Please sign in to comment.