Skip to content

Commit

Permalink
commands/object: fix object diff option error
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
  • Loading branch information
overbool committed Oct 27, 2018
1 parent 2f8dedc commit f0da3fb
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 332 deletions.
68 changes: 29 additions & 39 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"

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/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/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 res.Emit(&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 f0da3fb

Please sign in to comment.