91 lines
2.2 KiB
Go
91 lines
2.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"git.bloy.org/mike/hasshelper/web"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var cfgFile string
|
|
|
|
var rootCmd = &cobra.Command{
|
|
Use: "hasshelper",
|
|
Short: "Helper for Home Assistant installations.",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
var logger *zap.Logger
|
|
var err error
|
|
if viper.GetString("deployment") == "prod" {
|
|
logger, err = zap.NewProduction(zap.WithCaller(false))
|
|
} else {
|
|
logger, err = zap.NewDevelopment(zap.WithCaller(false))
|
|
}
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
logger.Sugar().With("version", viper.GetString("version")).Info("HASSHelper startup")
|
|
exitchan := make(chan bool)
|
|
web.Run(logger, exitchan)
|
|
<-exitchan // run the main command until one of the goroutines is done
|
|
},
|
|
}
|
|
|
|
// Execute will kick off cobra's processing of the root command
|
|
func Execute() {
|
|
if err := rootCmd.Execute(); err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
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",
|
|
"version",
|
|
"webserver_port",
|
|
}
|
|
for _, key := range expected_config {
|
|
if !viper.IsSet(key) {
|
|
fmt.Fprintf(os.Stderr, "Missing configuration value: %s\n", key)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|