gci is configured with the sections standard, default and localmodule, and goimports with the module path as its local prefix, so both formatters agree.
70 lines
1.6 KiB
Go
70 lines
1.6 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("config.set.PersistentPreRun")
|
|
},
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
slog.Debug("config.set.PersistentPostRun")
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
slog.Debug("config.set.Run")
|
|
|
|
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
|
|
}
|