Procházet zdrojové kódy

feat(config): add new flag to specify config path (#548)

Ayyan před 11 měsíci
rodič
revize
5e1c4e2c06
2 změnil soubory, kde provedl 12 přidání a 6 odebrání
  1. 6 3
      cmd/root.go
  2. 6 3
      internal/config/config.go

+ 6 - 3
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
 	}

+ 6 - 3
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()