♲ 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
|
|
@ -1,6 +1,7 @@
|
|||
package files
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
|
@ -11,11 +12,8 @@ import (
|
|||
"codeberg.org/danjones000/my-log/models"
|
||||
)
|
||||
|
||||
func Append(l models.Log) error {
|
||||
conf, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
func Append(ctx context.Context, l models.Log) error {
|
||||
_, conf := config.RetrieveFromContext(ctx)
|
||||
|
||||
filename := l.Name
|
||||
if conf.Input.DotFolder {
|
||||
|
|
@ -27,7 +25,7 @@ func Append(l models.Log) error {
|
|||
}
|
||||
path := fp.Join(conf.Input.Path, filename)
|
||||
dir := fp.Dir(path)
|
||||
err = os.MkdirAll(dir, 0750)
|
||||
err := os.MkdirAll(dir, 0750)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue