add new write function to persist to disk

This commit is contained in:
Richard Nordström 2024-09-23 20:26:43 +02:00
parent 89d3b1a4df
commit d037ec32a1
No known key found for this signature in database
GPG Key ID: ACCB352EC60AF27C
1 changed files with 10 additions and 10 deletions

View File

@ -52,11 +52,7 @@ func (c *FileTokenCache) Store(key string, t *oauth2.Token) error {
c.Tokens = map[string]*oauth2.Token{}
}
c.Tokens[key] = t
raw, err := json.MarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
return os.WriteFile(c.fileLocation, raw, ownerReadWrite)
return c.write()
}
func (c *FileTokenCache) Lookup(key string) (*oauth2.Token, error) {
@ -88,11 +84,7 @@ func (c *FileTokenCache) Delete(key string) error {
return ErrNotConfigured
}
delete(c.Tokens, key)
raw, err := json.MarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
return os.WriteFile(c.fileLocation, raw, ownerReadWrite)
return c.write()
}
func (c *FileTokenCache) location() (string, error) {
@ -127,4 +119,12 @@ func (c *FileTokenCache) load() error {
return nil
}
func (c *FileTokenCache) write() error {
raw, err := json.MarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("marshal: %w", err)
}
return os.WriteFile(c.fileLocation, raw, ownerReadWrite)
}
var _ TokenCache = (*FileTokenCache)(nil)