Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: add database flag; improve systemd db check #17993

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ const (
)

var (
log = util.NewLogger("main")
cfgFile string
log = util.NewLogger("main")
cfgFile string
cfgDatabase string

ignoreEmpty = "" // ignore empty keys
ignoreLogs = []string{"log"} // ignore log messages, including warn/error
Expand All @@ -63,6 +64,7 @@ func init() {

// global options
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file (default \"~/evcc.yaml\" or \"/etc/evcc.yaml\")")
rootCmd.PersistentFlags().StringVarP(&cfgDatabase, "database", "d", "", "Database location (default \"~/.evcc/evcc.db\")")
rootCmd.PersistentFlags().BoolP("help", "h", false, "Help")
rootCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
rootCmd.PersistentFlags().Bool(flagIgnoreDatabase, false, flagIgnoreDatabaseDescription)
Expand Down Expand Up @@ -97,6 +99,9 @@ func initConfig() {

viper.SetConfigName("evcc")
}
if cfgDatabase != "" {
viper.Set("Database.Dsn", cfgDatabase)
}

viper.SetEnvPrefix("evcc")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
Expand Down
45 changes: 25 additions & 20 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ import (
"golang.org/x/text/currency"
)

var defaultDSN = "~/.evcc/evcc.db"

var conf = globalconfig.All{
Interval: 10 * time.Second,
Log: "info",
Expand All @@ -68,7 +70,7 @@ var conf = globalconfig.All{
},
Database: globalconfig.DB{
Type: "sqlite",
Dsn: "~/.evcc/evcc.db",
Dsn: defaultDSN,
},
}

Expand All @@ -81,22 +83,6 @@ func nameValid(name string) error {
return nil
}

func tokenDanger(conf []config.Named) bool {
problematic := []string{"tesla", "psa", "opel", "citroen", "ds", "peugeot"}

for _, cc := range conf {
if slices.Contains(problematic, cc.Type) {
return true
}
template, ok := cc.Other["template"].(string)
if ok && cc.Type == "template" && slices.Contains(problematic, template) {
return true
}
}

return false
}

func loadConfigFile(conf *globalconfig.All, checkDB bool) error {
err := viper.ReadInConfig()

Expand All @@ -113,16 +99,26 @@ func loadConfigFile(conf *globalconfig.All, checkDB bool) error {
}

// check service database
if _, err := os.Stat(serviceDB); err == nil && checkDB && conf.Database.Dsn != serviceDB && tokenDanger(conf.Vehicles) {
log.FATAL.Fatal(`
if _, err := os.Stat(serviceDB); err == nil && checkDB {
if conf.Database.Dsn == defaultDSN {
// no explicit db path given, use service database
if isWritable(serviceDB) {
conf.Database.Dsn = serviceDB
log.INFO.Println("system database found, using it.")
} else {
log.FATAL.Fatal(`Found systemd service database at "` + serviceDB + `", but it is not writable. Try running the command with 'sudo'.`)
}
} else {
// service database found, but explicit db path given
log.FATAL.Fatal(`

Found systemd service database at "` + serviceDB + `", evcc has been invoked with database "` + conf.Database.Dsn + `".
Running evcc with vehicles configured in evcc.yaml may lead to expiring the yaml configuration's vehicle tokens.
This is due to the fact, that the token refresh will be saved to the local instead of the service's database.
If you have vehicles with touchy tokens like PSA or Tesla, make sure to remove vehicle configuration from the yaml file.

If you know what you're doing, you can run evcc ignoring the service database with the --ignore-db flag.
`)
}
}

// parse log levels after reading config
Expand All @@ -133,6 +129,15 @@ If you know what you're doing, you can run evcc ignoring the service database wi
return err
}

func isWritable(filePath string) bool {
file, err := os.OpenFile(filePath, os.O_WRONLY, 0666)
if err != nil {
return false
}
file.Close()
return true
}

func configureCircuits(conf []config.Named) error {
// migrate settings
if settings.Exists(keys.Circuits) {
Expand Down
Loading