-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinifilesettings.go
147 lines (115 loc) · 2.86 KB
/
inifilesettings.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
145
146
147
// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package walk
import (
"bufio"
"fmt"
"io"
"os"
"path"
"strings"
)
type IniFileSettings struct {
data map[string]string
}
func NewIniFileSettings() *IniFileSettings {
return &IniFileSettings{data: make(map[string]string)}
}
func (ifs *IniFileSettings) Get(key string) (string, bool) {
val, ok := ifs.data[key]
return val, ok
}
func (ifs *IniFileSettings) Put(key, value string) error {
if strings.IndexAny(key, "=\r\n") > -1 || strings.IndexAny(value, "\r\n") > -1 {
return newError("either key or value contains at least one of the invalid characters '=\\r\\n'")
}
ifs.data[key] = value
return nil
}
func (ifs *IniFileSettings) filePath() (string, error) {
appDataPath, err := AppDataPath()
if err != nil {
return "", err
}
return path.Join(
appDataPath,
appSingleton.OrganizationName(),
appSingleton.ProductName(),
"settings.ini"), nil
}
func (ifs *IniFileSettings) fileExists() (bool, error) {
filePath, err := ifs.filePath()
if err != nil {
return false, err
}
_, err = os.Stat(filePath)
if err != nil {
// FIXME: Not necessarily a file does not exist error.
return false, nil
}
return true, nil
}
func (ifs *IniFileSettings) withFile(flags int, f func(file *os.File) error) error {
filePath, err := ifs.filePath()
dirPath, _ := path.Split(filePath)
if err := os.MkdirAll(dirPath, 0644); err != nil {
return wrapError(err)
}
file, err := os.OpenFile(filePath, flags, 0644)
if err != nil {
return wrapError(err)
}
defer file.Close()
return f(file)
}
func (ifs *IniFileSettings) Load() error {
exists, err := ifs.fileExists()
if err != nil {
return err
}
if !exists {
return nil
}
return ifs.withFile(os.O_RDONLY, func(file *os.File) error {
lineBytes := make([]byte, 0, 4096)
reader := bufio.NewReader(file)
for {
lineBytes = lineBytes[:0]
for {
ln, isPrefix, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
return nil
}
return wrapError(err)
}
lineBytes = append(lineBytes, ln...)
if !isPrefix {
break
}
}
lineStr := string(lineBytes)
assignIndex := strings.Index(lineStr, "=")
if assignIndex == -1 {
return newError("bad line format: missing '='")
}
key := strings.TrimSpace(lineStr[:assignIndex])
val := strings.TrimSpace(lineStr[assignIndex+1:])
ifs.data[key] = val
}
return nil
})
}
func (ifs *IniFileSettings) Save() error {
return ifs.withFile(os.O_CREATE|os.O_TRUNC|os.O_WRONLY, func(file *os.File) error {
bufWriter := bufio.NewWriter(file)
for key, val := range ifs.data {
line := fmt.Sprintf("%s=%s\n", key, val)
if _, err := bufWriter.WriteString(line); err != nil {
return wrapError(err)
}
}
return bufWriter.Flush()
})
}