package config import ( "log/slog" "os" "github.com/spf13/viper" ) // Config . type Config struct { MySubaru MySubaru `json:"mysubaru" yaml:"mysubaru"` Timezone string `json:"timezone" yaml:"timezone"` Logging *Logging `json:"logging" yaml:"logging"` } // Emporia . type MySubaru struct { Credentials Credentials `json:"credentials" yaml:"credentials"` Region string `json:"region" yaml:"region"` AutoReconnect bool `json:"auto_reconnect" yaml:"auto_reconnect"` } // Credentials . type Credentials struct { Username string `json:"username" yaml:"username"` Password string `json:"password" yaml:"password"` PIN string `json:"pin" yaml:"pin"` DeviceID string `json:"deviceid" yaml:"deviceid"` DeviceName string `json:"devicename" yaml:"devicename"` } // Logging . type Logging struct { Level string `json:"level" yaml:"level"` Output string `json:"output" yaml:"output"` Source bool `json:"source,omitempty" yaml:"source,omitempty"` } func New() (*Config, error) { viper.SetConfigName("config") // name of config file (without extension) viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name viper.AddConfigPath(".") // optionally look for config in the working directory viper.AddConfigPath("/etc/mysubaru") // optionally look for config in the working directory viper.AutomaticEnv() viper.SetDefault("Timezone", "America/New_York") viper.SetDefault("MySubaru.AutoReconnect", true) viper.SetDefault("Logging.Level", "INFO") viper.SetDefault("Logging.Output", "TEXT") viper.SetDefault("Logging.Source", "false") err := viper.ReadInConfig() // Find and read the config file if err != nil { // Handle errors reading the config file slog.Error("cannot open config file", "error", err.Error()) os.Exit(1) } // viper.WatchConfig() // viper.OnConfigChange(func(e fsnotify.Event) { // log.Println("Config file changed:", e.Name) // }) var config Config if err := viper.Unmarshal(&config); err != nil { slog.Error("cannot unmarshal config file", "error", err.Error()) os.Exit(1) } return &config, nil }