@cryptotaxi247 / kubo / commits / 57689ffc2

fix(2/version) make it possible to execute before init

License: MIT Signed-off-by: Brian Tiger Chow <brian@perfmode.com>

Brian Tiger Chow committed Nov 15, 2014 at 05:44 UTC 57689ffc2b7f76a0c50958a474f4a9348ffc2651
2 files changed +17 -11
cmd/ipfs2/ipfs.go
+16 -10
@@ -51,18 +51,23 @@ func isLocal(cmd *cmds.Command) bool {
51 return found
52 }
53
54 +// NB: when necessary, properties are described using negatives in order to
55 +// provide desirable defaults
56 type cmdDetails struct {
57 cannotRunOnClient bool
58 cannotRunOnDaemon bool
59 doesNotUseRepo bool
60
59 - // initializesConfig describes commands that initialize the config.
60 - // pre-command hooks that require configs must not be run before this
61 - // command
62 - initializesConfig bool
61 + // doesNotUseConfigAsInput describes commands that do not use the config as
62 + // input. These commands either initialize the config or perform operations
63 + // that don't require access to the config.
64 + //
65 + // pre-command hooks that require configs must not be run before these
66 + // commands.
67 + doesNotUseConfigAsInput bool
68
69 // preemptsAutoUpdate describes commands that must be executed without the
65 - // pre-command update hook
70 + // auto-update pre-command hook
71 preemptsAutoUpdate bool
72 }
73
@@ -71,21 +76,22 @@ func (d *cmdDetails) String() string {
76 d.canRunOnClient(), d.canRunOnDaemon(), d.usesRepo())
77 }
78
74 -func (d *cmdDetails) canRunOnClient() bool { return !d.cannotRunOnClient }
75 -func (d *cmdDetails) canRunOnDaemon() bool { return !d.cannotRunOnDaemon }
76 -func (d *cmdDetails) usesRepo() bool { return !d.doesNotUseRepo }
79 +func (d *cmdDetails) usesConfigAsInput() bool { return !d.doesNotUseConfigAsInput }
80 +func (d *cmdDetails) canRunOnClient() bool { return !d.cannotRunOnClient }
81 +func (d *cmdDetails) canRunOnDaemon() bool { return !d.cannotRunOnDaemon }
82 +func (d *cmdDetails) usesRepo() bool { return !d.doesNotUseRepo }
83
84 // "What is this madness!?" you ask. Our commands have the unfortunate problem of
85 // not being able to run on all the same contexts. This map describes these
86 // properties so that other code can make decisions about whether to invoke a
87 // command or return an error to the user.
88 var cmdDetailsMap = map[*cmds.Command]cmdDetails{
83 - initCmd: cmdDetails{initializesConfig: true, cannotRunOnDaemon: true, doesNotUseRepo: true},
89 + initCmd: cmdDetails{doesNotUseConfigAsInput: true, cannotRunOnDaemon: true, doesNotUseRepo: true},
90 daemonCmd: cmdDetails{cannotRunOnDaemon: true},
91 commandsClientCmd: cmdDetails{doesNotUseRepo: true},
92 commands.CommandsDaemonCmd: cmdDetails{doesNotUseRepo: true},
93 commands.DiagCmd: cmdDetails{cannotRunOnClient: true},
88 - commands.VersionCmd: cmdDetails{doesNotUseRepo: true},
94 + commands.VersionCmd: cmdDetails{doesNotUseConfigAsInput: true, doesNotUseRepo: true}, // must be permitted to run before init
95 commands.UpdateCmd: cmdDetails{preemptsAutoUpdate: true, cannotRunOnDaemon: true},
96 commands.UpdateCheckCmd: cmdDetails{preemptsAutoUpdate: true},
97 commands.UpdateLogCmd: cmdDetails{preemptsAutoUpdate: true},
cmd/ipfs2/main.go
+1 -1
@@ -217,7 +217,7 @@ func callPreCommandHooks(details cmdDetails, req cmds.Request, root *cmds.Comman
217 // check for updates when 1) commands is going to be run locally, 2) the
218 // command does not initialize the config, and 3) the command does not
219 // pre-empt updates
220 - if !daemon && !details.initializesConfig && !details.preemptsAutoUpdate {
220 + if !daemon && details.usesConfigAsInput() && !details.preemptsAutoUpdate {
221
222 log.Debug("Calling hook: Check for updates")
223