From c1d572b5420100f950dfaa0e6e3a6b86526dd398 Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann Date: Fri, 25 Apr 2025 22:57:27 +0200 Subject: [PATCH] config: add GetBool convenience method It's the boolean equivalent of `GetInt()`. The value handling is taken from the Python version (`srht.config.cfgb()`). --- config/config.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/config/config.go b/config/config.go index 9f1517c3485154f5c106a2f9f788301d234d049a..0dbf1b3cbfba7d8376afa401794573ba4f76e305 100644 --- a/config/config.go +++ b/config/config.go @@ -129,6 +129,24 @@ func GetInt(conf ini.File, section, name string, defValue int) int { return value } +// Returns the configured boolean value for the given section and variable name. +// If nothing is configured, the default value will be returned. +// If an invalid boolean is configured, this function will panic. +func GetBool(conf ini.File, section, name string, defValue bool) bool { + value := defValue + if s, ok := conf.Get(section, name); ok { + switch s { + case "true", "yes", "on", "1": + value = true + case "false", "no", "off", "0": + value = false + default: + panic(fmt.Errorf("[%s]%s: invalid boolean value", section, name)) + } + } + return value +} + // Returns true if the given IP address is part of the internal networks as // per the configuration of [sr.ht]internal-ipnet. func IsInternalIP(ip net.IP) bool {