Skip to content

Commit 8f443a7

Browse files
whyrusleepingkeks
authored andcommitted
Merge pull request #3806 from ipfs/lgierth-patch-1
commands: improve name and key helptexts
2 parents 646dbde + 0e68ffa commit 8f443a7

File tree

11 files changed

+265
-158
lines changed

11 files changed

+265
-158
lines changed

cmd/ipfs/daemon.go

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"sort"
1212
"sync"
1313

14-
cmds "github.com/ipfs/go-ipfs/commands"
14+
"github.com/ipfs/go-ipfs-cmds/cmdsutil"
15+
16+
cmds "github.com/ipfs/go-ipfs-cmds"
1517
"github.com/ipfs/go-ipfs/core"
1618
commands "github.com/ipfs/go-ipfs/core/commands"
1719
corehttp "github.com/ipfs/go-ipfs/core/corehttp"
@@ -54,7 +56,7 @@ const (
5456
)
5557

5658
var daemonCmd = &cmds.Command{
57-
Helptext: cmds.HelpText{
59+
Helptext: cmdsutil.HelpText{
5860
Tagline: "Run a network-connected IPFS node.",
5961
ShortDescription: `
6062
'ipfs daemon' runs a persistent ipfs daemon that can serve commands
@@ -145,24 +147,25 @@ Headers.
145147
`,
146148
},
147149

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

182185
var fileDescriptorCheck = func() error { return nil }
183186

184-
func daemonFunc(req cmds.Request, res cmds.Response) {
187+
func daemonFunc(req cmds.Request, re cmds.ResponseEmitter) {
185188
// Inject metrics before we do anything
186189

187190
err := mprome.Inject()
@@ -221,7 +224,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
221224
// running in an uninitialized state.
222225
initialize, _, err := req.Option(initOptionKwd).Bool()
223226
if err != nil {
224-
res.SetError(err, cmds.ErrNormal)
227+
re.SetError(err, cmdsutil.ErrNormal)
225228
return
226229
}
227230

@@ -234,7 +237,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
234237
if !util.FileExists(req.InvocContext().ConfigRoot) {
235238
err := initWithDefaults(os.Stdout, req.InvocContext().ConfigRoot)
236239
if err != nil {
237-
res.SetError(err, cmds.ErrNormal)
240+
re.SetError(err, cmdsutil.ErrNormal)
238241
return
239242
}
240243
}
@@ -245,7 +248,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
245248
repo, err := fsrepo.Open(req.InvocContext().ConfigRoot)
246249
switch err {
247250
default:
248-
res.SetError(err, cmds.ErrNormal)
251+
re.SetError(err, cmdsutil.ErrNormal)
249252
return
250253
case fsrepo.ErrNeedMigration:
251254
domigrate, found, _ := req.Option(migrateKwd).Bool()
@@ -258,7 +261,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
258261
if !domigrate {
259262
fmt.Println("Not running migrations of fs-repo now.")
260263
fmt.Println("Please get fs-repo-migrations from https://dist.ipfs.io")
261-
res.SetError(fmt.Errorf("fs-repo requires migration"), cmds.ErrNormal)
264+
re.SetError(fmt.Errorf("fs-repo requires migration"), cmdsutil.ErrNormal)
262265
return
263266
}
264267

@@ -268,13 +271,13 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
268271
fmt.Printf(" %s\n", err)
269272
fmt.Println("If you think this is a bug, please file an issue and include this whole log output.")
270273
fmt.Println(" https://github.com/ipfs/fs-repo-migrations")
271-
res.SetError(err, cmds.ErrNormal)
274+
re.SetError(err, cmdsutil.ErrNormal)
272275
return
273276
}
274277

275278
repo, err = fsrepo.Open(req.InvocContext().ConfigRoot)
276279
if err != nil {
277-
res.SetError(err, cmds.ErrNormal)
280+
re.SetError(err, cmdsutil.ErrNormal)
278281
return
279282
}
280283
case nil:
@@ -283,7 +286,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
283286

284287
cfg, err := ctx.GetConfig()
285288
if err != nil {
286-
res.SetError(err, cmds.ErrNormal)
289+
re.SetError(err, cmdsutil.ErrNormal)
287290
return
288291
}
289292

@@ -305,14 +308,14 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
305308

306309
routingOption, _, err := req.Option(routingOptionKwd).String()
307310
if err != nil {
308-
res.SetError(err, cmds.ErrNormal)
311+
re.SetError(err, cmdsutil.ErrNormal)
309312
return
310313
}
311314
switch routingOption {
312315
case routingOptionSupernodeKwd:
313316
servers, err := cfg.SupernodeRouting.ServerIPFSAddrs()
314317
if err != nil {
315-
res.SetError(err, cmds.ErrNormal)
318+
re.SetError(err, cmdsutil.ErrNormal)
316319
repo.Close() // because ownership hasn't been transferred to the node
317320
return
318321
}
@@ -332,14 +335,14 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
332335
case routingOptionNoneKwd:
333336
ncfg.Routing = core.NilRouterOption
334337
default:
335-
res.SetError(fmt.Errorf("unrecognized routing option: %s", routingOption), cmds.ErrNormal)
338+
re.SetError(fmt.Errorf("unrecognized routing option: %s", routingOption), cmdsutil.ErrNormal)
336339
return
337340
}
338341

339342
node, err := core.NewNode(req.Context(), ncfg)
340343
if err != nil {
341344
log.Error("error from node construction: ", err)
342-
res.SetError(err, cmds.ErrNormal)
345+
re.SetError(err, cmdsutil.ErrNormal)
343346
return
344347
}
345348
node.SetLocal(false)
@@ -370,32 +373,43 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
370373
// construct api endpoint - every time
371374
err, apiErrc := serveHTTPApi(req)
372375
if err != nil {
373-
res.SetError(err, cmds.ErrNormal)
376+
re.SetError(err, cmdsutil.ErrNormal)
374377
return
375378
}
376379

380+
// construct http gateway - if it is set in the config
381+
var gwErrc <-chan error
382+
if len(cfg.Addresses.Gateway) > 0 {
383+
var err error
384+
err, gwErrc = serveHTTPGateway(req)
385+
if err != nil {
386+
re.SetError(err, cmdsutil.ErrNormal)
387+
return
388+
}
389+
}
390+
377391
// construct fuse mountpoints - if the user provided the --mount flag
378392
mount, _, err := req.Option(mountKwd).Bool()
379393
if err != nil {
380-
res.SetError(err, cmds.ErrNormal)
394+
re.SetError(err, cmdsutil.ErrNormal)
381395
return
382396
}
383397
if mount && offline {
384-
res.SetError(errors.New("mount is not currently supported in offline mode"),
385-
cmds.ErrClient)
398+
re.SetError(errors.New("mount is not currently supported in offline mode"),
399+
cmdsutil.ErrClient)
386400
return
387401
}
388402
if mount {
389403
if err := mountFuse(req); err != nil {
390-
res.SetError(err, cmds.ErrNormal)
404+
re.SetError(err, cmdsutil.ErrNormal)
391405
return
392406
}
393407
}
394408

395409
// repo blockstore GC - if --enable-gc flag is present
396410
err, gcErrc := maybeRunGC(req, node)
397411
if err != nil {
398-
res.SetError(err, cmds.ErrNormal)
412+
re.SetError(err, cmdsutil.ErrNormal)
399413
return
400414
}
401415

@@ -419,7 +433,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
419433
for err := range merge(apiErrc, gwErrc, gcErrc) {
420434
if err != nil {
421435
log.Error(err)
422-
res.SetError(err, cmds.ErrNormal)
436+
re.SetError(err, cmdsutil.ErrNormal)
423437
}
424438
}
425439
return

cmd/ipfs/init.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"errors"
67
"fmt"
78
"io"
89
"os"
910
"path"
1011

11-
context "context"
1212
assets "github.com/ipfs/go-ipfs/assets"
1313
cmds "github.com/ipfs/go-ipfs/commands"
1414
core "github.com/ipfs/go-ipfs/core"
1515
namesys "github.com/ipfs/go-ipfs/namesys"
1616
config "github.com/ipfs/go-ipfs/repo/config"
1717
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
18+
19+
"github.com/ipfs/go-ipfs-cmds/cmdsutil"
1820
)
1921

2022
const (
2123
nBitsForKeypairDefault = 2048
2224
)
2325

2426
var initCmd = &cmds.Command{
25-
Helptext: cmds.HelpText{
27+
Helptext: cmdsutil.HelpText{
2628
Tagline: "Initializes ipfs config file.",
2729
ShortDescription: `
2830
Initializes ipfs configuration files and generates a new keypair.
@@ -34,17 +36,17 @@ environment variable:
3436
export IPFS_PATH=/path/to/ipfsrepo
3537
`,
3638
},
37-
Arguments: []cmds.Argument{
38-
cmds.FileArg("default-config", false, false, "Initialize with the given configuration.").EnableStdin(),
39+
Arguments: []cmdsutil.Argument{
40+
cmdsutil.FileArg("default-config", false, false, "Initialize with the given configuration.").EnableStdin(),
3941
},
40-
Options: []cmds.Option{
41-
cmds.IntOption("bits", "b", "Number of bits to use in the generated RSA private key.").Default(nBitsForKeypairDefault),
42-
cmds.BoolOption("empty-repo", "e", "Don't add and pin help files to the local storage.").Default(false),
42+
Options: []cmdsutil.Option{
43+
cmdsutil.IntOption("bits", "b", "Number of bits to use in the generated RSA private key.").Default(nBitsForKeypairDefault),
44+
cmdsutil.BoolOption("empty-repo", "e", "Don't add and pin help files to the local storage.").Default(false),
4345

4446
// TODO need to decide whether to expose the override as a file or a
4547
// directory. That is: should we allow the user to also specify the
4648
// name of the file?
47-
// TODO cmds.StringOption("event-logs", "l", "Location for machine-readable event logs."),
49+
// TODO cmdsutil.StringOption("event-logs", "l", "Location for machine-readable event logs."),
4850
},
4951
PreRun: func(req cmds.Request) error {
5052
daemonLocked, err := fsrepo.LockedByOtherProcess(req.InvocContext().ConfigRoot)
@@ -63,19 +65,19 @@ environment variable:
6365
},
6466
Run: func(req cmds.Request, res cmds.Response) {
6567
if req.InvocContext().Online {
66-
res.SetError(errors.New("init must be run offline only!"), cmds.ErrNormal)
68+
res.SetError(errors.New("init must be run offline only!"), cmdsutil.ErrNormal)
6769
return
6870
}
6971

7072
empty, _, err := req.Option("e").Bool()
7173
if err != nil {
72-
res.SetError(err, cmds.ErrNormal)
74+
res.SetError(err, cmdsutil.ErrNormal)
7375
return
7476
}
7577

7678
nBitsForKeypair, _, err := req.Option("b").Int()
7779
if err != nil {
78-
res.SetError(err, cmds.ErrNormal)
80+
res.SetError(err, cmdsutil.ErrNormal)
7981
return
8082
}
8183

@@ -85,19 +87,19 @@ environment variable:
8587
if f != nil {
8688
confFile, err := f.NextFile()
8789
if err != nil {
88-
res.SetError(err, cmds.ErrNormal)
90+
res.SetError(err, cmdsutil.ErrNormal)
8991
return
9092
}
9193

9294
conf = &config.Config{}
9395
if err := json.NewDecoder(confFile).Decode(conf); err != nil {
94-
res.SetError(err, cmds.ErrNormal)
96+
res.SetError(err, cmdsutil.ErrNormal)
9597
return
9698
}
9799
}
98100

99101
if err := doInit(os.Stdout, req.InvocContext().ConfigRoot, empty, nBitsForKeypair, conf); err != nil {
100-
res.SetError(err, cmds.ErrNormal)
102+
res.SetError(err, cmdsutil.ErrNormal)
101103
return
102104
}
103105
},

cmd/ipfs/ipfs.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package main
33
import (
44
"fmt"
55

6-
cmds "github.com/ipfs/go-ipfs/commands"
6+
cmds "github.com/ipfs/go-ipfs-cmds"
7+
8+
oldcmds "github.com/ipfs/go-ipfs/commands"
79
commands "github.com/ipfs/go-ipfs/core/commands"
810
)
911

@@ -22,7 +24,7 @@ var commandsClientCmd = commands.CommandsCmd(Root)
2224
// They can override subcommands in commands.Root by defining a subcommand with the same name.
2325
var localCommands = map[string]*cmds.Command{
2426
"daemon": daemonCmd,
25-
"init": initCmd,
27+
"init": cmds.NewCommand(initCmd),
2628
"commands": commandsClientCmd,
2729
}
2830
var localMap = make(map[*cmds.Command]bool)
@@ -31,8 +33,14 @@ func init() {
3133
// setting here instead of in literal to prevent initialization loop
3234
// (some commands make references to Root)
3335
Root.Subcommands = localCommands
36+
Root.OldSubcommands = map[string]*oldcmds.Command{}
3437

3538
// copy all subcommands from commands.Root into this root (if they aren't already present)
39+
for k, v := range commands.Root.OldSubcommands {
40+
if _, found := Root.OldSubcommands[k]; !found {
41+
Root.OldSubcommands[k] = v
42+
}
43+
}
3644
for k, v := range commands.Root.Subcommands {
3745
if _, found := Root.Subcommands[k]; !found {
3846
Root.Subcommands[k] = v
@@ -95,17 +103,16 @@ func (d *cmdDetails) usesRepo() bool { return !d.doesNotUseRepo
95103
// not being able to run on all the same contexts. This map describes these
96104
// properties so that other code can make decisions about whether to invoke a
97105
// command or return an error to the user.
98-
var cmdDetailsMap = map[*cmds.Command]cmdDetails{
99-
initCmd: {doesNotUseConfigAsInput: true, cannotRunOnDaemon: true, doesNotUseRepo: true},
100-
101-
// daemonCmd allows user to initialize the config. Thus, it may be called
102-
// without using the config as input
103-
daemonCmd: {doesNotUseConfigAsInput: true, cannotRunOnDaemon: true},
104-
commandsClientCmd: {doesNotUseRepo: true},
105-
commands.CommandsDaemonCmd: {doesNotUseRepo: true},
106-
commands.VersionCmd: {doesNotUseConfigAsInput: true, doesNotUseRepo: true}, // must be permitted to run before init
107-
commands.LogCmd: {cannotRunOnClient: true},
108-
commands.ActiveReqsCmd: {cannotRunOnClient: true},
109-
commands.RepoFsckCmd: {cannotRunOnDaemon: true},
110-
commands.ConfigCmd.Subcommand("edit"): {cannotRunOnDaemon: true, doesNotUseRepo: true},
106+
//
107+
// TODO WIP keks : switch to paths as keys
108+
//var cmdDetailsMap = map[*cmds.Command]cmdDetails{
109+
var cmdDetailsMap = map[string]cmdDetails{
110+
"init": {doesNotUseConfigAsInput: true, cannotRunOnDaemon: true, doesNotUseRepo: true},
111+
"daemon": {doesNotUseConfigAsInput: true, cannotRunOnDaemon: true},
112+
"commands": {doesNotUseRepo: true},
113+
"version": {doesNotUseConfigAsInput: true, doesNotUseRepo: true}, // must be permitted to run before init
114+
"log": {cannotRunOnClient: true},
115+
"diag/cmds": {cannotRunOnClient: true},
116+
"repo/fsck": {cannotRunOnDaemon: true},
117+
"config/edit": {cannotRunOnDaemon: true, doesNotUseRepo: true},
111118
}

0 commit comments

Comments
 (0)