diff --git a/core/commands/object/diff.go b/core/commands/object/diff.go index f200e10dade..840e1cb6423 100644 --- a/core/commands/object/diff.go +++ b/core/commands/object/diff.go @@ -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 } @@ -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 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 + }), }, } diff --git a/core/commands/object/object.go b/core/commands/object/object.go index e0e0a3b93af..0eef518baca 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,19 @@ 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{ +// ObjectDataCmd object data command +var ObjectDataCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Output the raw bytes of an IPFS object.", ShortDescription: ` @@ -81,30 +76,28 @@ 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{ +// ObjectLinksCmd object links command +var ObjectLinksCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Output the links pointed to by the specified object.", ShortDescription: ` @@ -120,35 +113,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 +148,28 @@ multihash. Links: outLinks, } - res.SetOutput(out) + return cmds.EmitOnce(res, 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{ +// ObjectGetCmd object get command +var ObjectGetCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Get and serialize the DAG node named by .", ShortDescription: ` @@ -229,47 +203,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 +252,30 @@ Supported values are: } } - res.SetOutput(node) + return cmds.EmitOnce(res, 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{ +// ObjectStatCmd object stat command +var ObjectStatCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Get stats for the DAG node named by .", ShortDescription: ` @@ -333,23 +293,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 +318,27 @@ var ObjectStatCmd = &oldcmds.Command{ CumulativeSize: ns.CumulativeSize, } - res.SetOutput(oldStat) + return cmds.EmitOnce(res, 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{ +// ObjectPutCmd object put command +var ObjectPutCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Store input as a DAG object, print its key.", ShortDescription: ` @@ -438,73 +385,61 @@ 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 cmds.EmitOnce(res, &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{ +// ObjectNewCmd object new command +var ObjectNewCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "Create a new object from an ipfs template.", ShortDescription: ` @@ -523,40 +458,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 cmds.EmitOnce(res, &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 +518,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..aefc745cde4 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.", @@ -67,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 @@ -88,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{ @@ -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 cmds.EmitOnce(res, &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 cmds.EmitOnce(res, &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 cmds.EmitOnce(res, &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..ff15cf4aedf 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -174,14 +174,14 @@ 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, "stat": ocmd.ObjectStatCmd, }, - }), + }, "dag": { Subcommands: map[string]*cmds.Command{ "get": dag.DagGetCmd,