From 5e1c4e2c06350cb360be9eb9347b52bb196ce071 Mon Sep 17 00:00:00 2001 From: Ayyan Date: Sat, 24 May 2025 22:36:08 -0400 Subject: [PATCH] feat(config): add new flag to specify config path (#548) --- cmd/root.go | 9 ++++++--- internal/config/config.go | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index f13a49f..508b812 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,6 +20,11 @@ var ( func Run() error { logLevel := flag.String("log-level", "info", "log level") + logFormat := flag.String("log-format", "text", "log format") + token := flag.String("token", "", "authentication token") + configPath := flag.String("config", config.DefaultPath(), "path to the configuration file") + flag.Parse() + var level slog.Level switch *logLevel { case "debug": @@ -33,7 +38,6 @@ func Run() error { level = slog.LevelError } - logFormat := flag.String("log-format", "text", "log format") var format logger.Format switch *logFormat { case "text": @@ -46,7 +50,6 @@ func Run() error { return err } - token := flag.String("token", "", "authentication token") tok := *token if tok == "" { var err error @@ -56,7 +59,7 @@ func Run() error { } } - cfg, err := config.Load() + cfg, err := config.Load(*configPath) if err != nil { return err } diff --git a/internal/config/config.go b/internal/config/config.go index b6e91b3..2bc2165 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -93,15 +93,18 @@ func defaultConfig() *Config { } } -// Reads the configuration file and parses it. -func Load() (*Config, error) { +func DefaultPath() string { path, err := os.UserConfigDir() if err != nil { slog.Info("user configuration directory path cannot be determined; falling back to the current directory path") path = "." } - path = filepath.Join(path, consts.Name, fileName) + return filepath.Join(path, consts.Name, fileName) +} + +// Reads the configuration file and parses it. +func Load(path string) (*Config, error) { f, err := os.Open(path) cfg := defaultConfig()