Skip to content

Commit

Permalink
Merge pull request #8 from observeinc/alew/update-config-path
Browse files Browse the repository at this point in the history
Update default config path
  • Loading branch information
obs-gh-alexlew authored May 20, 2024
2 parents da50336 + 5eccf32 commit 4a751a4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ nfpms:
# Your app's homepage.
homepage: https://observeinc.com/

maintainer: Konstantin Tikhonov <konstantin.tikhonov@observeinc.com>
maintainer: Alex Lew <alex.lew@observeinc.com>

description: |-
Observe agent.
Expand Down
14 changes: 14 additions & 0 deletions cmd/config/confighandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"os"
"runtime"

"observe/agent/cmd/connections"

Expand Down Expand Up @@ -61,3 +62,16 @@ func GetOverrideConfigFile(sub *viper.Viper) (string, error) {
}
return f.Name(), nil
}

func GetDefaultConfigFolder() string {
switch os := runtime.GOOS; os {
case "darwin":
return "$HOME"
case "windows":
return "%PROGRAMDATA%\\observe-agent\\config"
case "linux":
return "/etc/observe-agent"
default:
return "/etc/observe-agent"
}
}
11 changes: 4 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"fmt"
"observe/agent/cmd/config"
"os"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -44,7 +45,7 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

RootCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file (default is $HOME/.observe-agent.yaml)")
RootCmd.PersistentFlags().StringVar(&CfgFile, "config", "", "config file path")

// Cobra also supports local flags, which will only run
// when this action is called directly.
Expand All @@ -57,14 +58,10 @@ func InitConfig() {
// Use config file from the flag.
viper.SetConfigFile(CfgFile)
} else {
// Find home directory.
home, err := os.UserHomeDir()
cobra.CheckErr(err)

// Search config in home directory with name ".observe-agent" (without extension).
viper.AddConfigPath(home)
viper.AddConfigPath(config.GetDefaultConfigFolder())
viper.SetConfigType("yaml")
viper.SetConfigName(".observe-agent")
viper.SetConfigName("observe-agent")
}

viper.AutomaticEnv() // read in environment variables that match
Expand Down

0 comments on commit 4a751a4

Please sign in to comment.