Skip to content

Commit

Permalink
refactor shouldRun to be easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed May 13, 2023
1 parent 6bd2986 commit aea3d90
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ Each command type supports the following options:
- `no_auto`: if set to `true` the command will not be executed automatically, but can be executed manually using the `--only` flag.
- `local`: if set to `true` the command will be executed on the local host (the one running the `spot` command) instead of the remote host(s).
- `sudo`: if set to `true` the command will be executed with `sudo` privileges.
- `only_on`: optional, allows to set a list of host names or addresses where the command will be executed. If not set, the command will be executed on all hosts.
- `only_on`: optional, allows to set a list of host names or addresses where the command will be executed. If not set, the command will be executed on all hosts. For example, `only_on: [host1, host2]` will execute command on `host1` and `host2` only. This option also supports reversed condition, so if user wants to execute command on all hosts except some, `!` prefix can be used. For example, `only_on: [!host1, !host2]` will execute command on all hosts except `host1` and `host2`.

example setting `ignore_errors`, `no_auto` and `only_on` options:

Expand Down
27 changes: 7 additions & 20 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,30 +252,17 @@ func (p *Process) shouldRunCmd(onlyOn []string, hostName, hostAddr string) bool
return true
}

isExcluded := func(host string) bool {
return strings.HasPrefix(host, "!") &&
(hostName == strings.TrimPrefix(host, "!") || hostAddr == strings.TrimPrefix(host, "!"))
}

isIncluded := func(host string) bool {
return !strings.HasPrefix(host, "!") && (hostName == host || hostAddr == host)
}

for _, host := range onlyOn {
if isExcluded(host) {
return false
if strings.HasPrefix(host, "!") { // exclude host
if hostName == host[1:] || hostAddr == host[1:] {
return false
}
continue
}
if isIncluded(host) {
if hostName == host || hostAddr == host { // include host
return true
}
}

// Default to running if there were no inclusions.
for _, host := range onlyOn {
if !strings.HasPrefix(host, "!") {
return false
}
}

return true
return false
}

0 comments on commit aea3d90

Please sign in to comment.