90 lines
2.4 KiB
Go
90 lines
2.4 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 cli
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"git.avsdev.uk/Go/job-search/internal/config"
|
|
"git.avsdev.uk/Go/job-search/internal/log"
|
|
"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) {
|
|
// 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 && !log.IsDebugEnabled() {
|
|
log.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 {
|
|
log.LogToFile(conf.Logging.File)
|
|
}
|
|
}
|
|
}
|
|
|
|
return conf, nil
|
|
}
|
|
|
|
// Finish shuts down any open file logger
|
|
func (o *Options) Finish() error {
|
|
if log.IsFileLogger() {
|
|
log.Shutdown()
|
|
}
|
|
|
|
return nil
|
|
}
|