/
jsonfilestore.go
144 lines (115 loc) · 2.72 KB
/
jsonfilestore.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2019 Grabtaxi Holdings PTE LTE (GRAB), All rights reserved.
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
*/
package state
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/mitchellh/go-homedir"
)
// JSONFileStore is a JSON-based storage for scan histories
type JSONFileStore struct {
DataFile *os.File
Records map[string]*History
}
// Initialize ...
func (fs *JSONFileStore) Initialize(filepath string) error {
file, err := os.OpenFile(filepath, os.O_RDWR, 0644)
if err != nil {
return err
}
recordBytes, err := ioutil.ReadAll(file)
if err != nil {
return err
}
fs.DataFile = file
fs.Records = map[string]*History{}
var records []*History
err = json.Unmarshal(recordBytes, &records)
if err != nil {
return err
}
for _, record := range records {
fs.Records[record.GetMapKey()] = record
}
return nil
}
// Close cleans up resources
func (fs *JSONFileStore) Close() {
if fs.DataFile != nil {
_ = fs.DataFile.Close()
}
}
// GetDefaultStorePath returns default store file path,
// creates dir and file is not found
func (fs *JSONFileStore) GetDefaultStorePath() (string, error) {
userHomeDir, err := homedir.Dir()
if err != nil {
return "", err
}
storeDirPath := path.Join(userHomeDir, DefaultStoreDir)
storeFilePath := path.Join(storeDirPath, DefaultStoreFile)
if _, err := os.Stat(storeFilePath); err != nil {
if os.IsNotExist(err) {
// attempt create dir and file
err = fs.createDefaultStoreFile(storeDirPath, DefaultStoreFile)
if err != nil {
return "", err
}
return storeFilePath, nil
}
return "", err
}
return storeFilePath, nil
}
// Get retrieves history from store
func (fs *JSONFileStore) Get(gitprovider, repoID string) *History {
val, exists := fs.Records[fmt.Sprintf("%s:%s", gitprovider, repoID)]
if exists {
return val
}
return nil
}
// Save persists records to file
func (fs *JSONFileStore) Save(history *History) error {
fs.Records[history.GetMapKey()] = history
var histories []*History
for _, val := range fs.Records {
histories = append(histories, val)
}
jsonBytes, err := json.Marshal(histories)
if err != nil {
return err
}
err = fs.DataFile.Truncate(0)
if err != nil {
return err
}
_, err = fs.DataFile.WriteAt(jsonBytes, 0)
if err != nil {
return err
}
return nil
}
func (fs *JSONFileStore) createDefaultStoreFile(dirPath, filename string) error {
err := os.MkdirAll(dirPath, 0700)
if err != nil {
return err
}
storeFile, err := os.Create(path.Join(dirPath, filename))
if err != nil {
return err
}
_, err = storeFile.Write([]byte("[]"))
if err != nil {
return err
}
defer func(f *os.File) {
_ = f.Close()
}(storeFile)
return nil
}