refactor(commands/id): use new command
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
Overbool committed
Oct 26, 2018 at 01:11 UTC
2fe969a26a5cb8f51918a61670f7d7799693848c
2 files changed
+32
-47
core/commands/id.go
+31
-46
@@ -1,18 +1,19 @@
1
package commands
2
3
import (
4
- "bytes"
4
"encoding/base64"
5
"encoding/json"
6
"errors"
7
+ "fmt"
8
"io"
9
"strings"
10
11
- cmds "github.com/ipfs/go-ipfs/commands"
11
core "github.com/ipfs/go-ipfs/core"
12
+ cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
13
e "github.com/ipfs/go-ipfs/core/commands/e"
14
15
ic "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
16
+ cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
17
"gx/ipfs/QmTRhk7cgjUf2gfQ3p2M9KPECNZEW9XUrmHcFCgog4cPgB/go-libp2p-peer"
18
pstore "gx/ipfs/QmTTJcDL3gsnGDALjh2fDGg1onGRUdVgNL2hU2WEZcVrMX/go-libp2p-peerstore"
19
identify "gx/ipfs/QmUDTcnDp2WssbmiDLC6aYurUeyt7QeRakHUQMxA2mZ5iB/go-libp2p/p2p/protocol/identify"
@@ -66,74 +67,58 @@ EXAMPLE:
67
Options: []cmdkit.Option{
68
cmdkit.StringOption(formatOptionName, "f", "Optional output format."),
69
},
69
- Run: func(req cmds.Request, res cmds.Response) {
70
- node, err := req.InvocContext().GetNode()
70
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
71
+ n, err := cmdenv.GetNode(env)
72
if err != nil {
72
- res.SetError(err, cmdkit.ErrNormal)
73
- return
73
+ return err
74
}
75
76
var id peer.ID
77
- if len(req.Arguments()) > 0 {
77
+ if len(req.Arguments) > 0 {
78
var err error
79
- id, err = peer.IDB58Decode(req.Arguments()[0])
79
+ id, err = peer.IDB58Decode(req.Arguments[0])
80
if err != nil {
81
- res.SetError(cmds.ClientError("Invalid peer id"), cmdkit.ErrClient)
82
- return
81
+ return fmt.Errorf("invalid peer id")
82
}
83
} else {
85
- id = node.Identity
84
+ id = n.Identity
85
}
86
88
- if id == node.Identity {
89
- output, err := printSelf(node)
87
+ if id == n.Identity {
88
+ output, err := printSelf(n)
89
if err != nil {
91
- res.SetError(err, cmdkit.ErrNormal)
92
- return
90
+ return err
91
}
94
- res.SetOutput(output)
95
- return
92
+ return cmds.EmitOnce(res, output)
93
}
94
95
// TODO handle offline mode with polymorphism instead of conditionals
99
- if !node.OnlineMode() {
100
- res.SetError(errors.New(offlineIdErrorMessage), cmdkit.ErrClient)
101
- return
96
+ if !n.OnlineMode() {
97
+ return errors.New(offlineIdErrorMessage)
98
}
99
104
- p, err := node.Routing.FindPeer(req.Context(), id)
100
+ p, err := n.Routing.FindPeer(req.Context, id)
101
if err == kb.ErrLookupFailure {
106
- res.SetError(errors.New(offlineIdErrorMessage), cmdkit.ErrClient)
107
- return
102
+ return errors.New(offlineIdErrorMessage)
103
}
104
if err != nil {
110
- res.SetError(err, cmdkit.ErrNormal)
111
- return
105
+ return err
106
}
107
114
- output, err := printPeer(node.Peerstore, p.ID)
108
+ output, err := printPeer(n.Peerstore, p.ID)
109
if err != nil {
116
- res.SetError(err, cmdkit.ErrNormal)
117
- return
110
+ return err
111
}
119
- res.SetOutput(output)
112
+ return cmds.EmitOnce(res, output)
113
},
121
- Marshalers: cmds.MarshalerMap{
122
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
123
- v, err := unwrapOutput(res.Output())
124
- if err != nil {
125
- return nil, err
126
- }
127
-
114
+ Encoders: cmds.EncoderMap{
115
+ cmds.Text: cmds.MakeEncoder(func(req *cmds.Request, w io.Writer, v interface{}) error {
116
val, ok := v.(*IdOutput)
117
if !ok {
130
- return nil, e.TypeErr(val, v)
118
+ return e.TypeErr(val, v)
119
}
120
133
- format, found, err := res.Request().Option(formatOptionName).String()
134
- if err != nil {
135
- return nil, err
136
- }
121
+ format, found := req.Options[formatOptionName].(string)
122
if found {
123
output := format
124
output = strings.Replace(output, "<id>", val.ID, -1)
@@ -143,17 +128,17 @@ EXAMPLE:
128
output = strings.Replace(output, "<addrs>", strings.Join(val.Addresses, "\n"), -1)
129
output = strings.Replace(output, "\\n", "\n", -1)
130
output = strings.Replace(output, "\\t", "\t", -1)
146
- return strings.NewReader(output), nil
131
+ fmt.Fprint(w, output)
132
} else {
148
-
133
marshaled, err := json.MarshalIndent(val, "", "\t")
134
if err != nil {
151
- return nil, err
135
+ return err
136
}
137
marshaled = append(marshaled, byte('\n'))
154
- return bytes.NewReader(marshaled), nil
138
+ fmt.Fprintln(w, string(marshaled))
139
}
156
- },
140
+ return nil
141
+ }),
142
},
143
Type: IdOutput{},
144
}
core/commands/root.go
+1
-1
@@ -128,7 +128,7 @@ var rootSubcommands = map[string]*cmds.Command{
128
"dht": lgc.NewCommand(DhtCmd),
129
"diag": lgc.NewCommand(DiagCmd),
130
"dns": lgc.NewCommand(DNSCmd),
131
- "id": lgc.NewCommand(IDCmd),
131
+ "id": IDCmd,
132
"key": KeyCmd,
133
"log": lgc.NewCommand(LogCmd),
134
"ls": lgc.NewCommand(LsCmd),