From 677c953dc4c063da443663a384a1ed1fce1f0305 Mon Sep 17 00:00:00 2001 From: Overbool Date: Sat, 27 Oct 2018 03:26:28 +0800 Subject: [PATCH 1/4] commands/object: use new cmds License: MIT Signed-off-by: Overbool --- core/commands/object/diff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/commands/object/diff.go b/core/commands/object/diff.go index f200e10dade..b07e5a6139b 100644 --- a/core/commands/object/diff.go +++ b/core/commands/object/diff.go @@ -5,11 +5,11 @@ import ( "fmt" "io" - cmds "github.com/ipfs/go-ipfs/commands" e "github.com/ipfs/go-ipfs/core/commands/e" 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" ) From df58a2d617b27fa103259db5772c5c40cf00fa31 Mon Sep 17 00:00:00 2001 From: Overbool Date: Sat, 27 Oct 2018 10:03:43 +0800 Subject: [PATCH 2/4] commands/object: fix object diff option error License: MIT Signed-off-by: Overbool --- core/commands/object/diff.go | 68 +++---- core/commands/object/object.go | 340 ++++++++++++--------------------- core/commands/object/patch.go | 127 ++++++------ core/commands/root.go | 4 +- 4 files changed, 208 insertions(+), 331 deletions(-) diff --git a/core/commands/object/diff.go b/core/commands/object/diff.go index b07e5a6139b..c6bccaf2c55 100644 --- a/core/commands/object/diff.go +++ b/core/commands/object/diff.go @@ -1,11 +1,10 @@ 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" @@ -13,6 +12,10 @@ import ( cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) +const ( + verboseOptionName = "verbose" +) + type Changes struct { Changes []*dagutils.Change } @@ -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)) @@ -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 + }), }, } diff --git a/core/commands/object/object.go b/core/commands/object/object.go index e0e0a3b93af..4d729052eca 100644 --- a/core/commands/object/object.go +++ b/core/commands/object/object.go @@ -1,18 +1,14 @@ package objectcmd import ( - "bytes" "encoding/base64" "errors" "fmt" "io" "io/ioutil" - "strings" "text/tabwriter" - oldcmds "github.com/ipfs/go-ipfs/commands" - lgc "github.com/ipfs/go-ipfs/commands/legacy" - 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/core/coreapi/interface/options" @@ -23,8 +19,6 @@ import ( cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) -const inputLimit = 2 << 20 - type Node struct { Links []Link Data string @@ -51,18 +45,18 @@ directly.`, }, Subcommands: map[string]*cmds.Command{ - "data": lgc.NewCommand(ObjectDataCmd), - "diff": lgc.NewCommand(ObjectDiffCmd), - "get": lgc.NewCommand(ObjectGetCmd), - "links": lgc.NewCommand(ObjectLinksCmd), - "new": lgc.NewCommand(ObjectNewCmd), + "data": ObjectDataCmd, + "diff": ObjectDiffCmd, + "get": ObjectGetCmd, + "links": ObjectLinksCmd, + "new": ObjectNewCmd, "patch": ObjectPatchCmd, - "put": lgc.NewCommand(ObjectPutCmd), - "stat": lgc.NewCommand(ObjectStatCmd), + "put": ObjectPutCmd, + "stat": ObjectStatCmd, }, } -var ObjectDataCmd = &oldcmds.Command{ +var ObjectDataCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Output the raw bytes of an IPFS object.", ShortDescription: ` @@ -81,30 +75,27 @@ is the raw data of the object. Arguments: []cmdkit.Argument{ cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(), }, - Run: func(req oldcmds.Request, res oldcmds.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 } - path, err := coreiface.ParsePath(req.Arguments()[0]) + path, err := coreiface.ParsePath(req.Arguments[0]) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - data, err := api.Object().Data(req.Context(), path) + data, err := api.Object().Data(req.Context, path) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - res.SetOutput(data) + return res.Emit(data) }, } -var ObjectLinksCmd = &oldcmds.Command{ +var ObjectLinksCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Output the links pointed to by the specified object.", ShortDescription: ` @@ -120,35 +111,25 @@ multihash. Options: []cmdkit.Option{ cmdkit.BoolOption("headers", "v", "Print table headers (Hash, Size, Name)."), }, - Run: func(req oldcmds.Request, res oldcmds.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 - } - - // get options early -> exit early in case of error - if _, _, err := req.Option("headers").Bool(); err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - path, err := coreiface.ParsePath(req.Arguments()[0]) + path, err := coreiface.ParsePath(req.Arguments[0]) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - rp, err := api.ResolvePath(req.Context(), path) + rp, err := api.ResolvePath(req.Context, path) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - links, err := api.Object().Links(req.Context(), rp) + links, err := api.Object().Links(req.Context, rp) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } outLinks := make([]Link, len(links)) @@ -165,37 +146,27 @@ multihash. Links: outLinks, } - res.SetOutput(out) + return res.Emit(out) }, - Marshalers: oldcmds.MarshalerMap{ - oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) { - v, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - - object, ok := v.(*Object) - if !ok { - return nil, e.TypeErr(object, v) - } - - buf := new(bytes.Buffer) - w := tabwriter.NewWriter(buf, 1, 2, 1, ' ', 0) - headers, _, _ := res.Request().Option("headers").Bool() + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { + tw := tabwriter.NewWriter(w, 1, 2, 1, ' ', 0) + headers, _ := req.Options["headers"].(bool) if headers { - fmt.Fprintln(w, "Hash\tSize\tName") + fmt.Fprintln(tw, "Hash\tSize\tName") } - for _, link := range object.Links { - fmt.Fprintf(w, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name) + for _, link := range out.Links { + fmt.Fprintf(tw, "%s\t%v\t%s\n", link.Hash, link.Size, link.Name) } - w.Flush() - return buf, nil - }, + tw.Flush() + + return nil + }), }, Type: &Object{}, } -var ObjectGetCmd = &oldcmds.Command{ +var ObjectGetCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Get and serialize the DAG node named by .", ShortDescription: ` @@ -229,47 +200,40 @@ Supported values are: Options: []cmdkit.Option{ cmdkit.StringOption("data-encoding", "Encoding type of the data field, either \"text\" or \"base64\".").WithDefault("text"), }, - Run: func(req oldcmds.Request, res oldcmds.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 } - path, err := coreiface.ParsePath(req.Arguments()[0]) + path, err := coreiface.ParsePath(req.Arguments[0]) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - datafieldenc, _, err := req.Option("data-encoding").String() + datafieldenc, _ := req.Options["data-encoding"].(string) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - nd, err := api.Object().Get(req.Context(), path) + nd, err := api.Object().Get(req.Context, path) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - r, err := api.Object().Data(req.Context(), path) + r, err := api.Object().Data(req.Context, path) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } data, err := ioutil.ReadAll(r) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } out, err := encodeData(data, datafieldenc) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } node := &Node{ @@ -285,37 +249,29 @@ Supported values are: } } - res.SetOutput(node) + return res.Emit(node) }, Type: Node{}, - Marshalers: oldcmds.MarshalerMap{ - oldcmds.Protobuf: func(res oldcmds.Response) (io.Reader, error) { - v, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - - node, ok := v.(*Node) - if !ok { - return nil, e.TypeErr(node, v) - } - + Encoders: cmds.EncoderMap{ + cmds.Protobuf: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Node) error { // deserialize the Data field as text as this was the standard behaviour - object, err := deserializeNode(node, "text") + object, err := deserializeNode(out, "text") if err != nil { - return nil, err + return nil } marshaled, err := object.Marshal() if err != nil { - return nil, err + return err } - return bytes.NewReader(marshaled), nil - }, + fmt.Fprint(w, marshaled) + + return nil + }), }, } -var ObjectStatCmd = &oldcmds.Command{ +var ObjectStatCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Get stats for the DAG node named by .", ShortDescription: ` @@ -333,23 +289,20 @@ var ObjectStatCmd = &oldcmds.Command{ Arguments: []cmdkit.Argument{ cmdkit.StringArg("key", true, false, "Key of the object to retrieve, in base58-encoded multihash format.").EnableStdin(), }, - Run: func(req oldcmds.Request, res oldcmds.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 } - path, err := coreiface.ParsePath(req.Arguments()[0]) + path, err := coreiface.ParsePath(req.Arguments[0]) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - ns, err := api.Object().Stat(req.Context(), path) + ns, err := api.Object().Stat(req.Context, path) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } oldStat := &ipld.NodeStat{ @@ -361,37 +314,26 @@ var ObjectStatCmd = &oldcmds.Command{ CumulativeSize: ns.CumulativeSize, } - res.SetOutput(oldStat) + return res.Emit(oldStat) }, Type: ipld.NodeStat{}, - Marshalers: oldcmds.MarshalerMap{ - oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) { - v, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - - ns, ok := v.(*ipld.NodeStat) - if !ok { - return nil, e.TypeErr(ns, v) + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ipld.NodeStat) error { + fw := func(s string, n int) { + fmt.Fprintf(w, "%s: %d\n", s, n) } - - buf := new(bytes.Buffer) - w := func(s string, n int) { - fmt.Fprintf(buf, "%s: %d\n", s, n) - } - w("NumLinks", ns.NumLinks) - w("BlockSize", ns.BlockSize) - w("LinksSize", ns.LinksSize) - w("DataSize", ns.DataSize) - w("CumulativeSize", ns.CumulativeSize) - - return buf, nil - }, + fw("NumLinks", out.NumLinks) + fw("BlockSize", out.BlockSize) + fw("LinksSize", out.LinksSize) + fw("DataSize", out.DataSize) + fw("CumulativeSize", out.CumulativeSize) + + return nil + }), }, } -var ObjectPutCmd = &oldcmds.Command{ +var ObjectPutCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Store input as a DAG object, print its key.", ShortDescription: ` @@ -438,73 +380,60 @@ And then run: cmdkit.BoolOption("pin", "Pin this object when adding."), cmdkit.BoolOption("quiet", "q", "Write minimal output."), }, - Run: func(req oldcmds.Request, res oldcmds.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 } - input, err := req.Files().NextFile() + input, err := req.Files.NextFile() if err != nil && err != io.EOF { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - inputenc, _, err := req.Option("inputenc").String() + inputenc, _ := req.Options["inputenc"].(string) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - datafieldenc, _, err := req.Option("datafieldenc").String() + datafieldenc, _ := req.Options["datafieldenc"].(string) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - dopin, _, err := req.Option("pin").Bool() + dopin, _ := req.Options["pin"].(bool) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - p, err := api.Object().Put(req.Context(), input, + p, err := api.Object().Put(req.Context, input, options.Object.DataType(datafieldenc), options.Object.InputEnc(inputenc), options.Object.Pin(dopin)) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - res.SetOutput(&Object{Hash: p.Cid().String()}) + return res.Emit(&Object{Hash: p.Cid().String()}) }, - Marshalers: oldcmds.MarshalerMap{ - oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) { - quiet, _, _ := res.Request().Option("quiet").Bool() + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { + quiet, _ := req.Options["quiet"].(bool) - v, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - obj, ok := v.(*Object) - if !ok { - return nil, e.TypeErr(obj, v) - } - - out := obj.Hash + "\n" + o := out.Hash if !quiet { - out = "added " + out + o = "added " + o } - return strings.NewReader(out), nil - }, + fmt.Fprintln(w, o) + + return nil + }), }, Type: Object{}, } -var ObjectNewCmd = &oldcmds.Command{ +var ObjectNewCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Create a new object from an ipfs template.", ShortDescription: ` @@ -523,40 +452,29 @@ Available templates: Arguments: []cmdkit.Argument{ cmdkit.StringArg("template", false, false, "Template to use. Optional."), }, - Run: func(req oldcmds.Request, res oldcmds.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 } template := "empty" - if len(req.Arguments()) == 1 { - template = req.Arguments()[0] + if len(req.Arguments) == 1 { + template = req.Arguments[0] } - nd, err := api.Object().New(req.Context(), options.Object.Type(template)) + nd, err := api.Object().New(req.Context, options.Object.Type(template)) if err != nil && err != io.EOF { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - res.SetOutput(&Object{Hash: nd.Cid().String()}) + return res.Emit(&Object{Hash: nd.Cid().String()}) }, - Marshalers: oldcmds.MarshalerMap{ - oldcmds.Text: func(res oldcmds.Response) (io.Reader, error) { - v, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - - obj, ok := v.(*Object) - if !ok { - return nil, e.TypeErr(obj, v) - } - - return strings.NewReader(obj.Hash + "\n"), nil - }, + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { + fmt.Fprintln(w, out.Hash) + return nil + }), }, Type: Object{}, } @@ -594,20 +512,6 @@ func deserializeNode(nd *Node, dataFieldEncoding string) (*dag.ProtoNode, error) return dagnode, nil } -// copy+pasted from ../commands.go -func unwrapOutput(i interface{}) (interface{}, error) { - var ( - ch <-chan interface{} - ok bool - ) - - if ch, ok = i.(<-chan interface{}); !ok { - return nil, e.TypeErr(ch, i) - } - - return <-ch, nil -} - func encodeData(data []byte, encoding string) (string, error) { switch encoding { case "text": diff --git a/core/commands/object/patch.go b/core/commands/object/patch.go index 0b9db961c6b..60cca92d866 100644 --- a/core/commands/object/patch.go +++ b/core/commands/object/patch.go @@ -3,12 +3,8 @@ package objectcmd import ( "fmt" "io" - "strings" - oldcmds "github.com/ipfs/go-ipfs/commands" - lgc "github.com/ipfs/go-ipfs/commands/legacy" cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" - e "github.com/ipfs/go-ipfs/core/commands/e" coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" "github.com/ipfs/go-ipfs/core/coreapi/interface/options" @@ -28,26 +24,12 @@ result. This is the Merkle-DAG version of modifying an object. Arguments: []cmdkit.Argument{}, Subcommands: map[string]*cmds.Command{ "append-data": patchAppendDataCmd, - "add-link": lgc.NewCommand(patchAddLinkCmd), - "rm-link": lgc.NewCommand(patchRmLinkCmd), - "set-data": lgc.NewCommand(patchSetDataCmd), + "add-link": patchAddLinkCmd, + "rm-link": patchRmLinkCmd, + "set-data": patchSetDataCmd, }, } -func objectMarshaler(res oldcmds.Response) (io.Reader, error) { - v, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - - o, ok := v.(*Object) - if !ok { - return nil, e.TypeErr(o, v) - } - - return strings.NewReader(o.Hash + "\n"), nil -} - var patchAppendDataCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Append data to the data segment of a dag node.", @@ -99,7 +81,7 @@ the limit will not be respected by the network. }, } -var patchSetDataCmd = &oldcmds.Command{ +var patchSetDataCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Set the data field of an IPFS object.", ShortDescription: ` @@ -114,36 +96,39 @@ Example: cmdkit.StringArg("root", true, false, "The hash of the node to modify."), cmdkit.FileArg("data", true, false, "The data to set the object to.").EnableStdin(), }, - Run: func(req oldcmds.Request, res oldcmds.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 { + return err + } - root, err := coreiface.ParsePath(req.StringArguments()[0]) + root, err := coreiface.ParsePath(req.Arguments[0]) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - data, err := req.Files().NextFile() + data, err := req.Files.NextFile() if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - p, err := api.Object().SetData(req.Context(), root, data) + p, err := api.Object().SetData(req.Context, root, data) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - res.SetOutput(&Object{Hash: p.Cid().String()}) + return res.Emit(&Object{Hash: p.Cid().String()}) }, Type: Object{}, - Marshalers: oldcmds.MarshalerMap{ - oldcmds.Text: objectMarshaler, + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { + fmt.Fprintln(w, out.Hash) + return nil + }), }, } -var patchRmLinkCmd = &oldcmds.Command{ +var patchRmLinkCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Remove a link from a given object.", ShortDescription: ` @@ -154,35 +139,35 @@ Remove a Merkle-link from the given object and return the hash of the result. cmdkit.StringArg("root", true, false, "The hash of the node to modify."), cmdkit.StringArg("name", true, false, "Name of the link to remove."), }, - Run: func(req oldcmds.Request, res oldcmds.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 } - root, err := coreiface.ParsePath(req.Arguments()[0]) + root, err := coreiface.ParsePath(req.Arguments[0]) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - name := req.Arguments()[1] - p, err := api.Object().RmLink(req.Context(), root, name) + name := req.Arguments[1] + p, err := api.Object().RmLink(req.Context, root, name) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - res.SetOutput(&Object{Hash: p.Cid().String()}) + return res.Emit(&Object{Hash: p.Cid().String()}) }, Type: Object{}, - Marshalers: oldcmds.MarshalerMap{ - oldcmds.Text: objectMarshaler, + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { + fmt.Fprintln(w, out.Hash) + return nil + }), }, } -var patchAddLinkCmd = &oldcmds.Command{ +var patchAddLinkCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Add a link to a given object.", ShortDescription: ` @@ -206,44 +191,42 @@ to a file containing 'bar', and returns the hash of the new object. Options: []cmdkit.Option{ cmdkit.BoolOption("create", "p", "Create intermediary nodes."), }, - Run: func(req oldcmds.Request, res oldcmds.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 } - root, err := coreiface.ParsePath(req.Arguments()[0]) + root, err := coreiface.ParsePath(req.Arguments[0]) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - name := req.Arguments()[1] + name := req.Arguments[1] - child, err := coreiface.ParsePath(req.Arguments()[2]) + child, err := coreiface.ParsePath(req.Arguments[2]) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - create, _, err := req.Option("create").Bool() + create, _ := req.Options["create"].(bool) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - p, err := api.Object().AddLink(req.Context(), root, name, child, + p, err := api.Object().AddLink(req.Context, root, name, child, options.Object.Create(create)) if err != nil { - res.SetError(err, cmdkit.ErrNormal) - return + return err } - res.SetOutput(&Object{Hash: p.Cid().String()}) + return res.Emit(&Object{Hash: p.Cid().String()}) }, Type: Object{}, - Marshalers: oldcmds.MarshalerMap{ - oldcmds.Text: objectMarshaler, + Encoders: cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { + fmt.Fprintln(w, out.Hash) + return nil + }), }, } diff --git a/core/commands/root.go b/core/commands/root.go index 9ddd227ed6c..b2c73ef52dd 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -174,8 +174,8 @@ var rootROSubcommands = map[string]*cmds.Command{ "resolve": name.IpnsCmd, }, }, - "object": lgc.NewCommand(&oldcmds.Command{ - Subcommands: map[string]*oldcmds.Command{ + "object": { + Subcommands: map[string]*cmds.Command{ "data": ocmd.ObjectDataCmd, "links": ocmd.ObjectLinksCmd, "get": ocmd.ObjectGetCmd, From 0fe7bdfce9f9f74f4eeaefe76b16c38b69b0c233 Mon Sep 17 00:00:00 2001 From: Overbool Date: Sat, 27 Oct 2018 10:08:42 +0800 Subject: [PATCH 3/4] commands/object: add comment License: MIT Signed-off-by: Overbool --- core/commands/object/diff.go | 2 +- core/commands/object/object.go | 6 ++++++ core/commands/root.go | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/core/commands/object/diff.go b/core/commands/object/diff.go index c6bccaf2c55..109fd79963c 100644 --- a/core/commands/object/diff.go +++ b/core/commands/object/diff.go @@ -8,7 +8,7 @@ import ( coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" "github.com/ipfs/go-ipfs/dagutils" - cmds "gx/ipfs/QmdTmGruUz23vgzym3uWpnAEQdGdGifQqBvP8UXSRjG8gZ/go-ipfs-cmds" + cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds" cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) diff --git a/core/commands/object/object.go b/core/commands/object/object.go index 4d729052eca..4226bf41d3b 100644 --- a/core/commands/object/object.go +++ b/core/commands/object/object.go @@ -56,6 +56,7 @@ directly.`, }, } +// ObjectDataCmd object data command var ObjectDataCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Output the raw bytes of an IPFS object.", @@ -95,6 +96,7 @@ is the raw data of the object. }, } +// ObjectLinksCmd object links command var ObjectLinksCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Output the links pointed to by the specified object.", @@ -166,6 +168,7 @@ multihash. Type: &Object{}, } +// ObjectGetCmd object get command var ObjectGetCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Get and serialize the DAG node named by .", @@ -271,6 +274,7 @@ Supported values are: }, } +// ObjectStatCmd object stat command var ObjectStatCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Get stats for the DAG node named by .", @@ -333,6 +337,7 @@ var ObjectStatCmd = &cmds.Command{ }, } +// ObjectPutCmd object put command var ObjectPutCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Store input as a DAG object, print its key.", @@ -433,6 +438,7 @@ And then run: Type: Object{}, } +// ObjectNewCmd object new command var ObjectNewCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Create a new object from an ipfs template.", diff --git a/core/commands/root.go b/core/commands/root.go index b2c73ef52dd..ff15cf4aedf 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -181,7 +181,7 @@ var rootROSubcommands = map[string]*cmds.Command{ "get": ocmd.ObjectGetCmd, "stat": ocmd.ObjectStatCmd, }, - }), + }, "dag": { Subcommands: map[string]*cmds.Command{ "get": dag.DagGetCmd, From e1d1f68f087443d915b3fd51c655f911ca450b3a Mon Sep 17 00:00:00 2001 From: Overbool Date: Tue, 30 Oct 2018 11:19:13 +0800 Subject: [PATCH 4/4] commands/object: use cmds.EmitOnce in single value License: MIT Signed-off-by: Overbool --- core/commands/object/diff.go | 2 +- core/commands/object/object.go | 10 +++++----- core/commands/object/patch.go | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/core/commands/object/diff.go b/core/commands/object/diff.go index 109fd79963c..840e1cb6423 100644 --- a/core/commands/object/diff.go +++ b/core/commands/object/diff.go @@ -94,7 +94,7 @@ Example: } } - return res.Emit(&Changes{out}) + return cmds.EmitOnce(res, &Changes{out}) }, Type: Changes{}, Encoders: cmds.EncoderMap{ diff --git a/core/commands/object/object.go b/core/commands/object/object.go index 4226bf41d3b..0eef518baca 100644 --- a/core/commands/object/object.go +++ b/core/commands/object/object.go @@ -148,7 +148,7 @@ multihash. Links: outLinks, } - return res.Emit(out) + return cmds.EmitOnce(res, out) }, Encoders: cmds.EncoderMap{ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { @@ -252,7 +252,7 @@ Supported values are: } } - return res.Emit(node) + return cmds.EmitOnce(res, node) }, Type: Node{}, Encoders: cmds.EncoderMap{ @@ -318,7 +318,7 @@ var ObjectStatCmd = &cmds.Command{ CumulativeSize: ns.CumulativeSize, } - return res.Emit(oldStat) + return cmds.EmitOnce(res, oldStat) }, Type: ipld.NodeStat{}, Encoders: cmds.EncoderMap{ @@ -419,7 +419,7 @@ And then run: return err } - return res.Emit(&Object{Hash: p.Cid().String()}) + return cmds.EmitOnce(res, &Object{Hash: p.Cid().String()}) }, Encoders: cmds.EncoderMap{ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { @@ -474,7 +474,7 @@ Available templates: return err } - return res.Emit(&Object{Hash: nd.Cid().String()}) + return cmds.EmitOnce(res, &Object{Hash: nd.Cid().String()}) }, Encoders: cmds.EncoderMap{ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *Object) error { diff --git a/core/commands/object/patch.go b/core/commands/object/patch.go index 60cca92d866..aefc745cde4 100644 --- a/core/commands/object/patch.go +++ b/core/commands/object/patch.go @@ -49,7 +49,7 @@ the limit will not be respected by the network. cmdkit.StringArg("root", true, false, "The hash of the node to modify."), cmdkit.FileArg("data", true, false, "Data to append.").EnableStdin(), }, - Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error { + Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { api, err := cmdenv.GetApi(env) if err != nil { return err @@ -70,7 +70,7 @@ the limit will not be respected by the network. return err } - return cmds.EmitOnce(re, &Object{Hash: p.Cid().String()}) + return cmds.EmitOnce(res, &Object{Hash: p.Cid().String()}) }, Type: &Object{}, Encoders: cmds.EncoderMap{ @@ -117,7 +117,7 @@ Example: return err } - return res.Emit(&Object{Hash: p.Cid().String()}) + return cmds.EmitOnce(res, &Object{Hash: p.Cid().String()}) }, Type: Object{}, Encoders: cmds.EncoderMap{ @@ -156,7 +156,7 @@ Remove a Merkle-link from the given object and return the hash of the result. return err } - return res.Emit(&Object{Hash: p.Cid().String()}) + return cmds.EmitOnce(res, &Object{Hash: p.Cid().String()}) }, Type: Object{}, Encoders: cmds.EncoderMap{ @@ -220,7 +220,7 @@ to a file containing 'bar', and returns the hash of the new object. return err } - return res.Emit(&Object{Hash: p.Cid().String()}) + return cmds.EmitOnce(res, &Object{Hash: p.Cid().String()}) }, Type: Object{}, Encoders: cmds.EncoderMap{