forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 14
/
ipfs.go
113 lines (96 loc) · 4.28 KB
/
ipfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
cmds "github.com/ipfs/go-ipfs/commands"
commands "github.com/ipfs/go-ipfs/core/commands"
)
// This is the CLI root, used for executing commands accessible to CLI clients.
// Some subcommands (like 'ipfs daemon' or 'ipfs init') are only accessible here,
// and can't be called through the HTTP API.
var Root = &cmds.Command{
Options: commands.Root.Options,
Helptext: commands.Root.Helptext,
}
// commandsClientCmd is the "ipfs commands" command for local cli
var commandsClientCmd = commands.CommandsCmd(Root)
// Commands in localCommands should always be run locally (even if daemon is running).
// They can override subcommands in commands.Root by defining a subcommand with the same name.
var localCommands = map[string]*cmds.Command{
"daemon": daemonCmd,
"init": initCmd,
"tour": tourCmd,
"commands": commandsClientCmd,
}
var localMap = make(map[*cmds.Command]bool)
func init() {
// setting here instead of in literal to prevent initialization loop
// (some commands make references to Root)
Root.Subcommands = localCommands
// copy all subcommands from commands.Root into this root (if they aren't already present)
for k, v := range commands.Root.Subcommands {
if _, found := Root.Subcommands[k]; !found {
Root.Subcommands[k] = v
}
}
for _, v := range localCommands {
localMap[v] = true
}
}
// isLocal returns true if the command should only be run locally (not sent to daemon), otherwise false
func isLocal(cmd *cmds.Command) bool {
_, found := localMap[cmd]
return found
}
// NB: when necessary, properties are described using negatives in order to
// provide desirable defaults
type cmdDetails struct {
cannotRunOnClient bool
cannotRunOnDaemon bool
doesNotUseRepo bool
// doesNotUseConfigAsInput describes commands that do not use the config as
// input. These commands either initialize the config or perform operations
// that don't require access to the config.
//
// pre-command hooks that require configs must not be run before these
// commands.
doesNotUseConfigAsInput bool
// preemptsAutoUpdate describes commands that must be executed without the
// auto-update pre-command hook
preemptsAutoUpdate bool
}
func (d *cmdDetails) String() string {
return fmt.Sprintf("on client? %t, on daemon? %t, uses repo? %t",
d.canRunOnClient(), d.canRunOnDaemon(), d.usesRepo())
}
func (d *cmdDetails) Loggable() map[string]interface{} {
return map[string]interface{}{
"canRunOnClient": d.canRunOnClient(),
"canRunOnDaemon": d.canRunOnDaemon(),
"preemptsAutoUpdate": d.preemptsAutoUpdate,
"usesConfigAsInput": d.usesConfigAsInput(),
"usesRepo": d.usesRepo(),
}
}
func (d *cmdDetails) usesConfigAsInput() bool { return !d.doesNotUseConfigAsInput }
func (d *cmdDetails) doesNotPreemptAutoUpdate() bool { return !d.preemptsAutoUpdate }
func (d *cmdDetails) canRunOnClient() bool { return !d.cannotRunOnClient }
func (d *cmdDetails) canRunOnDaemon() bool { return !d.cannotRunOnDaemon }
func (d *cmdDetails) usesRepo() bool { return !d.doesNotUseRepo }
// "What is this madness!?" you ask. Our commands have the unfortunate problem of
// not being able to run on all the same contexts. This map describes these
// properties so that other code can make decisions about whether to invoke a
// command or return an error to the user.
var cmdDetailsMap = map[*cmds.Command]cmdDetails{
initCmd: cmdDetails{doesNotUseConfigAsInput: true, cannotRunOnDaemon: true, doesNotUseRepo: true},
// daemonCmd allows user to initialize the config. Thus, it may be called
// without using the config as input
daemonCmd: cmdDetails{doesNotUseConfigAsInput: true, cannotRunOnDaemon: true},
commandsClientCmd: cmdDetails{doesNotUseRepo: true},
commands.CommandsDaemonCmd: cmdDetails{doesNotUseRepo: true},
commands.DiagCmd: cmdDetails{cannotRunOnClient: true},
commands.VersionCmd: cmdDetails{doesNotUseConfigAsInput: true, doesNotUseRepo: true}, // must be permitted to run before init
commands.UpdateCmd: cmdDetails{preemptsAutoUpdate: true, cannotRunOnDaemon: true},
commands.UpdateCheckCmd: cmdDetails{preemptsAutoUpdate: true},
commands.UpdateLogCmd: cmdDetails{preemptsAutoUpdate: true},
commands.LogCmd: cmdDetails{cannotRunOnClient: true},
}