Add .env support and allow overriding config with environment settings

This commit is contained in:
andig 2022-12-04 13:05:01 +01:00
parent 84ebd21d52
commit e2200917ea
4 changed files with 9 additions and 4 deletions

View file

@ -21,8 +21,9 @@ import (
"github.com/evcc-io/evcc/util/pipe"
"github.com/evcc-io/evcc/util/sponsor"
"github.com/evcc-io/evcc/util/telemetry"
"github.com/prometheus/client_golang/prometheus/promhttp"
_ "github.com/joho/godotenv/autoload"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
@ -86,6 +87,7 @@ func initConfig() {
viper.SetConfigName("evcc")
}
viper.SetEnvPrefix("evcc")
viper.AutomaticEnv() // read in environment variables that match
// print version

View file

@ -68,9 +68,9 @@ func configureEnvironment(cmd *cobra.Command, conf config) (err error) {
err = machine.CustomID(conf.Plant)
}
// setup sponsorship
if err == nil && conf.SponsorToken != "" {
err = sponsor.ConfigureSponsorship(conf.SponsorToken)
// setup sponsorship (allow env override)
if token := viper.GetString("sponsortoken"); err == nil && token != "" {
err = sponsor.ConfigureSponsorship(token)
}
// setup translations

1
go.mod
View file

@ -51,6 +51,7 @@ require (
github.com/jeremywohl/flatten v1.0.1
github.com/jinzhu/copier v0.3.5
github.com/joeshaw/carwings v0.0.0-20220711204617-d90801cf2848
github.com/joho/godotenv v1.4.0
github.com/jpfielding/go-http-digest v0.0.0-20211006141426-fbc93758452e
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/koron/go-ssdp v0.0.3

2
go.sum
View file

@ -523,6 +523,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joeshaw/carwings v0.0.0-20220711204617-d90801cf2848 h1:mUoi+B77PKkv/pYCR7uOh5ouZdgzFAwOZ3HU8Jr1P+4=
github.com/joeshaw/carwings v0.0.0-20220711204617-d90801cf2848/go.mod h1:tB0OlpicmRVTL1Vksc5XRiYo+wkK2kl/GI7eGMIl6Rs=
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/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jpfielding/go-http-digest v0.0.0-20211006141426-fbc93758452e h1:iLmt7czLE9ophoU4xZcckVNCXp+auTvVSQ9MGTWbgBY=
github.com/jpfielding/go-http-digest v0.0.0-20211006141426-fbc93758452e/go.mod h1:oLt6zF2euTyCg2Cxz01B3VC5SJcPpKpYnOAqvO+480s=