add ability to read from config file
This commit is contained in:
parent
00cb7b3934
commit
e24d2163a5
27
cmd/root.go
27
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",
|
||||
|
||||
15
cmd/root_test.go
Normal file
15
cmd/root_test.go
Normal file
@ -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()
|
||||
}
|
||||
1
go.mod
1
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
|
||||
|
||||
2
go.sum
2
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=
|
||||
|
||||
1
main.go
1
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
|
||||
|
||||
7
main_test.go
Normal file
7
main_test.go
Normal file
@ -0,0 +1,7 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestTesting(t *testing.T) {
|
||||
t.Log("Compilation and testing is fine")
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user