From e24d2163a51f47c36346edd12906e3d91d2bde51 Mon Sep 17 00:00:00 2001 From: Mike Bloy Date: Fri, 27 May 2022 16:37:58 -0500 Subject: [PATCH] add ability to read from config file --- cmd/root.go | 27 ++++++++++++++++++++++++++- cmd/root_test.go | 15 +++++++++++++++ go.mod | 1 - go.sum | 2 -- main.go | 1 - main_test.go | 7 +++++++ modd.conf | 2 +- 7 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 cmd/root_test.go create mode 100644 main_test.go diff --git a/cmd/root.go b/cmd/root.go index b3c9369..b836a92 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "log" "os" "git.bloy.org/mike/hasshelper/web" @@ -10,6 +11,8 @@ import ( "go.uber.org/zap" ) +var cfgFile string + var rootCmd = &cobra.Command{ Use: "hasshelper", Short: "Helper for Home Assistant installations.", @@ -34,7 +37,6 @@ var rootCmd = &cobra.Command{ // Execute will kick off cobra's processing of the root command func Execute() { - viper.SetDefault("Version", "1.2.3") if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) @@ -42,7 +44,20 @@ func Execute() { } func init() { + + rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", + "config file (default is $HOME/.config/hasshelper/config.toml)") cobra.OnInitialize(initConfig) + const dirName = "hasshelper" + var userConfigDir, err = os.UserConfigDir() + if err == nil { + viper.AddConfigPath( + fmt.Sprintf("%s%c%s", userConfigDir, os.PathSeparator, dirName)) + } else { + log.Println("could not locate user config dir:", err) + } + viper.AddConfigPath(fmt.Sprintf("/etc%c%s", os.PathSeparator, dirName)) + viper.SetConfigName("config.toml") viper.SetEnvPrefix("hasshelper") viper.Set("version", gitVersion) viper.SetDefault("deployment", "prod") @@ -50,6 +65,16 @@ func init() { func initConfig() { viper.AutomaticEnv() + if cfgFile != "" { + viper.SetConfigFile(cfgFile) + } + if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); ok { + log.Printf("no config file found. Environment only") + } else { + log.Fatalf("error reading config file: %v\n", err) + } + } expected_config := []string{ "deployment", "image_dir", diff --git a/cmd/root_test.go b/cmd/root_test.go new file mode 100644 index 0000000..89abf88 --- /dev/null +++ b/cmd/root_test.go @@ -0,0 +1,15 @@ +package cmd + +import ( + "testing" + + "github.com/spf13/viper" +) + +func TestInitConfig(t *testing.T) { + viper.Set("deployment", "testing") + viper.Set("image_dir", "/tmp") + viper.Set("webserver_port", "8080") + + initConfig() +} diff --git a/go.mod b/go.mod index 16e0d2b..682fbb1 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.18 require ( github.com/cortesi/devd v0.0.0-20200427000907-c1a3bfba27d8 github.com/cortesi/modd v0.0.0-20211215124449-6083f9d1c171 - github.com/joho/godotenv v1.4.0 github.com/mdomke/git-semver/v6 v6.2.0 github.com/spf13/cobra v1.4.0 github.com/spf13/viper v1.11.0 diff --git a/go.sum b/go.sum index 347857c..9982ed9 100644 --- a/go.sum +++ b/go.sum @@ -197,8 +197,6 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= -github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= -github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/juju/ratelimit v1.0.1 h1:+7AIFJVQ0EQgq/K9+0Krm7m530Du7tIz0METWzN0RgY= diff --git a/main.go b/main.go index bcb3a61..d9cbbdf 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import ( "git.bloy.org/mike/hasshelper/cmd" - _ "github.com/joho/godotenv/autoload" ) //go:generate go run ./gen_version.go -p cmd -f cmd/git_version.go diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..f44fe01 --- /dev/null +++ b/main_test.go @@ -0,0 +1,7 @@ +package main + +import "testing" + +func TestTesting(t *testing.T) { + t.Log("Compilation and testing is fine") +} diff --git a/modd.conf b/modd.conf index 08c2080..1258eba 100644 --- a/modd.conf +++ b/modd.conf @@ -10,5 +10,5 @@ prep: go mod tidy prep: go test @dirmods prep: go build - daemon +sigterm: ./hasshelper + daemon +sigterm: ./hasshelper -c ../hasshelper.toml }