♲ 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:
Dan Jones 2026-03-08 22:59:33 -05:00
commit 9f05f933dd
21 changed files with 338 additions and 360 deletions

View file

@ -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
}