-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
46 lines (36 loc) · 900 Bytes
/
database.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
package main
import (
"crypto/sha256"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
type database struct {
Path string
}
func (db *database) caseStatusPath(caseNumber string) string {
hash := sha256.New()
io.WriteString(hash, caseNumber)
hashedCaseNumber := fmt.Sprintf("%x", hash.Sum(nil))
return filepath.Join(db.Path, hashedCaseNumber)
}
func (db *database) create() {
err := os.Mkdir(db.Path, 0755)
checkFileError(err)
}
func (db *database) saveCaseStatus(caseNumber string, caseStatus *caseStatus) {
filePath := db.caseStatusPath(caseNumber)
f, err := os.Create(filePath)
check(err)
defer f.Close()
_, err = f.WriteString(caseStatus.Status)
check(err)
}
func (db *database) loadCaseStatus(caseNumber string) caseStatus {
filePath := db.caseStatusPath(caseNumber)
dat, err := ioutil.ReadFile(filePath)
checkFileError(err)
return caseStatus{Status: string(dat)}
}