Commit 57689ffc authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Juan Batiz-Benet

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

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent 4678dea0
...@@ -51,18 +51,23 @@ func isLocal(cmd *cmds.Command) bool { ...@@ -51,18 +51,23 @@ func isLocal(cmd *cmds.Command) bool {
return found return found
} }
// NB: when necessary, properties are described using negatives in order to
// provide desirable defaults
type cmdDetails struct { type cmdDetails struct {
cannotRunOnClient bool cannotRunOnClient bool
cannotRunOnDaemon bool cannotRunOnDaemon bool
doesNotUseRepo bool doesNotUseRepo bool
// initializesConfig describes commands that initialize the config. // doesNotUseConfigAsInput describes commands that do not use the config as
// pre-command hooks that require configs must not be run before this // input. These commands either initialize the config or perform operations
// command // that don't require access to the config.
initializesConfig bool //
// pre-command hooks that require configs must not be run before these
// commands.
doesNotUseConfigAsInput bool
// preemptsAutoUpdate describes commands that must be executed without the // preemptsAutoUpdate describes commands that must be executed without the
// pre-command update hook // auto-update pre-command hook
preemptsAutoUpdate bool preemptsAutoUpdate bool
} }
...@@ -71,21 +76,22 @@ func (d *cmdDetails) String() string { ...@@ -71,21 +76,22 @@ func (d *cmdDetails) String() string {
d.canRunOnClient(), d.canRunOnDaemon(), d.usesRepo()) d.canRunOnClient(), d.canRunOnDaemon(), d.usesRepo())
} }
func (d *cmdDetails) canRunOnClient() bool { return !d.cannotRunOnClient } func (d *cmdDetails) usesConfigAsInput() bool { return !d.doesNotUseConfigAsInput }
func (d *cmdDetails) canRunOnDaemon() bool { return !d.cannotRunOnDaemon } func (d *cmdDetails) canRunOnClient() bool { return !d.cannotRunOnClient }
func (d *cmdDetails) usesRepo() bool { return !d.doesNotUseRepo } 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 // "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 // 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 // properties so that other code can make decisions about whether to invoke a
// command or return an error to the user. // command or return an error to the user.
var cmdDetailsMap = map[*cmds.Command]cmdDetails{ var cmdDetailsMap = map[*cmds.Command]cmdDetails{
initCmd: cmdDetails{initializesConfig: true, cannotRunOnDaemon: true, doesNotUseRepo: true}, initCmd: cmdDetails{doesNotUseConfigAsInput: true, cannotRunOnDaemon: true, doesNotUseRepo: true},
daemonCmd: cmdDetails{cannotRunOnDaemon: true}, daemonCmd: cmdDetails{cannotRunOnDaemon: true},
commandsClientCmd: cmdDetails{doesNotUseRepo: true}, commandsClientCmd: cmdDetails{doesNotUseRepo: true},
commands.CommandsDaemonCmd: cmdDetails{doesNotUseRepo: true}, commands.CommandsDaemonCmd: cmdDetails{doesNotUseRepo: true},
commands.DiagCmd: cmdDetails{cannotRunOnClient: true}, commands.DiagCmd: cmdDetails{cannotRunOnClient: true},
commands.VersionCmd: cmdDetails{doesNotUseRepo: true}, commands.VersionCmd: cmdDetails{doesNotUseConfigAsInput: true, doesNotUseRepo: true}, // must be permitted to run before init
commands.UpdateCmd: cmdDetails{preemptsAutoUpdate: true, cannotRunOnDaemon: true}, commands.UpdateCmd: cmdDetails{preemptsAutoUpdate: true, cannotRunOnDaemon: true},
commands.UpdateCheckCmd: cmdDetails{preemptsAutoUpdate: true}, commands.UpdateCheckCmd: cmdDetails{preemptsAutoUpdate: true},
commands.UpdateLogCmd: cmdDetails{preemptsAutoUpdate: true}, commands.UpdateLogCmd: cmdDetails{preemptsAutoUpdate: true},
......
...@@ -217,7 +217,7 @@ func callPreCommandHooks(details cmdDetails, req cmds.Request, root *cmds.Comman ...@@ -217,7 +217,7 @@ func callPreCommandHooks(details cmdDetails, req cmds.Request, root *cmds.Comman
// check for updates when 1) commands is going to be run locally, 2) the // check for updates when 1) commands is going to be run locally, 2) the
// command does not initialize the config, and 3) the command does not // command does not initialize the config, and 3) the command does not
// pre-empt updates // pre-empt updates
if !daemon && !details.initializesConfig && !details.preemptsAutoUpdate { if !daemon && details.usesConfigAsInput() && !details.preemptsAutoUpdate {
log.Debug("Calling hook: Check for updates") log.Debug("Calling hook: Check for updates")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment