-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.go
108 lines (83 loc) · 2.56 KB
/
fs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package repository
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/williammartin/jobcannon"
)
var errorAbsent = errors.New("record is absent")
func IsAbsent(err error) bool {
return errors.Is(err, errorAbsent)
}
type Storage struct {
dirPath string
}
func (s *Storage) filePath(catalogId jobcannon.CatalogId, jobId jobcannon.JobId) string {
return filepath.Join(s.dirPath, fmt.Sprintf("%d-%d.json", catalogId, jobId))
}
func Filesystem(dirPath string) (*Storage, error) {
storageDirExists, err := dirExists(dirPath)
if err != nil {
return &Storage{}, fmt.Errorf("unexpected error while checking existence of %s directory: %w", dirPath, err)
}
if !storageDirExists {
return &Storage{}, fmt.Errorf("storage directory '%s' does not exist", dirPath)
}
return &Storage{dirPath: dirPath}, nil
}
func (s *Storage) Persist(review jobcannon.ExpressionOfInterest) error {
marshalledReview, err := json.Marshal(review)
if err != nil {
return fmt.Errorf("failed to marshal job review %v: %w", review, err)
}
if err := ioutil.WriteFile(s.filePath(review.CatalogId, review.JobId), marshalledReview, 0644); err != nil {
return fmt.Errorf("failed to persist to filesystem: %w", err)
}
return nil
}
func (s *Storage) Exists(catalogId jobcannon.CatalogId, jobId jobcannon.JobId) (bool, error) {
return fileExists(s.filePath(catalogId, jobId))
}
func (s *Storage) Load(catalogId jobcannon.CatalogId, jobId jobcannon.JobId) (jobcannon.ExpressionOfInterest, error) {
content, err := ioutil.ReadFile(s.filePath(catalogId, jobId))
if os.IsNotExist(err) {
return jobcannon.ExpressionOfInterest{}, errorAbsent
}
if err != nil {
return jobcannon.ExpressionOfInterest{}, fmt.Errorf("failed to load content from filesystem: %w", err)
}
var jobReview *jobcannon.ExpressionOfInterest = new(jobcannon.ExpressionOfInterest)
if err := json.Unmarshal(content, jobReview); err != nil {
return jobcannon.ExpressionOfInterest{}, fmt.Errorf("failed to load marshal file content: %w", err)
}
return *jobReview, nil
}
func dirExists(dirPath string) (bool, error) {
f, err := os.Stat(dirPath)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
if !f.IsDir() {
return false, fmt.Errorf("path exists but was not a directory")
}
return true, nil
}
func fileExists(filePath string) (bool, error) {
f, err := os.Stat(filePath)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
if f.IsDir() {
return false, fmt.Errorf("path exists but was a directory")
}
return true, nil
}