提交 204a5324 作者: Overbool

commands/refs: use new cmds

License: MIT
Signed-off-by: 's avatarOverbool <overbool.xu@gmail.com>
上级 887be024
...@@ -4,16 +4,19 @@ import ( ...@@ -4,16 +4,19 @@ import (
"bytes" "bytes"
"context" "context"
"errors" "errors"
"fmt"
"io" "io"
"strings" "strings"
cmds "github.com/ipfs/go-ipfs/commands" oldcmds "github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core" 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" e "github.com/ipfs/go-ipfs/core/commands/e"
cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid" cid "gx/ipfs/QmR8BauakNcBa3RbE4nbQu76PDiJgoQgz8AJdhJuiU4TAw/go-cid"
path "gx/ipfs/QmRG3XuGwT7GYuAqgWDJBKTzdaHMwAnc1x7J2KHEXNHxzG/go-path" path "gx/ipfs/QmRG3XuGwT7GYuAqgWDJBKTzdaHMwAnc1x7J2KHEXNHxzG/go-path"
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format" ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
cmds "gx/ipfs/QmSXUokcP4TJpFfqozT69AVAYRtzXVMUjzQVkYX41R9Svs/go-ipfs-cmds"
cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit" cmdkit "gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
) )
...@@ -31,7 +34,7 @@ const ( ...@@ -31,7 +34,7 @@ const (
) )
// KeyListTextMarshaler outputs a KeyList as plaintext, one key per line // KeyListTextMarshaler outputs a KeyList as plaintext, one key per line
func KeyListTextMarshaler(res cmds.Response) (io.Reader, error) { func KeyListTextMarshaler(res oldcmds.Response) (io.Reader, error) {
out, err := unwrapOutput(res.Output()) out, err := unwrapOutput(res.Output())
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -74,65 +77,37 @@ NOTE: List all references recursively by using the flag '-r'. ...@@ -74,65 +77,37 @@ NOTE: List all references recursively by using the flag '-r'.
cmdkit.BoolOption(refsRecursiveOptionName, "r", "Recursively list links of child nodes."), cmdkit.BoolOption(refsRecursiveOptionName, "r", "Recursively list links of child nodes."),
cmdkit.IntOption(refsMaxDepthOptionName, "Only for recursive refs, limits fetch and listing to the given depth").WithDefault(-1), cmdkit.IntOption(refsMaxDepthOptionName, "Only for recursive refs, limits fetch and listing to the given depth").WithDefault(-1),
}, },
Run: func(req cmds.Request, res cmds.Response) { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := req.Context() ctx := req.Context
n, err := req.InvocContext().GetNode() n, err := cmdenv.GetNode(env)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
unique, _, err := req.Option(refsUniqueOptionName).Bool() unique, _ := req.Options[refsUniqueOptionName].(bool)
if err != nil { recursive, _ := req.Options[refsRecursiveOptionName].(bool)
res.SetError(err, cmdkit.ErrNormal) maxDepth, _ := req.Options[refsMaxDepthOptionName].(int)
return edges, _ := req.Options[refsEdgesOptionName].(bool)
} format, _ := req.Options[refsFormatOptionName].(string)
recursive, _, err := req.Option(refsRecursiveOptionName).Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
maxDepth, _, err := req.Option(refsMaxDepthOptionName).Int()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
if !recursive { if !recursive {
maxDepth = 1 // write only direct refs maxDepth = 1 // write only direct refs
} }
format, _, err := req.Option(refsFormatOptionName).String()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
edges, _, err := req.Option(refsEdgesOptionName).Bool()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
if edges { if edges {
if format != "<dst>" { if format != "<dst>" {
res.SetError(errors.New("using format argument with edges is not allowed"), return errors.New("using format argument with edges is not allowed")
cmdkit.ErrClient)
return
} }
format = "<src> -> <dst>" format = "<src> -> <dst>"
} }
objs, err := objectsForPaths(ctx, n, req.Arguments()) objs, err := objectsForPaths(ctx, n, req.Arguments)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
out := make(chan interface{}) out := make(chan interface{})
res.SetOutput((<-chan interface{})(out))
go func() { go func() {
defer close(out) defer close(out)
...@@ -156,9 +131,20 @@ NOTE: List all references recursively by using the flag '-r'. ...@@ -156,9 +131,20 @@ 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
}),
}, },
Marshalers: refsMarshallerMap, Type: RefWrapper{},
Type: RefWrapper{},
} }
var RefsLocalCmd = &cmds.Command{ var RefsLocalCmd = &cmds.Command{
...@@ -169,23 +155,20 @@ Displays the hashes of all local objects. ...@@ -169,23 +155,20 @@ Displays the hashes of all local objects.
`, `,
}, },
Run: func(req cmds.Request, res cmds.Response) { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
ctx := req.Context() ctx := req.Context
n, err := req.InvocContext().GetNode() n, err := cmdenv.GetNode(env)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
// todo: make async // todo: make async
allKeys, err := n.Blockstore.AllKeysChan(ctx) allKeys, err := n.Blockstore.AllKeysChan(ctx)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
out := make(chan interface{}) out := make(chan interface{})
res.SetOutput((<-chan interface{})(out))
go func() { go func() {
defer close(out) defer close(out)
...@@ -193,18 +176,29 @@ Displays the hashes of all local objects. ...@@ -193,18 +176,29 @@ Displays the hashes of all local objects.
for k := range allKeys { for k := range allKeys {
select { select {
case out <- &RefWrapper{Ref: k.String()}: case out <- &RefWrapper{Ref: k.String()}:
case <-req.Context().Done(): case <-req.Context.Done():
return return
} }
} }
}() }()
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
}),
}, },
Marshalers: refsMarshallerMap, Type: RefWrapper{},
Type: RefWrapper{},
} }
var refsMarshallerMap = cmds.MarshalerMap{ var refsMarshallerMap = oldcmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) { cmds.Text: func(res oldcmds.Response) (io.Reader, error) {
v, err := unwrapOutput(res.Output()) v, err := unwrapOutput(res.Output())
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -138,7 +138,7 @@ var rootSubcommands = map[string]*cmds.Command{ ...@@ -138,7 +138,7 @@ var rootSubcommands = map[string]*cmds.Command{
"pin": lgc.NewCommand(PinCmd), "pin": lgc.NewCommand(PinCmd),
"ping": PingCmd, "ping": PingCmd,
"p2p": P2PCmd, "p2p": P2PCmd,
"refs": lgc.NewCommand(RefsCmd), "refs": RefsCmd,
"resolve": ResolveCmd, "resolve": ResolveCmd,
"swarm": SwarmCmd, "swarm": SwarmCmd,
"tar": TarCmd, "tar": TarCmd,
...@@ -155,7 +155,7 @@ var RootRO = &cmds.Command{} ...@@ -155,7 +155,7 @@ var RootRO = &cmds.Command{}
var CommandsDaemonROCmd = CommandsCmd(RootRO) var CommandsDaemonROCmd = CommandsCmd(RootRO)
var RefsROCmd = &oldcmds.Command{} var RefsROCmd = &cmds.Command{}
var rootROSubcommands = map[string]*cmds.Command{ var rootROSubcommands = map[string]*cmds.Command{
"commands": CommandsDaemonROCmd, "commands": CommandsDaemonROCmd,
...@@ -198,12 +198,12 @@ func init() { ...@@ -198,12 +198,12 @@ func init() {
// sanitize readonly refs command // sanitize readonly refs command
*RefsROCmd = *RefsCmd *RefsROCmd = *RefsCmd
RefsROCmd.Subcommands = map[string]*oldcmds.Command{} RefsROCmd.Subcommands = map[string]*cmds.Command{}
// this was in the big map definition above before, // this was in the big map definition above before,
// but if we leave it there lgc.NewCommand will be executed // but if we leave it there lgc.NewCommand will be executed
// before the value is updated (:/sanitize readonly refs command/) // before the value is updated (:/sanitize readonly refs command/)
rootROSubcommands["refs"] = lgc.NewCommand(RefsROCmd) rootROSubcommands["refs"] = RefsROCmd
Root.Subcommands = rootSubcommands Root.Subcommands = rootSubcommands
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论