Files
job-search/internal/cli/config/set.go
T
bwilliams 043e307663 Logging: structured slog messages throughout, and WithLogger options so callers choose the logger
Log messages are lower-case event names with their details as key/value
attributes ("error" for errors), in place of formatted strings.

Loggable package take a WithLogger option as part of their "New*" call,
defaulting to slog.Default(). Each tags its lines with its `component`.

Some debug loggers print directly rather than using slog when in
terminal logging mode.
2026-10-15 16:10:52 +01:00

70 lines
1.7 KiB
Go

package config
import (
"errors"
"fmt"
"log/slog"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"git.avsdev.uk/Go/job-search/internal/cli"
"git.avsdev.uk/Go/job-search/internal/config"
)
func commandSet(opts *cli.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "set [config_key] [config_value]",
Args: cobra.MatchAll(cobra.MinimumNArgs(1), cobra.MaximumNArgs(2)),
Short: "Set a value to configuration",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
slog.Debug("command persistent pre-run", "command", "config.set")
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
slog.Debug("command persistent post-run", "command", "config.set")
},
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("command run", "command", "config.set")
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
return err
}
if err := cobra.MaximumNArgs(2)(cmd, args); err != nil {
return err
}
unset, unsetFlagErr := cmd.Flags().GetBool("unset")
if unsetFlagErr != nil {
return unsetFlagErr
}
if err := cobra.ExactArgs(2)(cmd, args); err != nil && !unset {
return err
}
conf, err := opts.RunSetup(cmd, true, true)
if err != nil {
return fmt.Errorf("no configuration loaded (%w)", err)
}
if viper.ConfigFileUsed() == "" {
return errors.New("no configuration file exists")
}
if unset {
return config.PurgeValue(conf, args[0])
}
confValue, confType, err := config.StoreValue(conf, args[0], args[1])
if err != nil {
return err
}
fmt.Print(args[0], " = ", confValue, " <", confType, ">\n")
return nil
},
}
cmd.Flags().BoolP("unset", "x", false, "clear configuration setting")
return cmd
}