Short and single-letter names in application code become full words (tests keep theirs). Wrapped calls, signatures and composite literals put one item per line, and one-line function bodies are expanded. Exported names and groups of constants get doc comments, and comments refer to the renamed identifiers.
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package config
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"maps"
|
|
"slices"
|
|
|
|
"git.avsdev.uk/Go/job-search/internal/logging"
|
|
)
|
|
|
|
// PrintTerminal prints the configuration to stdout as String formats it.
|
|
func (c JobSearchConfig) PrintTerminal() {
|
|
fmt.Print(c.String() + "\n")
|
|
}
|
|
|
|
// PrintJson prints the configuration to stdout as JSON.
|
|
func (c JobSearchConfig) PrintJson() {
|
|
fmt.Print(c.JSON() + "\n")
|
|
}
|
|
|
|
// PrintYaml prints the configuration to stdout as YAML.
|
|
func (c JobSearchConfig) PrintYaml() {
|
|
fmt.Print(c.YAML() + "\n")
|
|
}
|
|
|
|
// PrintLog logs each configuration key and value, sorted by key, at atLevel:
|
|
// debug, warn, error, or info for any other level. On a terminal it logs one
|
|
// line and prints the values under it as "key = value" lines; elsewhere each
|
|
// value is a log entry of its own.
|
|
func (conf JobSearchConfig) PrintLog(atLevel slog.Level) {
|
|
switch atLevel {
|
|
case slog.LevelDebug, slog.LevelWarn, slog.LevelError:
|
|
default:
|
|
atLevel = slog.LevelInfo
|
|
}
|
|
|
|
ctx := context.Background()
|
|
logger := slog.Default()
|
|
flat := conf.Flatten()
|
|
keys := slices.Sorted(maps.Keys(flat))
|
|
|
|
if logging.WritesToTerminal(logger.Handler()) {
|
|
if !logger.Enabled(ctx, atLevel) {
|
|
return
|
|
}
|
|
logger.Log(ctx, atLevel, "config values", "count", len(keys))
|
|
for _, key := range keys {
|
|
logging.PrintToTerminal(fmt.Sprintf("\t%s = %v", key, flat[key]))
|
|
}
|
|
return
|
|
}
|
|
|
|
for _, key := range keys {
|
|
logger.Log(ctx, atLevel, "config value", "key", key, "value", flat[key])
|
|
}
|
|
} |