Skip to content

Commit 0c20d0a

Browse files
kekskeks
authored and
keks
committed
cmd: use go-ipfs-cmds
License: MIT Signed-off-by: keks <[email protected]>
1 parent fda7dd1 commit 0c20d0a

File tree

119 files changed

+2912
-3331
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+2912
-3331
lines changed

blocks/blockstore/caching.go

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55

66
context "context"
7+
78
"gx/ipfs/QmRg1gKTHzc3CZXSKzem8aR4E3TubFhbgXwfVuWnSK5CC5/go-metrics-interface"
89
)
910

blocks/blockstore/util/remove.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,17 @@ func FilterPinned(pins pin.Pinner, out chan<- interface{}, cids []*cid.Cid) []*c
8585
return stillOkay
8686
}
8787

88-
// ProcRmOutput takes the channel returned by RmBlocks and writes
89-
// to stdout/stderr according to the RemovedBlock objects received in
90-
// that channel.
91-
func ProcRmOutput(in <-chan interface{}, sout io.Writer, serr io.Writer) error {
88+
// ProcRmOutput takes a function which returns a result from RmBlocks or EOF if there is no input.
89+
// It then writes to stdout/stderr according to the RemovedBlock object returned from the function.
90+
func ProcRmOutput(next func() (interface{}, error), sout io.Writer, serr io.Writer) error {
9291
someFailed := false
93-
for res := range in {
92+
for {
93+
res, err := next()
94+
if err == io.EOF {
95+
break
96+
} else if err != nil {
97+
return err
98+
}
9499
r := res.(*RemovedBlock)
95100
if r.Hash == "" && r.Error != "" {
96101
return fmt.Errorf("aborted: %s", r.Error)

cmd/ipfs/daemon.go

+41-39
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"sort"
1212
"sync"
1313

14-
cmds "github.com/ipfs/go-ipfs/commands"
1514
"github.com/ipfs/go-ipfs/core"
1615
commands "github.com/ipfs/go-ipfs/core/commands"
1716
corehttp "github.com/ipfs/go-ipfs/core/corehttp"
@@ -20,6 +19,8 @@ import (
2019
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
2120
migrate "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
2221

22+
cmds "gx/ipfs/QmQVvuDwXUGbtYmbmTcbLtGRYXnEbymaR2zEj38GVysqWe/go-ipfs-cmds"
23+
"gx/ipfs/QmSNbH2A1evCCbJSDC6u3RV3GGDhgu6pRGbXHvrN89tMKf/go-ipfs-cmdkit"
2324
mprome "gx/ipfs/QmSk46nSD78YiuNojYMS8NW6hSCjH95JajqqzzoychZgef/go-metrics-prometheus"
2425
"gx/ipfs/QmX3QZ5jHEPidwUrymXV1iSCSUhdGxj15sm2gP4jKMef7B/client_golang/prometheus"
2526
"gx/ipfs/QmX3U3YXCQ6UYBxq2LVWF8dARS1hPUTEYLrSx654Qyxyw6/go-multiaddr-net"
@@ -51,7 +52,7 @@ const (
5152
)
5253

5354
var daemonCmd = &cmds.Command{
54-
Helptext: cmds.HelpText{
55+
Helptext: cmdkit.HelpText{
5556
Tagline: "Run a network-connected IPFS node.",
5657
ShortDescription: `
5758
'ipfs daemon' runs a persistent ipfs daemon that can serve commands
@@ -142,24 +143,25 @@ Headers.
142143
`,
143144
},
144145

145-
Options: []cmds.Option{
146-
cmds.BoolOption(initOptionKwd, "Initialize ipfs with default settings if not already initialized").Default(false),
147-
cmds.StringOption(routingOptionKwd, "Overrides the routing option").Default("dht"),
148-
cmds.BoolOption(mountKwd, "Mounts IPFS to the filesystem").Default(false),
149-
cmds.BoolOption(writableKwd, "Enable writing objects (with POST, PUT and DELETE)").Default(false),
150-
cmds.StringOption(ipfsMountKwd, "Path to the mountpoint for IPFS (if using --mount). Defaults to config setting."),
151-
cmds.StringOption(ipnsMountKwd, "Path to the mountpoint for IPNS (if using --mount). Defaults to config setting."),
152-
cmds.BoolOption(unrestrictedApiAccessKwd, "Allow API access to unlisted hashes").Default(false),
153-
cmds.BoolOption(unencryptTransportKwd, "Disable transport encryption (for debugging protocols)").Default(false),
154-
cmds.BoolOption(enableGCKwd, "Enable automatic periodic repo garbage collection").Default(false),
155-
cmds.BoolOption(adjustFDLimitKwd, "Check and raise file descriptor limits if needed").Default(true),
156-
cmds.BoolOption(offlineKwd, "Run offline. Do not connect to the rest of the network but provide local API.").Default(false),
157-
cmds.BoolOption(migrateKwd, "If true, assume yes at the migrate prompt. If false, assume no."),
158-
cmds.BoolOption(enableFloodSubKwd, "Instantiate the ipfs daemon with the experimental pubsub feature enabled."),
159-
cmds.BoolOption(enableMultiplexKwd, "Add the experimental 'go-multiplex' stream muxer to libp2p on construction.").Default(true),
146+
Options: []cmdkit.Option{
147+
cmdkit.BoolOption(initOptionKwd, "Initialize ipfs with default settings if not already initialized").Default(false),
148+
cmdkit.StringOption(routingOptionKwd, "Overrides the routing option").Default("dht"),
149+
cmdkit.BoolOption(mountKwd, "Mounts IPFS to the filesystem").Default(false),
150+
cmdkit.BoolOption(writableKwd, "Enable writing objects (with POST, PUT and DELETE)").Default(false),
151+
cmdkit.StringOption(ipfsMountKwd, "Path to the mountpoint for IPFS (if using --mount). Defaults to config setting."),
152+
cmdkit.StringOption(ipnsMountKwd, "Path to the mountpoint for IPNS (if using --mount). Defaults to config setting."),
153+
cmdkit.BoolOption(unrestrictedApiAccessKwd, "Allow API access to unlisted hashes").Default(false),
154+
cmdkit.BoolOption(unencryptTransportKwd, "Disable transport encryption (for debugging protocols)").Default(false),
155+
cmdkit.BoolOption(enableGCKwd, "Enable automatic periodic repo garbage collection").Default(false),
156+
cmdkit.BoolOption(adjustFDLimitKwd, "Check and raise file descriptor limits if needed").Default(true),
157+
cmdkit.BoolOption(offlineKwd, "Run offline. Do not connect to the rest of the network but provide local API.").Default(false),
158+
cmdkit.BoolOption(migrateKwd, "If true, assume yes at the migrate prompt. If false, assume no."),
159+
cmdkit.BoolOption(enableFloodSubKwd, "Instantiate the ipfs daemon with the experimental pubsub feature enabled."),
160+
cmdkit.BoolOption(enableMultiplexKwd, "Add the experimental 'go-multiplex' stream muxer to libp2p on construction.").Default(true),
161+
160162
// TODO: add way to override addresses. tricky part: updating the config if also --init.
161-
// cmds.StringOption(apiAddrKwd, "Address for the daemon rpc API (overrides config)"),
162-
// cmds.StringOption(swarmAddrKwd, "Address for the swarm socket (overrides config)"),
163+
// cmdkit.StringOption(apiAddrKwd, "Address for the daemon rpc API (overrides config)"),
164+
// cmdkit.StringOption(swarmAddrKwd, "Address for the swarm socket (overrides config)"),
163165
},
164166
Subcommands: map[string]*cmds.Command{},
165167
Run: daemonFunc,
@@ -178,7 +180,7 @@ func defaultMux(path string) corehttp.ServeOption {
178180

179181
var fileDescriptorCheck = func() error { return nil }
180182

181-
func daemonFunc(req cmds.Request, res cmds.Response) {
183+
func daemonFunc(req cmds.Request, re cmds.ResponseEmitter) {
182184
// Inject metrics before we do anything
183185

184186
err := mprome.Inject()
@@ -216,7 +218,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
216218
// running in an uninitialized state.
217219
initialize, _, err := req.Option(initOptionKwd).Bool()
218220
if err != nil {
219-
res.SetError(err, cmds.ErrNormal)
221+
re.SetError(err, cmdkit.ErrNormal)
220222
return
221223
}
222224

@@ -226,7 +228,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
226228
if !fsrepo.IsInitialized(cfg) {
227229
err := initWithDefaults(os.Stdout, cfg)
228230
if err != nil {
229-
res.SetError(err, cmds.ErrNormal)
231+
re.SetError(err, cmdkit.ErrNormal)
230232
return
231233
}
232234
}
@@ -237,7 +239,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
237239
repo, err := fsrepo.Open(ctx.ConfigRoot)
238240
switch err {
239241
default:
240-
res.SetError(err, cmds.ErrNormal)
242+
re.SetError(err, cmdkit.ErrNormal)
241243
return
242244
case fsrepo.ErrNeedMigration:
243245
domigrate, found, _ := req.Option(migrateKwd).Bool()
@@ -250,7 +252,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
250252
if !domigrate {
251253
fmt.Println("Not running migrations of fs-repo now.")
252254
fmt.Println("Please get fs-repo-migrations from https://dist.ipfs.io")
253-
res.SetError(fmt.Errorf("fs-repo requires migration"), cmds.ErrNormal)
255+
re.SetError(fmt.Errorf("fs-repo requires migration"), cmdkit.ErrNormal)
254256
return
255257
}
256258

@@ -260,13 +262,13 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
260262
fmt.Printf(" %s\n", err)
261263
fmt.Println("If you think this is a bug, please file an issue and include this whole log output.")
262264
fmt.Println(" https://github.com/ipfs/fs-repo-migrations")
263-
res.SetError(err, cmds.ErrNormal)
265+
re.SetError(err, cmdkit.ErrNormal)
264266
return
265267
}
266268

267269
repo, err = fsrepo.Open(ctx.ConfigRoot)
268270
if err != nil {
269-
res.SetError(err, cmds.ErrNormal)
271+
re.SetError(err, cmdkit.ErrNormal)
270272
return
271273
}
272274
case nil:
@@ -275,7 +277,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
275277

276278
cfg, err := ctx.GetConfig()
277279
if err != nil {
278-
res.SetError(err, cmds.ErrNormal)
280+
re.SetError(err, cmdkit.ErrNormal)
279281
return
280282
}
281283

@@ -297,12 +299,12 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
297299

298300
routingOption, _, err := req.Option(routingOptionKwd).String()
299301
if err != nil {
300-
res.SetError(err, cmds.ErrNormal)
302+
re.SetError(err, cmdkit.ErrNormal)
301303
return
302304
}
303305
switch routingOption {
304306
case routingOptionSupernodeKwd:
305-
res.SetError(errors.New("supernode routing was never fully implemented and has been removed"), cmds.ErrNormal)
307+
re.SetError(errors.New("supernode routing was never fully implemented and has been removed"), cmdkit.ErrNormal)
306308
return
307309
case routingOptionDHTClientKwd:
308310
ncfg.Routing = core.DHTClientOption
@@ -311,14 +313,14 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
311313
case routingOptionNoneKwd:
312314
ncfg.Routing = core.NilRouterOption
313315
default:
314-
res.SetError(fmt.Errorf("unrecognized routing option: %s", routingOption), cmds.ErrNormal)
316+
re.SetError(fmt.Errorf("unrecognized routing option: %s", routingOption), cmdkit.ErrNormal)
315317
return
316318
}
317319

318320
node, err := core.NewNode(req.Context(), ncfg)
319321
if err != nil {
320322
log.Error("error from node construction: ", err)
321-
res.SetError(err, cmds.ErrNormal)
323+
re.SetError(err, cmdkit.ErrNormal)
322324
return
323325
}
324326
node.SetLocal(false)
@@ -349,32 +351,32 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
349351
// construct api endpoint - every time
350352
err, apiErrc := serveHTTPApi(req)
351353
if err != nil {
352-
res.SetError(err, cmds.ErrNormal)
354+
re.SetError(err, cmdkit.ErrNormal)
353355
return
354356
}
355357

356358
// construct fuse mountpoints - if the user provided the --mount flag
357359
mount, _, err := req.Option(mountKwd).Bool()
358360
if err != nil {
359-
res.SetError(err, cmds.ErrNormal)
361+
re.SetError(err, cmdkit.ErrNormal)
360362
return
361363
}
362364
if mount && offline {
363-
res.SetError(errors.New("mount is not currently supported in offline mode"),
364-
cmds.ErrClient)
365+
re.SetError(errors.New("mount is not currently supported in offline mode"),
366+
cmdkit.ErrClient)
365367
return
366368
}
367369
if mount {
368370
if err := mountFuse(req); err != nil {
369-
res.SetError(err, cmds.ErrNormal)
371+
re.SetError(err, cmdkit.ErrNormal)
370372
return
371373
}
372374
}
373375

374376
// repo blockstore GC - if --enable-gc flag is present
375377
err, gcErrc := maybeRunGC(req, node)
376378
if err != nil {
377-
res.SetError(err, cmds.ErrNormal)
379+
re.SetError(err, cmdkit.ErrNormal)
378380
return
379381
}
380382

@@ -384,7 +386,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
384386
var err error
385387
err, gwErrc = serveHTTPGateway(req)
386388
if err != nil {
387-
res.SetError(err, cmds.ErrNormal)
389+
re.SetError(err, cmdkit.ErrNormal)
388390
return
389391
}
390392
}
@@ -398,7 +400,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
398400
for err := range merge(apiErrc, gwErrc, gcErrc) {
399401
if err != nil {
400402
log.Error(err)
401-
res.SetError(err, cmds.ErrNormal)
403+
re.SetError(err, cmdkit.ErrNormal)
402404
}
403405
}
404406
}

cmd/ipfs/init.go

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"errors"
67
"fmt"
@@ -9,21 +10,22 @@ import (
910
"path"
1011
"strings"
1112

12-
context "context"
1313
assets "github.com/ipfs/go-ipfs/assets"
1414
cmds "github.com/ipfs/go-ipfs/commands"
1515
core "github.com/ipfs/go-ipfs/core"
1616
namesys "github.com/ipfs/go-ipfs/namesys"
1717
config "github.com/ipfs/go-ipfs/repo/config"
1818
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
19+
20+
"gx/ipfs/QmSNbH2A1evCCbJSDC6u3RV3GGDhgu6pRGbXHvrN89tMKf/go-ipfs-cmdkit"
1921
)
2022

2123
const (
2224
nBitsForKeypairDefault = 2048
2325
)
2426

2527
var initCmd = &cmds.Command{
26-
Helptext: cmds.HelpText{
28+
Helptext: cmdkit.HelpText{
2729
Tagline: "Initializes ipfs config file.",
2830
ShortDescription: `
2931
Initializes ipfs configuration files and generates a new keypair.
@@ -44,18 +46,18 @@ environment variable:
4446
export IPFS_PATH=/path/to/ipfsrepo
4547
`,
4648
},
47-
Arguments: []cmds.Argument{
48-
cmds.FileArg("default-config", false, false, "Initialize with the given configuration.").EnableStdin(),
49+
Arguments: []cmdkit.Argument{
50+
cmdkit.FileArg("default-config", false, false, "Initialize with the given configuration.").EnableStdin(),
4951
},
50-
Options: []cmds.Option{
51-
cmds.IntOption("bits", "b", "Number of bits to use in the generated RSA private key.").Default(nBitsForKeypairDefault),
52-
cmds.BoolOption("empty-repo", "e", "Don't add and pin help files to the local storage.").Default(false),
53-
cmds.StringOption("profile", "p", "Apply profile settings to config. Multiple profiles can be separated by ','"),
52+
Options: []cmdkit.Option{
53+
cmdkit.IntOption("bits", "b", "Number of bits to use in the generated RSA private key.").Default(nBitsForKeypairDefault),
54+
cmdkit.BoolOption("empty-repo", "e", "Don't add and pin help files to the local storage.").Default(false),
55+
cmdkit.StringOption("profile", "p", "Apply profile settings to config. Multiple profiles can be separated by ','"),
5456

5557
// TODO need to decide whether to expose the override as a file or a
5658
// directory. That is: should we allow the user to also specify the
5759
// name of the file?
58-
// TODO cmds.StringOption("event-logs", "l", "Location for machine-readable event logs."),
60+
// TODO cmdkit.StringOption("event-logs", "l", "Location for machine-readable event logs."),
5961
},
6062
PreRun: func(req cmds.Request) error {
6163
daemonLocked, err := fsrepo.LockedByOtherProcess(req.InvocContext().ConfigRoot)
@@ -73,20 +75,23 @@ environment variable:
7375
return nil
7476
},
7577
Run: func(req cmds.Request, res cmds.Response) {
78+
// needs to be called at least once
79+
res.SetOutput(nil)
80+
7681
if req.InvocContext().Online {
77-
res.SetError(errors.New("init must be run offline only!"), cmds.ErrNormal)
82+
res.SetError(errors.New("init must be run offline only!"), cmdkit.ErrNormal)
7883
return
7984
}
8085

8186
empty, _, err := req.Option("e").Bool()
8287
if err != nil {
83-
res.SetError(err, cmds.ErrNormal)
88+
res.SetError(err, cmdkit.ErrNormal)
8489
return
8590
}
8691

8792
nBitsForKeypair, _, err := req.Option("b").Int()
8893
if err != nil {
89-
res.SetError(err, cmds.ErrNormal)
94+
res.SetError(err, cmdkit.ErrNormal)
9095
return
9196
}
9297

@@ -96,20 +101,20 @@ environment variable:
96101
if f != nil {
97102
confFile, err := f.NextFile()
98103
if err != nil {
99-
res.SetError(err, cmds.ErrNormal)
104+
res.SetError(err, cmdkit.ErrNormal)
100105
return
101106
}
102107

103108
conf = &config.Config{}
104109
if err := json.NewDecoder(confFile).Decode(conf); err != nil {
105-
res.SetError(err, cmds.ErrNormal)
110+
res.SetError(err, cmdkit.ErrNormal)
106111
return
107112
}
108113
}
109114

110115
profile, _, err := req.Option("profile").String()
111116
if err != nil {
112-
res.SetError(err, cmds.ErrNormal)
117+
res.SetError(err, cmdkit.ErrNormal)
113118
return
114119
}
115120

@@ -119,7 +124,7 @@ environment variable:
119124
}
120125

121126
if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair, profiles, conf); err != nil {
122-
res.SetError(err, cmds.ErrNormal)
127+
res.SetError(err, cmdkit.ErrNormal)
123128
return
124129
}
125130
},

0 commit comments

Comments
 (0)