Skip to content

Commit

Permalink
Allow passing configuration file location on the command line.
Browse files Browse the repository at this point in the history
  • Loading branch information
haimgel committed Feb 7, 2023
1 parent f1031d5 commit e878785
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang 1.18.6
golangci-lint 1.49.0
19 changes: 9 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,7 @@ func Load(version string, exit func(int), args []string) (*ApplicationConfig, er
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

configDir, err := defaultConfigHome()
if err != nil {
return nil, err
}
viper.SetConfigType("yaml")
viper.SetConfigName("config")
viper.AddConfigPath(configDir)
viper.SetConfigFile(viper.GetString("config"))
viper.SetDefault("app-id", AppName)
err = viper.ReadInConfig()
if err != nil {
Expand All @@ -64,6 +58,7 @@ func Load(version string, exit func(int), args []string) (*ApplicationConfig, er
}

func processCommandLineArguments(versionStr string, exit func(int), args []string) {
pflag.StringP("config", "c", defaultConfigFile(), "Configuration file path")
pflag.StringP("mqtt.broker", "b", "tcp://localhost:1883", "MQTT broker")
pflag.StringP("log.path", "l", defaultLogFile(), "Log file path")
helpFlag := pflag.BoolP("help", "h", false, "This help message")
Expand All @@ -81,11 +76,15 @@ func processCommandLineArguments(versionStr string, exit func(int), args []strin

func defaultConfigHome() (string, error) {
cfgDir, err := os.UserConfigDir()
return filepath.Join(cfgDir, AppName), err
}

func defaultConfigFile() string {
configDir, err := defaultConfigHome()
if err != nil {
return "", err
return ""
}
err = os.MkdirAll(cfgDir, 0750)
return filepath.Join(cfgDir, AppName), err
return filepath.Join(configDir, "config.yaml")
}

func defaultLogFile() string {
Expand Down

0 comments on commit e878785

Please sign in to comment.