Skip to content

Commit

Permalink
cmds/refs: remove redundant func
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 Nov 7, 2018
1 parent bd5be7e commit 30c6dd9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 78 deletions.
14 changes: 2 additions & 12 deletions core/commands/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,8 @@ 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
}),
},
Type: RefWrapper{},
Encoders: refsEncoderMap,
Type: RefWrapper{},
}

func getFilestore(env cmds.Environment) (*core.IpfsNode, *filestore.Filestore, error) {
Expand Down
88 changes: 22 additions & 66 deletions core/commands/refs.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.",
Expand All @@ -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 {
Expand Down Expand Up @@ -134,17 +129,8 @@ 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
}),
},
Type: RefWrapper{},
Encoders: refsEncoderMap,
Type: RefWrapper{},
}

var RefsLocalCmd = &cmds.Command{
Expand Down Expand Up @@ -177,37 +163,8 @@ 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
}),
},
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
},
Encoders: refsEncoderMap,
Type: RefWrapper{},
}

func objectsForPaths(ctx context.Context, n *core.IpfsNode, paths []string) ([]ipld.Node, error) {
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 30c6dd9

Please sign in to comment.