Skip to content

Commit

Permalink
feat: load decrypted storage file into memory so we don't need to dec…
Browse files Browse the repository at this point in the history
…rypt multiple times
  • Loading branch information
Alexander Huck committed Oct 16, 2023
1 parent 6ff961d commit 8211a4c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions internal/auth/yaml_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@ import (
"io/fs"
"os"

"github.com/charmbracelet/log"
"github.com/inovex/CalendarSync/internal/config"
"gopkg.in/yaml.v3"
)

type YamlStorage struct {
StoragePath string
StorageEncryptionKey string
// Holds the decrypted CalendarAuth Config in memory, so the file does not have to be read multiple times
DecryptedAuth []CalendarAuth
}

func (y *YamlStorage) Setup(config config.AuthStorage, encryptionPassphrase string) error {
y.StorageEncryptionKey = encryptionPassphrase
y.StoragePath = config.Config["path"].(string)

log.Debug("Loading saved auth data into memory")
stor, err := y.readAndParseFile()
if errors.Is(err, os.ErrNotExist) {
log.Debug("No storage file found, skipping loading from memory")
} else {
// Put the data into DecryptedAuth
y.DecryptedAuth = stor.Calendars
}
return nil
}

Expand Down Expand Up @@ -47,10 +59,25 @@ func (y *YamlStorage) WriteCalendarAuth(newCal CalendarAuth) (bool, error) {
return false, err
}

// Adding freshly written CalendarAuth to memory
// Probably unneeded, the next time this data will be retrieved is on the next calendarsync run
log.Debugf("Adding calendar auth for cal %s to memory", newCal.CalendarID)
y.DecryptedAuth = append(y.DecryptedAuth, newCal)

return true, nil
}

func (y *YamlStorage) ReadCalendarAuth(calendarID string) (*CalendarAuth, error) {
// if we already decrypted the file, read from memory
if len(y.DecryptedAuth) > 0 {
for _, cal := range y.DecryptedAuth {
if cal.CalendarID == calendarID {
log.Debug("loaded auth data from memory", "calendarID", cal.CalendarID)
return &cal, nil
}
}
}

file, err := y.readAndParseFile()
if err != nil {
return nil, ignoreNoFile(err)
Expand Down

0 comments on commit 8211a4c

Please sign in to comment.