S6 settings: read a setting by its key in a process and a bundle
The node-engine composes each config value as MESH_CONFIG_<KEY>, in a process's environment and the module's tool-runner slice alike; stdio.Setting and SettingDuration read it, the one reader both sides use. Builds: design 47 H2 §781; H-A-350 DMC-13, H-A-370
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package stdio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SettingWord is the environment word the node-engine composes a module's config value key into, in a process's
|
||||
// environment and in the module's slice of the tool runner's MESH_TOOL_ENV alike (design 47 H2 §781; H-A-370):
|
||||
// MESH_CONFIG_<KEY>, the key upper-cased with '-' and '.' written '_', holding the value as text.
|
||||
func SettingWord(key string) string {
|
||||
return "MESH_CONFIG_" + strings.ToUpper(strings.NewReplacer("-", "_", ".", "_").Replace(key))
|
||||
}
|
||||
|
||||
// Setting is the module's config value key as the node-engine composed it, and whether it composed one: the one
|
||||
// settings reader of a process and a bundle (§781).
|
||||
func Setting(key string) (string, bool) { return os.LookupEnv(SettingWord(key)) }
|
||||
|
||||
// SettingDuration is the config value key read as a Go duration ("10m"): def when none is composed or it is empty, an
|
||||
// error naming the key for a value that is no duration above zero.
|
||||
func SettingDuration(key string, def time.Duration) (time.Duration, error) {
|
||||
v, ok := Setting(key)
|
||||
if v = strings.TrimSpace(v); !ok || v == "" {
|
||||
return def, nil
|
||||
}
|
||||
d, err := time.ParseDuration(v)
|
||||
if err != nil || d <= 0 {
|
||||
return 0, fmt.Errorf("the setting %s (%s) holds %q, which is not a duration above zero", key, SettingWord(key), v)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
Reference in New Issue
Block a user