Skip to content

Commit

Permalink
Enable session cache by default
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Seth Terashima authored and sethterashima committed Dec 17, 2024
1 parent 8e16f0d commit fef8f3a
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pkg/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import (
"fmt"
"io/fs"
"os"
"path/filepath"
"sort"
"strings"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit fef8f3a

Please sign in to comment.