printLoadedConf modified to only print explicitly set config values (via env or config file)

This commit is contained in:
bwilliams committed 2026-10-14 15:38:38 +01:00
1 parent d6688a6754
commit 803a8929e6
3 files changed
+122 -7

No files matched your search

+24 -1
View File
@@ -3,12 +3,35 @@ package config
import (
"fmt"
"log/slog"
"os"
"strings"
"github.com/joho/godotenv"
"github.com/spf13/viper"
)
// envKeyReplacer turns a dotted key into its environment variable's suffix
// (logging.level to LOGGING_LEVEL, once upper-cased).
var envKeyReplacer = strings.NewReplacer(".", "_")
// envVarName returns the environment variable that sets key, e.g.
// JOBSEARCH_LOGGING_LEVEL for logging.level.
func envVarName(key string) string {
return EnvPrefix + "_" + strings.ToUpper(envKeyReplacer.Replace(key))
}
// isExplicitlySet reports whether key's value in Viper was set by the config
// file or by its environment variable, rather than coming from its default.
// As Viper does, it ignores an empty environment variable. Values set with
// viper.Set (e.g. by StoreValue) are not counted.
func isExplicitlySet(key string) bool {
if viper.InConfig(key) {
return true
}
value, ok := os.LookupEnv(envVarName(key))
return ok && value != ""
}
// loadEnv loads environment variables from a .env file in the working
// directory.
func loadEnv() error {
@@ -37,7 +60,7 @@ func loadConfig(cfgFile string) error {
LoadDefaultConfig()
viper.SetEnvPrefix(EnvPrefix)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.SetEnvKeyReplacer(envKeyReplacer)
viper.AutomaticEnv() // read in environment variables that match
err := viper.ReadInConfig()