swap-logging #1

Merged
mike merged 3 commits from swap-logging into main 2022-05-28 00:11:44 +00:00
7 changed files with 49 additions and 6 deletions
Showing only changes of commit e24d2163a5 - Show all commits

View File

@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"git.bloy.org/mike/hasshelper/web" "git.bloy.org/mike/hasshelper/web"
@ -10,6 +11,8 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
var cfgFile string
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "hasshelper", Use: "hasshelper",
Short: "Helper for Home Assistant installations.", 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 // Execute will kick off cobra's processing of the root command
func Execute() { func Execute() {
viper.SetDefault("Version", "1.2.3")
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
@ -42,7 +44,20 @@ func Execute() {
} }
func init() { func init() {
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "",
"config file (default is $HOME/.config/hasshelper/config.toml)")
cobra.OnInitialize(initConfig) 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.SetEnvPrefix("hasshelper")
viper.Set("version", gitVersion) viper.Set("version", gitVersion)
viper.SetDefault("deployment", "prod") viper.SetDefault("deployment", "prod")
@ -50,6 +65,16 @@ func init() {
func initConfig() { func initConfig() {
viper.AutomaticEnv() 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{ expected_config := []string{
"deployment", "deployment",
"image_dir", "image_dir",

15
cmd/root_test.go Normal file
View 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
View File

@ -5,7 +5,6 @@ go 1.18
require ( require (
github.com/cortesi/devd v0.0.0-20200427000907-c1a3bfba27d8 github.com/cortesi/devd v0.0.0-20200427000907-c1a3bfba27d8
github.com/cortesi/modd v0.0.0-20211215124449-6083f9d1c171 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/mdomke/git-semver/v6 v6.2.0
github.com/spf13/cobra v1.4.0 github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.11.0 github.com/spf13/viper v1.11.0

2
go.sum
View File

@ -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/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.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= 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.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/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/juju/ratelimit v1.0.1 h1:+7AIFJVQ0EQgq/K9+0Krm7m530Du7tIz0METWzN0RgY= github.com/juju/ratelimit v1.0.1 h1:+7AIFJVQ0EQgq/K9+0Krm7m530Du7tIz0METWzN0RgY=

View File

@ -3,7 +3,6 @@ package main
import ( import (
"git.bloy.org/mike/hasshelper/cmd" "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 //go:generate go run ./gen_version.go -p cmd -f cmd/git_version.go

7
main_test.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "testing"
func TestTesting(t *testing.T) {
t.Log("Compilation and testing is fine")
}

View File

@ -10,5 +10,5 @@
prep: go mod tidy prep: go mod tidy
prep: go test @dirmods prep: go test @dirmods
prep: go build prep: go build
daemon +sigterm: ./hasshelper daemon +sigterm: ./hasshelper -c ../hasshelper.toml
} }