From fef8f3a51b576385691ffc6c4d10fcdeeaed9153 Mon Sep 17 00:00:00 2001 From: Seth Terashima Date: Mon, 25 Nov 2024 15:50:47 -0800 Subject: [PATCH] Enable session cache by default A session cache can reduce the number of Fleet API calls made by clients, but needed to be explicitly enabled by the user. With this change, the cache will be enabled by default and saved to ~/.tesla-cache.json. This path can be overridden using the `-session-cache` command-line flag or by setting TESLA_CACHE_FILE, as before. Users can add the `-disable-session-cache` command-line flag to disable the feature. --- pkg/cli/config.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/pkg/cli/config.go b/pkg/cli/config.go index 0a64ea0..9e2aec0 100644 --- a/pkg/cli/config.go +++ b/pkg/cli/config.go @@ -54,6 +54,7 @@ import ( "fmt" "io/fs" "os" + "path/filepath" "sort" "strings" @@ -146,6 +147,7 @@ type Config struct { TokenFilename string KeyFilename string CacheFilename string + DisableCache bool Backend keyring.Config BackendType backendType Debug bool // Enable keyring debug messages @@ -185,7 +187,8 @@ func (c *Config) RegisterCommandLineFlags() { if !c.Flags.isSet(FlagVIN) { log.Debug("FlagPrivateKey is set but FlagVIN is not. A VIN is required to send vehicle commands.") } - flag.StringVar(&c.CacheFilename, "session-cache", "", "Load session info cache from `file`. Defaults to $TESLA_CACHE_FILE.") + flag.StringVar(&c.CacheFilename, "session-cache", "", "Load session info cache from `file`. Defaults to $TESLA_CACHE_FILE then ~/.tesla-cache.json.") + flag.BoolVar(&c.DisableCache, "disable-session-cache", false, "Disable the session info cache.") flag.StringVar(&c.KeyringKeyName, "key-name", "", "System keyring `name` for private key. Defaults to $TESLA_KEY_NAME.") flag.StringVar(&c.KeyFilename, "key-file", "", "A `file` containing private key. Defaults to $TESLA_KEY_FILE.") flag.Var(&c.Domains, "domain", "Domains to connect to (can be repeated; omit for all)") @@ -236,8 +239,13 @@ func (c *Config) ReadFromEnvironment() { } } if c.Flags.isSet(FlagPrivateKey) { - if c.CacheFilename == "" { + if !c.DisableCache && c.CacheFilename == "" { c.CacheFilename = os.Getenv(EnvTeslaCacheFile) + if c.CacheFilename == "" { + if homeDir := os.Getenv("HOME"); homeDir != "" { + c.CacheFilename = filepath.Join(homeDir, ".tesla-cache.json") + } + } log.Debug("Set session cache file to '%s'", c.CacheFilename) } if c.KeyringKeyName == "" && c.KeyFilename == "" { @@ -286,11 +294,12 @@ func (c *Config) ReadFromEnvironment() { // If c.CacheFilename is not set or no vehicle handshake has occurred, then this method does // nothing. func (c *Config) UpdateCachedSessions(v *vehicle.Vehicle) { - if c.CacheFilename != "" && c.sessions != nil { - v.UpdateCachedSessions(c.sessions) - if err := c.sessions.ExportToFile(c.CacheFilename); err != nil { - log.Error("Error updating cache: %s", err) - } + if c.CacheFilename == "" || c.sessions == nil { + return + } + v.UpdateCachedSessions(c.sessions) + if err := c.sessions.ExportToFile(c.CacheFilename); err != nil { + log.Error("Error updating cache: %s", err) } }