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.
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"git.avsdev.uk/Go/job-search/internal/cli"
|
|
)
|
|
|
|
func commandGet(opts *cli.Options) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "get [config_key]",
|
|
Args: cobra.ExactArgs(1),
|
|
Short: "Get a value from configuration",
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
slog.Debug("command persistent pre-run", "command", "config.get")
|
|
},
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
slog.Debug("command persistent post-run", "command", "config.get")
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
slog.Debug("command run", "command", "config.get")
|
|
|
|
if err := cobra.ExactArgs(1)(cmd, args); err != nil {
|
|
return err
|
|
}
|
|
|
|
conf, err := opts.RunSetup(cmd, true, true)
|
|
if err != nil {
|
|
return fmt.Errorf("no configuration loaded (%w)", err)
|
|
}
|
|
|
|
v, err := conf.ValueOf(args[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t, _ := conf.TypeOf(args[0])
|
|
fmt.Println(v, "<"+t.String()+">")
|
|
|
|
return nil
|
|
},
|
|
}
|
|
return cmd
|
|
}
|