♲ Refactor configuration to use viper with context propagation
- Replace global ConfigPath and Overrides with viper-based configuration - Add viper.New() to create configurable viper instances - Store viper and unmarshaled Config struct in context for testability - Add RetrieveFromContext and AddToContext helper functions - Update files.Append to accept context and retrieve config from it - Update formatters.Preferred and formatters.New to accept context - Add PersistentPreRunE in CLI to create and configure viper instance - Support -c flag for custom config file path - Support -v flag for config value overrides - Update all test files to create viper and add to context - Remove unused config types and load functions - Add viper as dependency with automatic env var support (MYLOG_*)
This commit is contained in:
parent
d34363b8c0
commit
9f05f933dd
21 changed files with 338 additions and 360 deletions
|
|
@ -32,23 +32,24 @@ var ConfigCmd = &cobra.Command{
|
|||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
print, _ := cmd.Flags().GetBool("print")
|
||||
if print {
|
||||
fmt.Fprintln(cmd.OutOrStdout(), config.ConfigPath)
|
||||
fmt.Fprintln(cmd.OutOrStdout(), config.DefaultPath())
|
||||
return nil
|
||||
}
|
||||
force, _ := cmd.Flags().GetBool("force")
|
||||
configPath := config.DefaultPath()
|
||||
if !force {
|
||||
_, err = os.Stat(config.ConfigPath)
|
||||
_, err = os.Stat(configPath)
|
||||
if !os.IsNotExist(err) {
|
||||
return fmt.Errorf("%s already exists. Use -f to overwrite", config.ConfigPath)
|
||||
return fmt.Errorf("%s already exists. Use -f to overwrite", configPath)
|
||||
}
|
||||
}
|
||||
dir := fp.Dir(config.ConfigPath)
|
||||
dir := fp.Dir(configPath)
|
||||
err = os.MkdirAll(dir, 0755)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Create(config.ConfigPath)
|
||||
f, err := os.Create(configPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,8 +42,9 @@ var DropCmd = &cobra.Command{
|
|||
Args: cobra.ExactArgs(2),
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
v, _ := config.RetrieveFromContext(cmd.Context())
|
||||
if outJson {
|
||||
config.Overrides["output.stdout.config.format"] = "json"
|
||||
v.Set("output.stdout.config.format", "json")
|
||||
}
|
||||
|
||||
log := args[0]
|
||||
|
|
@ -60,12 +61,12 @@ var DropCmd = &cobra.Command{
|
|||
}
|
||||
e := models.Entry{Title: title, Date: d.Time(), Fields: *ms}
|
||||
l := models.Log{Name: log, Entries: []models.Entry{e}}
|
||||
err := files.Append(l)
|
||||
err := files.Append(cmd.Context(), l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
form, err := formatters.Preferred()
|
||||
form, err := formatters.Preferred(cmd.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
36
cli/root.go
36
cli/root.go
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
Copyright © 2024 Dan Jones <danjones@goodevilgenius.org>
|
||||
Copyright © 2026 Dan Jones <danjones@goodevilgenius.org>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
|
|
@ -17,6 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"codeberg.org/danjones000/my-log/config"
|
||||
|
|
@ -26,16 +27,41 @@ import (
|
|||
var RootCmd = &cobra.Command{
|
||||
Use: "my-log",
|
||||
Short: "A brief description of your application",
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx, v, err := config.New(cmd.Context())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if configPath != "" {
|
||||
v.SetConfigFile(configPath)
|
||||
err := v.ReadInConfig()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for k, val := range configValues {
|
||||
v.Set(k, val)
|
||||
}
|
||||
|
||||
cmd.SetContext(ctx)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
err := RootCmd.Execute()
|
||||
func Execute(ctx context.Context) {
|
||||
err := RootCmd.ExecuteContext(ctx)
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
var configPath string
|
||||
var configValues map[string]string
|
||||
|
||||
func init() {
|
||||
RootCmd.PersistentFlags().StringVarP(&config.ConfigPath, "config", "c", config.ConfigPath, "config file")
|
||||
RootCmd.PersistentFlags().StringToStringVarP(&config.Overrides, "config-value", "v", config.Overrides, "Override config values. Use dot syntax to specify key. E.g. -v output.stdout.config.format=json")
|
||||
RootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", config.DefaultPath(), "config file")
|
||||
RootCmd.PersistentFlags().StringToStringVarP(&configValues, "config-value", "v", nil, "Override config values. Use dot syntax to specify key. E.g. -v output.stdout.config.format=json")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue