Moved config set/unset to internal/config/modify.go as Store and Purge.

This commit is contained in:
bwilliams committed 2026-10-09 17:07:06 +01:00
1 parent 539e88e73c
commit 09644d0691
4 files changed
+102 -67

No files matched your search

+10 -64
View File
@@ -4,8 +4,6 @@ import (
"errors"
"fmt"
"log/slog"
"reflect"
"strconv"
"git.avsdev.uk/Go/job-search/internal/cli"
"git.avsdev.uk/Go/job-search/internal/config"
@@ -13,65 +11,6 @@ import (
"github.com/spf13/viper"
)
func setConfig(conf *config.JobSearchConfig, key string, value string) error {
var confValue any = nil
var confType reflect.Kind
var err error
confType, err = conf.TypeOf(key)
if err != nil {
return err
}
switch confType {
case reflect.Bool:
confValue, err = strconv.ParseBool(value)
case reflect.String:
confValue = value
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
confValue, err = strconv.ParseInt(value, 10, 64)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
confValue, err = strconv.ParseUint(value, 10, 64)
case reflect.Float32, reflect.Float64:
confValue, err = strconv.ParseFloat(value, 64)
default:
err = errors.ErrUnsupported
}
if err != nil {
return fmt.Errorf("config value type not accepted, expected <%s>", confType)
}
viper.Set(key, confValue)
if err := viper.WriteConfig(); err != nil {
return err
}
fmt.Print(key, " = ", confValue, " <", confType, ">\n")
return nil
}
func unsetConfig(conf *config.JobSearchConfig, key string) error {
if _, err := conf.TypeOf(key); err != nil {
return err
}
v := viper.New()
v.SetConfigFile(viper.ConfigFileUsed())
keys := viper.AllKeys()
for i := range keys {
k := keys[i]
if k != key {
v.Set(k, viper.Get(k))
}
}
if err := v.WriteConfig(); err != nil {
return err
}
return nil
}
func commandSet(opts *cli.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "set [config_key] [config_value]",
@@ -111,10 +50,17 @@ func commandSet(opts *cli.Options) *cobra.Command {
}
if unset {
return unsetConfig(conf, args[0])
} else {
return setConfig(conf, args[0], args[1])
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")