Skip to content

Commit

Permalink
fleetctl: support truthy values in boolean unit options
Browse files Browse the repository at this point in the history
Support truthy values in boolean unit options(yes, on, 1, t).

Originally written by wuqixuan <wuqixuan@huawei.com>
Supersedes coreos#1271
Fixes: coreos#1108
  • Loading branch information
Dongsu Park committed Apr 29, 2016
1 parent da56590 commit 9bb303d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (u *Unit) IsGlobal() bool {
}
// Last value found wins
last := values[len(values)-1]
return strings.ToLower(last) == "true"
return isTruthyValue(last)
}

// NewJob creates a new Job based on the given name and Unit.
Expand Down Expand Up @@ -301,3 +301,10 @@ func unitPrintf(s string, nu unit.UnitNameInfo) (out string) {
out = strings.Replace(out, "%i", nu.Instance, -1)
return
}

// isTruthyValue returns true if a given string is any of "truthy" value,
// i.e. "true", "yes", "1", "on", or "t".
func isTruthyValue(astr string) bool {
chl := strings.ToLower(astr)
return chl == "true" || chl == "yes" || chl == "1" || chl == "on" || chl == "t"
}
5 changes: 5 additions & 0 deletions job/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ func TestUnitIsGlobal(t *testing.T) {
// correct specifications
{"[X-Fleet]\nMachineOf=foo\nGlobal=true", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=True", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=Yes", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=On", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=1", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=t", true},
{"[X-Fleet]\nMachineOf=foo\nGlobal=12", false},
// multiple parameters - last wins
{"[X-Fleet]\nGlobal=true\nGlobal=false", false},
{"[X-Fleet]\nGlobal=false\nGlobal=true", true},
Expand Down

0 comments on commit 9bb303d

Please sign in to comment.