-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
114 lines (91 loc) · 2.72 KB
/
file.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
package config
import (
"encoding/json"
"io"
"log"
"os"
"path/filepath"
"sync"
"github.com/pkg/errors"
)
// dirPath indicates the path to the config. This variable is created when
// __init is called.
var dirPath string
// Singleton to initialize the config directories once.
var __initonce sync.Once
func __init() {
// Load the config dir:
d, err := os.UserConfigDir()
if err != nil {
log.Fatalln("Failed to get config dir:", err)
}
// Fill Path:
dirPath = filepath.Join(d, "cchat-gtk")
// Ensure it exists:
if err := os.Mkdir(dirPath, 0755|os.ModeDir); err != nil && !os.IsExist(err) {
log.Fatalln("Failed to make config dir:", err)
}
}
// PrettyMarshal pretty marshals v into dst as formatted JSON.
func PrettyMarshal(dst io.Writer, v interface{}) error {
enc := json.NewEncoder(dst)
enc.SetIndent("", "\t")
return enc.Encode(v)
}
// DirPath returns the config directory.
func DirPath() string {
// Ensure that files and folders are initialized.
__initonce.Do(__init)
return dirPath
}
// SaveToFile saves the given bytes into the given filename. The filename will
// be prepended with the config directory.
func SaveToFile(file string, v []byte) error {
file = filepath.Join(DirPath(), file)
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_SYNC|os.O_TRUNC, 0644)
if err != nil {
return errors.Wrap(err, "failed to open file")
}
defer f.Close()
if _, err := f.Write(v); err != nil {
return errors.Wrap(err, "failed to write")
}
return nil
}
// MarshalToFile marshals the given interface into the given filename. The
// filename will be prepended with the config directory.
func MarshalToFile(file string, from interface{}) error {
file = filepath.Join(DirPath(), file)
dir := filepath.Dir(file)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return errors.Wrap(err, "failed to create config dir")
}
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_SYNC|os.O_TRUNC, 0644)
if err != nil {
return errors.Wrap(err, "failed to open file")
}
defer f.Close()
if err := PrettyMarshal(f, from); err != nil {
return errors.Wrap(err, "failed to marshal given struct")
}
return nil
}
// UnmarshalFromFile unmarshals the given filename to the given interface. The
// filename will be prepended with the config directory. IsNotExist errors are
// ignored.
func UnmarshalFromFile(file string, to interface{}) error {
file = filepath.Join(DirPath(), file)
f, err := os.OpenFile(file, os.O_RDONLY, 0644)
if err != nil {
// Ignore does not exist error, leave struct as it is.
if os.IsNotExist(err) {
return nil
}
return err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(to); err != nil {
return errors.Wrap(err, "Failed to unmarshal to given struct")
}
return nil
}