Files
job-search/internal/config/print.go
T
2026-09-23 14:32:50 +01:00

76 lines
2.0 KiB
Go

/*
Copyright © 2026 Craig Williams <craig@avsdev.uk>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package config
import (
"encoding/json"
"fmt"
"log/slog"
"go.yaml.in/yaml/v3"
)
// Prints configuration to stdout in flat dotted format
func (conf JobSearchConfig) PrintTerminal() {
for k, v := range conf.Flatten() {
typ, _ := conf.TypeOf(k)
fmt.Print(k, " = ", v, " <", typ, ">\n")
}
}
// Prints configuration to stdout as JSON
func (conf JobSearchConfig) PrintJson() {
confMap := conf.Map()
jbytes, _ := json.MarshalIndent(confMap, "", " ")
fmt.Print(string(jbytes) + "\n")
}
// Prints configuration to stdout as YAML
func (conf JobSearchConfig) PrintYaml() {
confMap := conf.Map()
jbytes, _ := yaml.Marshal(confMap)
fmt.Print(string(jbytes) + "\n")
}
// Prints configuration to logrus log at debug, info (default) or error level
func (conf JobSearchConfig) PrintLog(atLevel slog.Level) {
var logFn func(args ...interface{})
switch atLevel {
case slog.LevelDebug:
logFn = func(args ...interface{}) {
slog.Debug(fmt.Sprint(args...))
}
case slog.LevelError:
logFn = func(args ...interface{}) {
slog.Error(fmt.Sprint(args...))
}
case slog.LevelWarn:
logFn = func(args ...interface{}) {
slog.Warn(fmt.Sprint(args...))
}
default:
logFn = func(args ...interface{}) {
slog.Info(fmt.Sprint(args...))
}
}
for k, v := range conf.Flatten() {
logFn("\t", k, " = ", v)
}
}