-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlefile.go
39 lines (34 loc) · 913 Bytes
/
handlefile.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
package qiic
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
)
const fileName = ".qiic-json"
var filePath = path.Join(os.Getenv("HOME"), fileName)
// Save saves the json file into local.
func Save(arts []*Article) error {
bytes, err := json.Marshal(arts) // encoding
if err != nil {
return fmt.Errorf("unable to marshal savedArticles")
}
err = ioutil.WriteFile(filePath, bytes, 0644)
if err != nil {
return fmt.Errorf("unable to write to %s", filePath)
}
return nil
}
// Load loads the json file from local.
func Load() ([]*Article, error) {
body, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("unable to load %s\nGet your articles with executing the following command\n$ qiic u", filePath)
}
var articles []*Article
if err := json.Unmarshal(body, &articles); err != nil {
return nil, fmt.Errorf("unable to unmashal body")
}
return articles, nil
}