Refactored main.go into cmd/jobsearch/main.go (in case separate commands are introduced later) which has required the options mechanism to change slightly as well as the logging.

This commit is contained in:
bwilliams committed 2026-10-09 09:29:42 +01:00
1 parent de3ebf68c5
commit 6436253629
14 files changed
+273 -224

No files matched your search

+5 -4
View File
@@ -19,10 +19,11 @@ package config
import (
"log/slog"
"git.avsdev.uk/Go/job-search/internal/cli"
"github.com/spf13/cobra"
)
func NewCommand() *cobra.Command {
func NewCommand(opts *cli.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "Configuration management",
@@ -33,9 +34,9 @@ func NewCommand() *cobra.Command {
slog.Debug("config.PersistentPostRun")
},
}
cmd.AddCommand(commandShow())
cmd.AddCommand(commandGet())
cmd.AddCommand(commandSet())
cmd.AddCommand(commandShow(opts))
cmd.AddCommand(commandGet(opts))
cmd.AddCommand(commandSet(opts))
cmd.AddCommand(commandInit())
return cmd
}
+4 -8
View File
@@ -17,7 +17,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package config
import (
"errors"
"fmt"
"log/slog"
@@ -25,7 +24,7 @@ import (
"github.com/spf13/cobra"
)
func commandGet() *cobra.Command {
func commandGet(opts *cli.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "get [config_key]",
Args: cobra.ExactArgs(1),
@@ -43,9 +42,9 @@ func commandGet() *cobra.Command {
return err
}
conf, ok := cli.ConfigFromContext(cmd.Context())
if !ok {
return errors.New("no configuration loaded")
conf, err := opts.RunSetup(cmd, true, true)
if err != nil {
return fmt.Errorf("no configuration loaded (%w)", err)
}
v, err := conf.ValueOf(args[0])
@@ -57,9 +56,6 @@ func commandGet() *cobra.Command {
return nil
},
Annotations: map[string]string{
"terminal_log": "1",
},
}
return cmd
}
-4
View File
@@ -87,10 +87,6 @@ func commandInit() *cobra.Command {
}
return nil
},
Annotations: map[string]string{
"no_config": "1",
"terminal_log": "1",
},
}
return cmd
}
+6 -9
View File
@@ -29,7 +29,7 @@ import (
"github.com/spf13/viper"
)
func setConfig(conf config.JobSearchConfig, key string, value string) error {
func setConfig(conf *config.JobSearchConfig, key string, value string) error {
var confValue any = nil
var confType reflect.Kind
var err error
@@ -65,7 +65,7 @@ func setConfig(conf config.JobSearchConfig, key string, value string) error {
return nil
}
func unsetConfig(conf config.JobSearchConfig, key string) error {
func unsetConfig(conf *config.JobSearchConfig, key string) error {
if _, err := conf.TypeOf(key); err != nil {
return err
}
@@ -88,7 +88,7 @@ func unsetConfig(conf config.JobSearchConfig, key string) error {
return nil
}
func commandSet() *cobra.Command {
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)),
@@ -117,9 +117,9 @@ func commandSet() *cobra.Command {
return err
}
conf, ok := cli.ConfigFromContext(cmd.Context())
if !ok {
return errors.New("no configuration loaded")
conf, err := opts.RunSetup(cmd, true, true)
if err != nil {
return fmt.Errorf("no configuration loaded (%w)", err)
}
if viper.ConfigFileUsed() == "" {
@@ -132,9 +132,6 @@ func commandSet() *cobra.Command {
return setConfig(conf, args[0], args[1])
}
},
Annotations: map[string]string{
"terminal_log": "1",
},
}
cmd.Flags().BoolP("unset", "x", false, "clear configuration setting")
return cmd
+5 -8
View File
@@ -17,14 +17,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package config
import (
"errors"
"fmt"
"log/slog"
"git.avsdev.uk/Go/job-search/internal/cli"
"github.com/spf13/cobra"
)
func commandShow() *cobra.Command {
func commandShow(opts *cli.Options) *cobra.Command {
cmd := &cobra.Command{
Use: "show",
Short: "Print config information",
@@ -37,9 +37,9 @@ func commandShow() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
slog.Debug("config.show.Run")
conf, ok := cli.ConfigFromContext(cmd.Context())
if !ok {
return errors.New("no configuration loaded")
conf, err := opts.RunSetup(cmd, true, true)
if err != nil {
return fmt.Errorf("no configuration loaded (%w)", err)
}
if yaml, _ := cmd.Flags().GetBool("yaml"); yaml {
@@ -52,9 +52,6 @@ func commandShow() *cobra.Command {
return nil
},
Annotations: map[string]string{
"terminal_log": "1",
},
}
cmd.Flags().Bool("json", false, "format configuration as json")
cmd.Flags().Bool("yaml", false, "format configuration as yaml")