From a0fad30e9940720775c8303015451be588cd5d13 Mon Sep 17 00:00:00 2001 From: Craig Williams Date: Tue, 29 Sep 2026 11:55:11 +0100 Subject: [PATCH] Config to string, JSON and YAML methods added and updated print methods to use new methods --- internal/cli/config/get.go | 22 ++-------------------- internal/config/config.go | 27 +++++++++++++++++++++++++++ internal/config/print.go | 16 +++------------- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/internal/cli/config/get.go b/internal/cli/config/get.go index 3eff309..6ffbdc3 100644 --- a/internal/cli/config/get.go +++ b/internal/cli/config/get.go @@ -27,26 +27,8 @@ import ( func commandGet() *cobra.Command { cmd := &cobra.Command{ - Use: "get [config_key]", - Args: cobra.ExactArgs(1), - // Args: func(cmd *cobra.Command, args []string) error { - // // Optionally run one of the validators provided by cobra - // if err := cobra.ExactArgs(1)(cmd, args); err != nil { - // return err - // } - - // conf, ok := cli.ConfigFromContext(cmd.Context()) - // if !ok { - // return errors.New("no configuration loaded") - // } - - // flatConf := config.FlattenConfig(conf) - // if !util.HasKey(flatConf, args[0]) { - // return fmt.Errorf("invalid config key specified: %s", args[0]) - // } - - // return nil - // }, + Use: "get [config_key]", + Args: cobra.ExactArgs(1), Short: "Get a value from configuration", PersistentPreRun: func(cmd *cobra.Command, args []string) { slog.Debug("config.get.PersistentPreRun") diff --git a/internal/config/config.go b/internal/config/config.go index e7c6ecb..b28c817 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -17,12 +17,15 @@ along with this program. If not, see . package config import ( + "encoding/json" "errors" + "fmt" "maps" "reflect" "strings" "github.com/spf13/viper" + "go.yaml.in/yaml/v3" ) const EnvPrefix string = "JOBSEARCH" @@ -112,3 +115,27 @@ func LoadToViper(conf JobSearchConfig) { viper.SetDefault(key, flatMap[key]) } } + +// Converts configuration to a string in flat dotted format +func (conf JobSearchConfig) String() string { + var sb strings.Builder + for k, v := range conf.Flatten() { + typ, _ := conf.TypeOf(k) + sb.WriteString(fmt.Sprint(k, " = ", v, " <", typ, ">")) + } + return sb.String() +} + +// Converts configuration to JSON +func (conf JobSearchConfig) JSON() string { + confMap := conf.Map() + jbytes, _ := json.MarshalIndent(confMap, "", " ") + return string(jbytes) +} + +// Converts configuration to YAML +func (conf JobSearchConfig) YAML() string { + confMap := conf.Map() + ybytes, _ := yaml.Marshal(confMap) + return string(ybytes) +} diff --git a/internal/config/print.go b/internal/config/print.go index 158a9a6..9f26a11 100644 --- a/internal/config/print.go +++ b/internal/config/print.go @@ -17,33 +17,23 @@ along with this program. If not, see . package config import ( - "encoding/json" "fmt" "log/slog" - - "go.yaml.in/yaml/v3" ) // Prints configuration to stdout in flat dotted format func (conf JobSearchConfig) PrintTerminal() { - for k, v := range conf.Flatten() { - typ, _ := conf.TypeOf(k) - fmt.Print(k, " = ", v, " <", typ, ">\n") - } + fmt.Print(conf.String() + "\n") } // Prints configuration to stdout as JSON func (conf JobSearchConfig) PrintJson() { - confMap := conf.Map() - jbytes, _ := json.MarshalIndent(confMap, "", " ") - fmt.Print(string(jbytes) + "\n") + fmt.Print(conf.JSON() + "\n") } // Prints configuration to stdout as YAML func (conf JobSearchConfig) PrintYaml() { - confMap := conf.Map() - jbytes, _ := yaml.Marshal(confMap) - fmt.Print(string(jbytes) + "\n") + fmt.Print(conf.YAML() + "\n") } // Prints configuration to logrus log at debug, info (default) or error level