2023-09-15 13:24:51 -06:00
|
|
|
// 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 (
|
2023-09-15 13:24:51 -06:00
|
|
|
"log/slog"
|
2023-07-12 12:43:25 -06:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Default(name, defaultValue string) string {
|
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
defaultValue = strings.TrimSpace(defaultValue)
|
|
|
|
if v := strings.TrimSpace(os.Getenv(name)); v != "" {
|
2023-09-15 13:24:51 -06:00
|
|
|
slog.Info("env", name, v)
|
2023-07-12 12:43:25 -06:00
|
|
|
return v
|
|
|
|
}
|
2023-09-15 13:24:51 -06:00
|
|
|
slog.Info("env", name, defaultValue+" (default)")
|
2023-07-12 12:43:25 -06:00
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
|
|
|
type secret string
|
|
|
|
|
|
|
|
func (s secret) String() string {
|
|
|
|
if s == "" {
|
|
|
|
return "(nil)"
|
|
|
|
}
|
|
|
|
return "***"
|
|
|
|
}
|
|
|
|
func (s secret) Secret() string {
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
func Secret(name, defaultValue string) secret {
|
|
|
|
name = strings.TrimSpace(name)
|
|
|
|
defaultValue = strings.TrimSpace(defaultValue)
|
|
|
|
if v := strings.TrimSpace(os.Getenv(name)); v != "" {
|
2023-09-15 13:24:51 -06:00
|
|
|
slog.Info("env", name, secret(v))
|
2023-07-12 12:43:25 -06:00
|
|
|
return secret(v)
|
|
|
|
}
|
2023-09-15 13:24:51 -06:00
|
|
|
slog.Info("env", name, secret(defaultValue).String()+" (default)")
|
2023-07-12 12:43:25 -06:00
|
|
|
return secret(defaultValue)
|
|
|
|
}
|