1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-05-30 06:27:58 +08:00

refactor(commands/id): use new command

License: MIT
Signed-off-by: Overbool <overbool.xu@gmail.com>
This commit is contained in:
Overbool
2018-10-26 01:11:11 +08:00
parent 7ecd6756b4
commit 2fe969a26a
2 changed files with 32 additions and 47 deletions

View File

@ -1,18 +1,19 @@
package commands package commands
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io" "io"
"strings" "strings"
cmds "github.com/ipfs/go-ipfs/commands"
core "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"
ic "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto" ic "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
"gx/ipfs/QmTRhk7cgjUf2gfQ3p2M9KPECNZEW9XUrmHcFCgog4cPgB/go-libp2p-peer" "gx/ipfs/QmTRhk7cgjUf2gfQ3p2M9KPECNZEW9XUrmHcFCgog4cPgB/go-libp2p-peer"
pstore "gx/ipfs/QmTTJcDL3gsnGDALjh2fDGg1onGRUdVgNL2hU2WEZcVrMX/go-libp2p-peerstore" pstore "gx/ipfs/QmTTJcDL3gsnGDALjh2fDGg1onGRUdVgNL2hU2WEZcVrMX/go-libp2p-peerstore"
identify "gx/ipfs/QmUDTcnDp2WssbmiDLC6aYurUeyt7QeRakHUQMxA2mZ5iB/go-libp2p/p2p/protocol/identify" identify "gx/ipfs/QmUDTcnDp2WssbmiDLC6aYurUeyt7QeRakHUQMxA2mZ5iB/go-libp2p/p2p/protocol/identify"
@ -66,74 +67,58 @@ EXAMPLE:
Options: []cmdkit.Option{ Options: []cmdkit.Option{
cmdkit.StringOption(formatOptionName, "f", "Optional output format."), cmdkit.StringOption(formatOptionName, "f", "Optional output format."),
}, },
Run: func(req cmds.Request, res cmds.Response) { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
node, err := req.InvocContext().GetNode() n, err := cmdenv.GetNode(env)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
var id peer.ID var id peer.ID
if len(req.Arguments()) > 0 { if len(req.Arguments) > 0 {
var err error var err error
id, err = peer.IDB58Decode(req.Arguments()[0]) id, err = peer.IDB58Decode(req.Arguments[0])
if err != nil { if err != nil {
res.SetError(cmds.ClientError("Invalid peer id"), cmdkit.ErrClient) return fmt.Errorf("invalid peer id")
return
} }
} else { } else {
id = node.Identity id = n.Identity
} }
if id == node.Identity { if id == n.Identity {
output, err := printSelf(node) output, err := printSelf(n)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
res.SetOutput(output) return cmds.EmitOnce(res, output)
return
} }
// TODO handle offline mode with polymorphism instead of conditionals // TODO handle offline mode with polymorphism instead of conditionals
if !node.OnlineMode() { if !n.OnlineMode() {
res.SetError(errors.New(offlineIdErrorMessage), cmdkit.ErrClient) return errors.New(offlineIdErrorMessage)
return
} }
p, err := node.Routing.FindPeer(req.Context(), id) p, err := n.Routing.FindPeer(req.Context, id)
if err == kb.ErrLookupFailure { if err == kb.ErrLookupFailure {
res.SetError(errors.New(offlineIdErrorMessage), cmdkit.ErrClient) return errors.New(offlineIdErrorMessage)
return
} }
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
output, err := printPeer(node.Peerstore, p.ID) output, err := printPeer(n.Peerstore, p.ID)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) return err
return
} }
res.SetOutput(output) return cmds.EmitOnce(res, output)
}, },
Marshalers: cmds.MarshalerMap{ Encoders: cmds.EncoderMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) { cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
v, err := unwrapOutput(res.Output())
if err != nil {
return nil, err
}
val, ok := v.(*IdOutput) val, ok := v.(*IdOutput)
if !ok { if !ok {
return nil, e.TypeErr(val, v) return e.TypeErr(val, v)
} }
format, found, err := res.Request().Option(formatOptionName).String() format, found := req.Options[formatOptionName].(string)
if err != nil {
return nil, err
}
if found { if found {
output := format output := format
output = strings.Replace(output, "<id>", val.ID, -1) output = strings.Replace(output, "<id>", val.ID, -1)
@ -143,17 +128,17 @@ EXAMPLE:
output = strings.Replace(output, "<addrs>", strings.Join(val.Addresses, "\n"), -1) output = strings.Replace(output, "<addrs>", strings.Join(val.Addresses, "\n"), -1)
output = strings.Replace(output, "\\n", "\n", -1) output = strings.Replace(output, "\\n", "\n", -1)
output = strings.Replace(output, "\\t", "\t", -1) output = strings.Replace(output, "\\t", "\t", -1)
return strings.NewReader(output), nil fmt.Fprint(w, output)
} else { } else {
marshaled, err := json.MarshalIndent(val, "", "\t") marshaled, err := json.MarshalIndent(val, "", "\t")
if err != nil { if err != nil {
return nil, err return err
} }
marshaled = append(marshaled, byte('\n')) marshaled = append(marshaled, byte('\n'))
return bytes.NewReader(marshaled), nil fmt.Fprintln(w, string(marshaled))
} }
}, return nil
}),
}, },
Type: IdOutput{}, Type: IdOutput{},
} }

View File

@ -128,7 +128,7 @@ var rootSubcommands = map[string]*cmds.Command{
"dht": lgc.NewCommand(DhtCmd), "dht": lgc.NewCommand(DhtCmd),
"diag": lgc.NewCommand(DiagCmd), "diag": lgc.NewCommand(DiagCmd),
"dns": lgc.NewCommand(DNSCmd), "dns": lgc.NewCommand(DNSCmd),
"id": lgc.NewCommand(IDCmd), "id": IDCmd,
"key": KeyCmd, "key": KeyCmd,
"log": lgc.NewCommand(LogCmd), "log": lgc.NewCommand(LogCmd),
"ls": lgc.NewCommand(LsCmd), "ls": lgc.NewCommand(LsCmd),