store: catch when file does not exist
This commit is contained in:
parent
7517cccae9
commit
7620f85da9
9
store.go
9
store.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
@ -26,11 +27,17 @@ type Store string
|
|||||||
func (s Store) Set(key string, value string) {
|
func (s Store) Set(key string, value string) {
|
||||||
err := os.WriteFile(path.Join(string(s), key), []byte(value), 0600)
|
err := os.WriteFile(path.Join(string(s), key), []byte(value), 0600)
|
||||||
if err != nil {
|
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) {
|
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))
|
data, err := os.ReadFile(path.Join(string(s), key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", nil
|
||||||
|
Loading…
Reference in New Issue
Block a user