util: add parse fallback helpers (#20022)

util/def: add def.Bool and def.Duration default parse helpers

Replace multiple instances of def.Bool and def.Duration with a new util/def
package.

Updates #20018

Co-authored-by: Bobby <boby@codelabs.co.id>
Co-authored-by: Simon Law <sfllaw@tailscale.com>
Signed-off-by: Bobby <boby@codelabs.co.id>
Signed-off-by: Simon Law <sfllaw@tailscale.com>
This commit is contained in:
Bobi Gunardi
2026-06-15 15:58:51 -07:00
committed by GitHub
co-authored by Bobby Simon Law
parent 94fbb03352
commit ca20611d11
11 changed files with 170 additions and 52 deletions
+2 -13
View File
@@ -11,7 +11,6 @@ import (
"net"
"net/http"
"net/netip"
"strconv"
"strings"
"sync"
"time"
@@ -20,6 +19,7 @@ import (
"tailscale.com/net/netmon"
"tailscale.com/net/portmapper"
"tailscale.com/types/logger"
"tailscale.com/util/def"
"tailscale.com/util/eventbus"
)
@@ -66,7 +66,7 @@ func serveDebugPortmap(h *localapi.Handler, w http.ResponseWriter, r *http.Reque
}
}
if defBool(r.FormValue("log_http"), false) {
if def.Bool(r.FormValue("log_http"), false) {
debugKnobs.LogHTTP = true
}
@@ -191,14 +191,3 @@ func serveDebugPortmap(h *localapi.Handler, w http.ResponseWriter, r *http.Reque
}
}
}
func defBool(a string, def bool) bool {
if a == "" {
return def
}
v, err := strconv.ParseBool(a)
if err != nil {
return def
}
return v
}