diff --git a/core/commands/filestore.go b/core/commands/filestore.go index 70c02a3a41e0..25b2c24ba02e 100644 --- a/core/commands/filestore.go +++ b/core/commands/filestore.go @@ -203,17 +203,7 @@ var dupsFileStore = &cmds.Command{ return nil }, - Encoders: cmds.EncoderMap{ - cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RefWrapper) error { - if out.Err != "" { - return fmt.Errorf(out.Err) - } - - fmt.Fprintln(w, out.Ref) - - return nil - }), - }, + Encoders: refsEncoderMap, Type: RefWrapper{}, } diff --git a/core/commands/refs.go b/core/commands/refs.go index 5a85bed1f954..643c8f7f5dde 100644 --- a/core/commands/refs.go +++ b/core/commands/refs.go @@ -1,25 +1,33 @@ package commands import ( - "bytes" "context" "errors" "fmt" "io" "strings" - oldcmds "github.com/ipfs/go-ipfs/commands" core "github.com/ipfs/go-ipfs/core" cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" - e "github.com/ipfs/go-ipfs/core/commands/e" cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid" path "gx/ipfs/QmRG3XuGwT7GYuAqgWDJBKTzdaHMwAnc1x7J2KHEXNHxzG/go-path" + cmds "gx/ipfs/Qma6uuSyjkecGhMFFLfzyJDPyoDtNJSHJNweDccZhaWkgU/go-ipfs-cmds" ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format" - cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds" cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" ) +var refsEncoderMap = cmds.EncoderMap{ + cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RefWrapper) error { + if out.Err != "" { + return fmt.Errorf(out.Err) + } + fmt.Fprintln(w, out.Ref) + + return nil + }), +} + // KeyList is a general type for outputting lists of keys type KeyList struct { Keys []cid.Cid @@ -33,25 +41,7 @@ const ( refsMaxDepthOptionName = "max-depth" ) -// KeyListTextMarshaler outputs a KeyList as plaintext, one key per line -func KeyListTextMarshaler(res oldcmds.Response) (io.Reader, error) { - out, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - - output, ok := out.(*KeyList) - if !ok { - return nil, e.TypeErr(output, out) - } - - buf := new(bytes.Buffer) - for _, key := range output.Keys { - buf.WriteString(key.String() + "\n") - } - return buf, nil -} - +// RefsCmd is the `ipfs refs` command var RefsCmd = &cmds.Command{ Helptext: cmdkit.HelpText{ Tagline: "List links (references) from an object.", @@ -78,6 +68,11 @@ NOTE: List all references recursively by using the flag '-r'. cmdkit.IntOption(refsMaxDepthOptionName, "Only for recursive refs, limits fetch and listing to the given depth").WithDefault(-1), }, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { + err := req.ParseBodyArgs() + if err != nil { + return err + } + ctx := req.Context n, err := cmdenv.GetNode(env) if err != nil { @@ -134,16 +129,7 @@ NOTE: List all references recursively by using the flag '-r'. return res.Emit(out) }, - Encoders: cmds.EncoderMap{ - cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RefWrapper) error { - if out.Err != "" { - return fmt.Errorf(out.Err) - } - fmt.Fprintln(w, out.Ref) - - return nil - }), - }, + Encoders: refsEncoderMap, Type: RefWrapper{}, } @@ -177,39 +163,10 @@ Displays the hashes of all local objects. return nil }, - Encoders: cmds.EncoderMap{ - cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *RefWrapper) error { - if out.Err != "" { - return fmt.Errorf(out.Err) - } - fmt.Fprintln(w, out.Ref) - - return nil - }), - }, + Encoders: refsEncoderMap, Type: RefWrapper{}, } -var refsMarshallerMap = oldcmds.MarshalerMap{ - cmds.Text: func(res oldcmds.Response) (io.Reader, error) { - v, err := unwrapOutput(res.Output()) - if err != nil { - return nil, err - } - - obj, ok := v.(*RefWrapper) - if !ok { - return nil, e.TypeErr(obj, v) - } - - if obj.Err != "" { - return nil, errors.New(obj.Err) - } - - return strings.NewReader(obj.Ref + "\n"), nil - }, -} - func objectsForPaths(ctx context.Context, n *core.IpfsNode, paths []string) ([]ipld.Node, error) { objects := make([]ipld.Node, len(paths)) for i, sp := range paths { @@ -247,7 +204,6 @@ type RefWriter struct { // WriteRefs writes refs of the given object to the underlying writer. func (rw *RefWriter) WriteRefs(n ipld.Node) (int, error) { return rw.writeRefsRecursive(n, 0) - } func (rw *RefWriter) writeRefsRecursive(n ipld.Node, depth int) (int, error) { @@ -384,3 +340,4 @@ func (rw *RefWriter) WriteEdge(from, to cid.Cid, linkname string) error { rw.out <- &RefWrapper{Ref: s} return nil } + diff --git a/core/commands/root.go b/core/commands/root.go index 4c0834e9d039..62f2edfabcca 100644 --- a/core/commands/root.go +++ b/core/commands/root.go @@ -155,6 +155,7 @@ var RootRO = &cmds.Command{} var CommandsDaemonROCmd = CommandsCmd(RootRO) +// RefsROCmd is `ipfs refs` command var RefsROCmd = &cmds.Command{} var rootROSubcommands = map[string]*cmds.Command{