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

This commit is contained in:
Ayyan 2025-05-24 22:36:08 -04:00 • committed by GitHub
parent 1dd5a347a4
commit 5e1c4e2c06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 6 deletions

View file

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

View file

@ -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()