mirror of
https://github.com/tailscale/tailscale.git
synced 2026-06-25 08:22:18 -04:00
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>
35 lines
657 B
Go
35 lines
657 B
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package def parses strings with fallback default values.
|
|
package def
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// Bool parses s as a bool, returning def when s is empty or invalid.
|
|
func Bool(s string, def bool) bool {
|
|
if s == "" {
|
|
return def
|
|
}
|
|
v, err := strconv.ParseBool(s)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Duration parses s as a time.Duration, returning def when s is empty or invalid.
|
|
func Duration(s string, def time.Duration) time.Duration {
|
|
if s == "" {
|
|
return def
|
|
}
|
|
v, err := time.ParseDuration(s)
|
|
if err != nil {
|
|
return def
|
|
}
|
|
return v
|
|
}
|