Skip to content
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

commands/object: use new cmds #5666

Merged
merged 4 commits into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for this?

Nothing wrong with, but it's not very consistent as we don't use contacts for any of the other options in this file.

Personally, I would err on the side of not using the constant, but others may disagree.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevina version is used in two places, so I use the constant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is almost every other option, or am I missing something.

In any case it not that big of a deal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the commands have actually been changed to use constants for these.

)

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