Add WWbool.BoolDefaultTrue()

New support in WWbool for booleans that default "true."

Used primarily for NetDev.OnBoot.

Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
Jonathon Anderson
2025-02-19 18:01:17 -07:00
parent 9ae77cb5b9
commit fd8de8bfab
15 changed files with 62 additions and 27 deletions

View File

@@ -1,5 +1,15 @@
package wwtype
import (
"strings"
"github.com/warewulf/warewulf/internal/pkg/util"
)
func GetUnsetVerbs() []string {
return []string{"UNSET", "DELETE", "UNDEF", "undef", "--", "nil", "0.0.0.0"}
return []string{"unset", "delete", "undef", "--", "nil", "0.0.0.0"}
}
func IsUnsetVerb(value string) bool {
return util.InSlice(GetUnsetVerbs(), strings.ToLower(value))
}

View File

@@ -4,8 +4,6 @@ import (
"fmt"
"strconv"
"strings"
"github.com/warewulf/warewulf/internal/pkg/util"
)
// Simple string which can be converted to bool. Backend storage
@@ -16,17 +14,38 @@ type WWbool string
Transform the underlying string value to bool
*/
func (val WWbool) Bool() bool {
str := string(val)
if util.InSlice(GetUnsetVerbs(), str) {
str := strings.ToLower(string(val))
if IsUnsetVerb(str) {
return false
}
if strings.ToLower(str) == "yes" {
switch str {
case "yes":
return true
case "no", "":
return false
}
bval, err := strconv.ParseBool(str)
if err != nil {
return false
}
return bval
}
func (val WWbool) BoolDefaultTrue() bool {
str := strings.ToLower(string(val))
if IsUnsetVerb(str) {
return false
}
switch str {
case "yes", "":
return true
case "no":
return false
}
bval, err := strconv.ParseBool(str)
if err != nil {
return true
}
if strings.ToLower(str) == "no" {
return false
}
bval, _ := strconv.ParseBool(str)
return bval
}
@@ -34,7 +53,7 @@ func (val WWbool) Bool() bool {
Set the string, only accept bool values like true, false, but also UNDEF
*/
func (val *WWbool) Set(str string) error {
if util.InSlice(GetUnsetVerbs(), str) {
if IsUnsetVerb(str) {
// run the unset verb trough, will be filtered out later
*val = WWbool(str)
return nil