- logging: LogToFile shadowed the package-level fileLogger, so IsFileLogger was always false and Shutdown never closed the file - logging: replace IsJSONHandler's unsafe reflection into slog internals with a *slog.JSONHandler type check - cli: fail if the log file can't be opened instead of silently logging to the terminal; return the log shutdown error from Finish - build.sh: -X flags used the wrong module path, so the version, build time and commit were never stamped
79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"git.avsdev.uk/Go/job-search/internal/config"
|
|
"git.avsdev.uk/Go/job-search/internal/logging"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type Options struct {
|
|
configFile string
|
|
debug bool
|
|
quiet bool
|
|
}
|
|
|
|
// Binds common options through the root command
|
|
func (o *Options) Bind(cmd *cobra.Command) {
|
|
pf := cmd.PersistentFlags()
|
|
pf.StringVarP(&o.configFile, "config", "c", "", "configuration file")
|
|
pf.BoolVarP(&o.debug, "debug", "d", false, "enable debug messages")
|
|
pf.BoolVarP(
|
|
&o.quiet, "quiet", "q", false, "don't report info or warning messages",
|
|
)
|
|
}
|
|
|
|
// Setup loads the config and ensure logging is switch to file if needed
|
|
func (o *Options) RunSetup(
|
|
cmd *cobra.Command, loadConfig bool, terminalLog bool,
|
|
) (*config.JobSearchConfig, error) {
|
|
// Flags and arguments are valid from here on, so errors are runtime
|
|
// failures: report them without cobra's usage text.
|
|
cmd.SilenceUsage = true
|
|
|
|
// Check for debug level or quiet supression via args
|
|
conf := &config.JobSearchConfig{
|
|
Logging: config.JobSearchConfigLogging{},
|
|
}
|
|
if o.debug {
|
|
conf.Logging.Level = "debug"
|
|
} else if o.quiet {
|
|
conf.Logging.Level = "error"
|
|
}
|
|
|
|
// Only load config if command requests it (nearly always true)
|
|
if loadConfig {
|
|
// Load config - from specified file if provided
|
|
if err := config.Init(o.configFile, conf); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Change logging level based off newly loaded config
|
|
if conf.Logging.LogLevel() <= slog.LevelDebug && !logging.IsDebugEnabled() {
|
|
logging.EnableDebug()
|
|
}
|
|
|
|
// Check if the logging should be switched to file
|
|
if !terminalLog {
|
|
if len(conf.Logging.File) == 0 {
|
|
slog.Warn("No log file configured, remaining in console logging mode!")
|
|
} else if err := logging.LogToFile(conf.Logging.File); err != nil {
|
|
return nil, fmt.Errorf("opening log file: %w", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return conf, nil
|
|
}
|
|
|
|
// Finish shuts down any open file logger
|
|
func (o *Options) Finish() error {
|
|
if logging.IsFileLogger() {
|
|
return logging.Shutdown()
|
|
}
|
|
|
|
return nil
|
|
}
|