go-pkg/env/env.go

25 lines
456 B
Go
Raw Normal View History

// SPDX-FileCopyrightText: 2023 Jon Lundy <jon@xuu.cc>
// SPDX-License-Identifier: BSD-3-Clause
2023-07-12 12:43:25 -06:00
package env
import (
"log/slog"
2023-07-12 12:43:25 -06:00
"os"
"strings"
)
2024-01-22 16:00:58 -07:00
func Default(name, defaultValue string) (s string) {
2023-07-12 12:43:25 -06:00
name = strings.TrimSpace(name)
2024-01-22 16:00:58 -07:00
s = strings.TrimSpace(defaultValue)
2023-07-12 12:43:25 -06:00
2024-01-22 16:00:58 -07:00
if v, ok := os.LookupEnv(name); ok {
s = strings.TrimSpace(v)
slog.Info("env", slog.String(name, v))
return
2023-07-12 12:43:25 -06:00
}
2024-01-22 16:00:58 -07:00
slog.Info("env", slog.String(name, s+" (default)"))
return
2023-07-12 12:43:25 -06:00
}