- 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_*)
48 lines
878 B
Go
48 lines
878 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
fp "path/filepath"
|
|
)
|
|
|
|
const ConfigStr = `# Configuration for my-log
|
|
|
|
[input]
|
|
# Path to where the log files are stored
|
|
path = "%s"
|
|
# File extension for log files
|
|
ext = "txt"
|
|
# Whether to look in sub-folders
|
|
recurse = true
|
|
# Whether to use a dot as a folder separator in log names
|
|
dotFolder = true
|
|
|
|
# config for output types
|
|
[output]
|
|
|
|
# This one just prints the logs to stdout when run
|
|
[output.stdout]
|
|
enabled = true
|
|
[output.stdout.config]
|
|
# Formatter to use when outputting to stdout
|
|
format = "plain"
|
|
|
|
[formatters]
|
|
|
|
[formatters.json]
|
|
# Set to true to pretty print JSON output
|
|
pretty_print = false
|
|
|
|
`
|
|
|
|
func DefaultPath() string {
|
|
conf, _ := os.UserConfigDir()
|
|
return fp.Join(conf, "my-log", "config.toml")
|
|
}
|
|
|
|
func DefaultStr() string {
|
|
home, _ := os.UserHomeDir()
|
|
inDir := fp.Join(home, "my-log")
|
|
return fmt.Sprintf(ConfigStr, inDir)
|
|
}
|