refactor(cmds): use new cmds lib in config
License: MIT Signed-off-by: Overbool <overbool.xu@gmail.com>
Overbool committed
Oct 26, 2018 at 22:52 UTC
b1efb997bfa95bacf8920498346d1ba1b4b2d869
4 files changed
+77
-143
core/commands/active.go
+1
-1
@@ -34,7 +34,7 @@ Lists running and recently run commands.
34
"clear": clearInactiveCmd,
35
"set-time": setRequestClearCmd,
36
},
37
- Encoders: cmds.EncoderMap {
37
+ Encoders: cmds.EncoderMap{
38
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *[]*cmds.ReqLogEntry) error {
39
verbose, _ := req.Options[verboseOptionName].(bool)
40
core/commands/config.go
+74
-140
@@ -1,7 +1,6 @@
1
package commands
2
3
import (
4
- "bytes"
4
"encoding/json"
5
"errors"
6
"fmt"
@@ -11,11 +10,11 @@ import (
10
"os/exec"
11
"strings"
12
14
- cmds "github.com/ipfs/go-ipfs/commands"
15
- e "github.com/ipfs/go-ipfs/core/commands/e"
13
+ oldcmds "github.com/ipfs/go-ipfs/commands"
14
repo "github.com/ipfs/go-ipfs/repo"
15
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
16
17
+ cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
18
"gx/ipfs/QmP2i47tnU23ijdshrZtuvrSkQPtf9HhsMb9fwGVe8owj2/jsondiff"
19
config "gx/ipfs/QmbK4EmM2Xx5fmbqK38TGP3PpY66r3tkXLZTcc7dF9mFwM/go-ipfs-config"
20
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
@@ -60,7 +59,12 @@ Set the value of the 'Datastore.Path' key:
59
$ ipfs config Datastore.Path ~/.ipfs/datastore
60
`,
61
},
63
-
62
+ Subcommands: map[string]*cmds.Command{
63
+ "show": configShowCmd,
64
+ "edit": configEditCmd,
65
+ "replace": configReplaceCmd,
66
+ "profile": configProfileCmd,
67
+ },
68
Arguments: []cmdkit.Argument{
69
cmdkit.StringArg("key", true, false, "The key of the config entry (e.g. \"Addresses.API\")."),
70
cmdkit.StringArg("value", false, false, "The value to set the config entry to."),
@@ -69,46 +73,37 @@ Set the value of the 'Datastore.Path' key:
73
cmdkit.BoolOption(configBoolOptionName, "Set a boolean value."),
74
cmdkit.BoolOption(configJSONOptionName, "Parse stringified JSON."),
75
},
72
- Run: func(req cmds.Request, res cmds.Response) {
73
- args := req.Arguments()
76
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
77
+ args := req.Arguments
78
key := args[0]
79
80
var output *ConfigField
77
- defer func() {
78
- if output != nil {
79
- res.SetOutput(output)
80
- } else {
81
- res.SetOutput(nil)
82
- }
83
- }()
81
82
// This is a temporary fix until we move the private key out of the config file
83
switch strings.ToLower(key) {
84
case "identity", "identity.privkey":
88
- res.SetError(fmt.Errorf("cannot show or change private key through API"), cmdkit.ErrNormal)
89
- return
85
+ return fmt.Errorf("cannot show or change private key through API")
86
default:
87
}
88
93
- r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
89
+ ctx := env.(*oldcmds.Context)
90
+ r, err := fsrepo.Open(ctx.ConfigRoot)
91
if err != nil {
95
- res.SetError(err, cmdkit.ErrNormal)
96
- return
92
+ return err
93
}
94
defer r.Close()
95
if len(args) == 2 {
96
value := args[1]
97
102
- if parseJSON, _, _ := req.Option(configJSONOptionName).Bool(); parseJSON {
98
+ if parseJSON, _ := req.Options[configJSONOptionName].(bool); parseJSON {
99
var jsonVal interface{}
100
if err := json.Unmarshal([]byte(value), &jsonVal); err != nil {
101
err = fmt.Errorf("failed to unmarshal json. %s", err)
106
- res.SetError(err, cmdkit.ErrNormal)
107
- return
102
+ return err
103
}
104
105
output, err = setConfig(r, key, jsonVal)
111
- } else if isbool, _, _ := req.Option(configBoolOptionName).Bool(); isbool {
106
+ } else if isbool, _ := req.Options[configBoolOptionName].(bool); isbool {
107
output, err = setConfig(r, key, value == "true")
108
} else {
109
output, err = setConfig(r, key, value)
@@ -116,46 +111,28 @@ Set the value of the 'Datastore.Path' key:
111
} else {
112
output, err = getConfig(r, key)
113
}
114
+
115
if err != nil {
120
- res.SetError(err, cmdkit.ErrNormal)
121
- return
116
+ return err
117
}
123
- },
124
- Marshalers: cmds.MarshalerMap{
125
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
126
- if len(res.Request().Arguments()) == 2 {
127
- return nil, nil // dont output anything
128
- }
129
-
130
- if res.Error() != nil {
131
- return nil, res.Error()
132
- }
133
-
134
- v, err := unwrapOutput(res.Output())
135
- if err != nil {
136
- return nil, err
137
- }
118
139
- vf, ok := v.(*ConfigField)
140
- if !ok {
141
- return nil, e.TypeErr(vf, v)
119
+ return res.Emit(output)
120
+ },
121
+ Encoders: cmds.EncoderMap{
122
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ConfigField) error {
123
+ if len(req.Arguments) == 2 {
124
+ return nil
125
}
126
144
- buf, err := config.HumanOutput(vf.Value)
127
+ buf, err := config.HumanOutput(out.Value)
128
if err != nil {
146
- return nil, err
129
+ return err
130
}
148
- buf = append(buf, byte('\n'))
149
- return bytes.NewReader(buf), nil
150
- },
131
+ fmt.Fprintln(w, string(buf))
132
+ return nil
133
+ }),
134
},
135
Type: ConfigField{},
153
- Subcommands: map[string]*cmds.Command{
154
- "show": configShowCmd,
155
- "edit": configEditCmd,
156
- "replace": configReplaceCmd,
157
- "profile": configProfileCmd,
158
- },
136
}
137
138
var configShowCmd = &cmds.Command{
@@ -166,57 +143,41 @@ NOTE: For security reasons, this command will omit your private key. If you woul
143
`,
144
},
145
Type: map[string]interface{}{},
169
- Run: func(req cmds.Request, res cmds.Response) {
170
- cfgPath := req.InvocContext().ConfigRoot
146
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
147
+ ctx := env.(*oldcmds.Context)
148
+ cfgPath := ctx.ConfigRoot
149
fname, err := config.Filename(cfgPath)
150
if err != nil {
173
- res.SetError(err, cmdkit.ErrNormal)
174
- return
151
+ return err
152
}
153
154
data, err := ioutil.ReadFile(fname)
155
if err != nil {
179
- res.SetError(err, cmdkit.ErrNormal)
180
- return
156
+ return err
157
}
158
159
var cfg map[string]interface{}
160
err = json.Unmarshal(data, &cfg)
161
if err != nil {
186
- res.SetError(err, cmdkit.ErrNormal)
187
- return
162
+ return err
163
}
164
165
err = scrubValue(cfg, []string{config.IdentityTag, config.PrivKeyTag})
166
if err != nil {
192
- res.SetError(err, cmdkit.ErrNormal)
193
- return
167
+ return err
168
}
195
- res.SetOutput(&cfg)
196
- },
197
- Marshalers: cmds.MarshalerMap{
198
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
199
- if res.Error() != nil {
200
- return nil, res.Error()
201
- }
169
203
- v, err := unwrapOutput(res.Output())
204
- if err != nil {
205
- return nil, err
206
- }
207
-
208
- cfg, ok := v.(*map[string]interface{})
209
- if !ok {
210
- return nil, e.TypeErr(cfg, v)
211
- }
212
-
213
- buf, err := config.HumanOutput(cfg)
170
+ return res.Emit(&cfg)
171
+ },
172
+ Encoders: cmds.EncoderMap{
173
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *map[string]interface{}) error {
174
+ buf, err := config.HumanOutput(out)
175
if err != nil {
215
- return nil, err
176
+ return err
177
}
217
- buf = append(buf, byte('\n'))
218
- return bytes.NewReader(buf), nil
219
- },
178
+ fmt.Fprintln(w, string(buf))
179
+ return nil
180
+ }),
181
},
182
}
183
@@ -270,17 +231,14 @@ variable set to your preferred text editor.
231
`,
232
},
233
273
- Run: func(req cmds.Request, res cmds.Response) {
274
- filename, err := config.Filename(req.InvocContext().ConfigRoot)
234
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
235
+ ctx := env.(*oldcmds.Context)
236
+ filename, err := config.Filename(ctx.ConfigRoot)
237
if err != nil {
276
- res.SetError(err, cmdkit.ErrNormal)
277
- return
238
+ return err
239
}
240
280
- err = editConfig(filename)
281
- if err != nil {
282
- res.SetError(err, cmdkit.ErrNormal)
283
- }
241
+ return editConfig(filename)
242
},
243
}
244
@@ -296,29 +254,21 @@ can't be undone.
254
Arguments: []cmdkit.Argument{
255
cmdkit.FileArg("file", true, false, "The file to use as the new config."),
256
},
299
- Run: func(req cmds.Request, res cmds.Response) {
300
- // has to be called
301
- res.SetOutput(nil)
302
-
303
- r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
257
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
258
+ ctx := env.(*oldcmds.Context)
259
+ r, err := fsrepo.Open(ctx.ConfigRoot)
260
if err != nil {
305
- res.SetError(err, cmdkit.ErrNormal)
306
- return
261
+ return err
262
}
263
defer r.Close()
264
310
- file, err := req.Files().NextFile()
265
+ file, err := req.Files.NextFile()
266
if err != nil {
312
- res.SetError(err, cmdkit.ErrNormal)
313
- return
267
+ return err
268
}
269
defer file.Close()
270
317
- err = replaceConfig(r, file)
318
- if err != nil {
319
- res.SetError(err, cmdkit.ErrNormal)
320
- return
321
- }
271
+ return replaceConfig(r, file)
272
},
273
}
274
@@ -346,58 +296,42 @@ var configProfileApplyCmd = &cmds.Command{
296
Arguments: []cmdkit.Argument{
297
cmdkit.StringArg("profile", true, false, "The profile to apply to the config."),
298
},
349
- Run: func(req cmds.Request, res cmds.Response) {
350
- profile, ok := config.Profiles[req.Arguments()[0]]
299
+ Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
300
+ profile, ok := config.Profiles[req.Arguments[0]]
301
if !ok {
352
- res.SetError(fmt.Errorf("%s is not a profile", req.Arguments()[0]), cmdkit.ErrNormal)
353
- return
302
+ return fmt.Errorf("%s is not a profile", req.Arguments[0])
303
}
304
356
- dryRun, _, _ := req.Option("dry-run").Bool()
357
- oldCfg, newCfg, err := transformConfig(req.InvocContext().ConfigRoot, req.Arguments()[0], profile.Transform, dryRun)
305
+ dryRun, _ := req.Options["dry-run"].(bool)
306
+ ctx := env.(*oldcmds.Context)
307
+ oldCfg, newCfg, err := transformConfig(ctx.ConfigRoot, req.Arguments[0], profile.Transform, dryRun)
308
if err != nil {
359
- res.SetError(err, cmdkit.ErrNormal)
360
- return
309
+ return err
310
}
311
312
oldCfgMap, err := scrubPrivKey(oldCfg)
313
if err != nil {
365
- res.SetError(err, cmdkit.ErrNormal)
366
- return
314
+ return err
315
}
316
317
newCfgMap, err := scrubPrivKey(newCfg)
318
if err != nil {
371
- res.SetError(err, cmdkit.ErrNormal)
372
- return
319
+ return err
320
}
321
375
- res.SetOutput(&ConfigUpdateOutput{
322
+ return res.Emit(&ConfigUpdateOutput{
323
OldCfg: oldCfgMap,
324
NewCfg: newCfgMap,
325
})
326
},
380
- Marshalers: cmds.MarshalerMap{
381
- cmds.Text: func(res cmds.Response) (io.Reader, error) {
382
- if res.Error() != nil {
383
- return nil, res.Error()
384
- }
385
-
386
- v, err := unwrapOutput(res.Output())
387
- if err != nil {
388
- return nil, err
389
- }
390
-
391
- apply, ok := v.(*ConfigUpdateOutput)
392
- if !ok {
393
- return nil, e.TypeErr(apply, v)
394
- }
395
-
396
- diff := jsondiff.Compare(apply.OldCfg, apply.NewCfg)
327
+ Encoders: cmds.EncoderMap{
328
+ cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ConfigUpdateOutput) error {
329
+ diff := jsondiff.Compare(out.OldCfg, out.NewCfg)
330
buf := jsondiff.Format(diff)
331
399
- return strings.NewReader(string(buf)), nil
400
- },
332
+ fmt.Fprint(w, string(buf))
333
+ return nil
334
+ }),
335
},
336
Type: ConfigUpdateOutput{},
337
}
core/commands/root.go
+1
-1
@@ -123,7 +123,7 @@ var rootSubcommands = map[string]*cmds.Command{
123
"repo": RepoCmd,
124
"stats": StatsCmd,
125
"bootstrap": lgc.NewCommand(BootstrapCmd),
126
- "config": lgc.NewCommand(ConfigCmd),
126
+ "config": ConfigCmd,
127
"dag": dag.DagCmd,
128
"dht": DhtCmd,
129
"diag": DiagCmd,
core/commands/sysdiag.go
+1
-1
@@ -5,8 +5,8 @@ import (
5
"path"
6
"runtime"
7
8
- cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
8
version "github.com/ipfs/go-ipfs"
9
+ cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
10
11
cmds "gx/ipfs/QmRRovo1DE6i5cMjCbf19mQCSuszF6SKwdZNUMS7MtBnH1/go-ipfs-cmds"
12
manet "gx/ipfs/QmQVUtnrNGtCRkCMpXgpApfzQjc8FDaDVxHqWH8cnZQeh5/go-multiaddr-net"