store: catch when file does not exist

This commit is contained in:
Aaron Bieber 2024-03-31 15:12:44 -06:00
parent 7517cccae9
commit 7620f85da9
No known key found for this signature in database

View File

@ -1,6 +1,7 @@
package main
import (
"errors"
"fmt"
"log"
"os"
@ -26,11 +27,17 @@ type Store string
func (s Store) Set(key string, value string) {
err := os.WriteFile(path.Join(string(s), key), []byte(value), 0600)
if err != nil {
log.Println(err)
log.Println(fmt.Errorf("failed to set %q: %s", key, err))
}
}
func (s Store) Get(key string) (string, error) {
keyPath := path.Join(string(s), key)
_, err := os.Stat(keyPath)
if errors.Is(err, os.ErrNotExist) {
return "", os.ErrNotExist
}
data, err := os.ReadFile(path.Join(string(s), key))
if err != nil {
return "", nil