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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
package config
// functions to import and export the protobuf
// data to and from config files
import (
"errors"
"os"
"path/filepath"
"strings"
"go.wit.com/log"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
)
/*
// loads a file from ~/.config/<argname>/
func Load(argname string) ([]byte, string) {
}
*/
var ErrEmpty error = log.Errorf("config file was empty")
var ErrMarshal error = log.Errorf("protobuf parse error")
// returns:
// - Full path to the config file. usually: ~/.config/<argname>
// - []byte : the contents of the file
// - error on read
func ConfigLoad(pb proto.Message, argname string, protoname string) error {
var fullname string
var err error
homeDir, err := os.UserHomeDir()
if err != nil {
log.Printf("ConfigLoad() UserHomeDir() err=%v\n", err)
return err
}
fullname = filepath.Join(homeDir, ".config", argname, protoname+".text")
SetFilename(pb, fullname)
// if both don't exist or both are empty, return known errors
// these can be used to detect if the user is new to the application
if err := missingConfig(fullname); err != nil {
if errors.Is(err, os.ErrNotExist) {
return os.ErrNotExist
}
if errors.Is(err, ErrEmpty) {
return ErrEmpty
}
log.Info("ConfigLoad() error", fullname, err)
return err
}
if err = loadTEXT(pb, fullname); err == nil {
return nil
} else {
if strings.HasSuffix(fullname, ".text") {
fulljson := fullname + ".json"
if err := loadJSON(pb, fulljson); err == nil {
return nil
} else {
log.Info("Config file load failed:", fulljson, err)
}
}
}
log.Info("Config file load failed:", fullname, err)
return ErrMarshal
}
func Load(pb proto.Message) error {
fullname, err := GetFilename(pb)
if err != nil {
log.Info("'Filename' is not in: =", fullname, err)
return err
}
// text is supposed to be "easy". Don't verify 'version'
if strings.HasSuffix(fullname, ".text") {
return loadTEXT(pb, fullname)
}
// verify 'version' for .pb files
// application will panic if they don't match
var worked bool
ver, err := GetString(pb, "version")
if err != nil {
log.Info("'Version' is not in: =", fullname, err)
return err
}
// maybe don't really verify .json files (?)
// doing it for now anyway. maybe just return an error
if strings.HasSuffix(fullname, ".json") {
if err := loadJSON(pb, fullname); err != nil {
return err
}
worked = true
}
if strings.HasSuffix(fullname, ".pb") {
if err := loadPB(pb, fullname); err != nil {
return err
}
worked = true
}
if !worked {
return log.Errorf("unknown filetype %s", fullname)
}
newver, _ := GetString(pb, "version")
if ver != newver {
log.Printf("VERSION '%s' != '%s'\n", ver, newver)
log.Info("Your protobuf file is old and can not be loaded")
log.Info("You must delete or convert the file", fullname)
// probably should ALWAYS PANIC HERE
panic("protobuf version mismatch")
}
return nil
}
func LoadFile(pb proto.Message, fullname string) error {
if strings.HasSuffix(fullname, ".text") {
return loadTEXT(pb, fullname)
}
if strings.HasSuffix(fullname, ".json") {
return loadJSON(pb, fullname)
}
if strings.HasSuffix(fullname, ".pb") {
return loadPB(pb, fullname)
}
return log.Errorf("unknown filetype %s", fullname)
}
func loadPB(pb proto.Message, fullname string) error {
data, err := loadFile(fullname)
if err != nil {
log.Warn("LoadPB()", fullname, err)
// set pb.Filename that was attempted
return err
}
if err = proto.Unmarshal(data, pb); err != nil {
log.Warn("LoadPB() error Unmarshal() ", fullname, err)
return err
}
return nil
}
func LoadPB(pb proto.Message, argname string, protoname string) (string, error) {
var fullname string
if strings.HasPrefix(argname, "/") {
fullname = filepath.Join(argname, protoname+".pb")
} else {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Warn("ConfigLoad() UserHomeDir() err", err)
return "", err
}
fullname = filepath.Join(homeDir, ".config", argname, protoname+".pb")
}
data, err := loadFile(fullname)
if err != nil {
log.Warn("LoadPB()", fullname, err)
// set pb.Filename that was attempted
return fullname, err
}
// Unmarshal()
if err = proto.Unmarshal(data, pb); err != nil {
log.Warn("LoadPB() file", fullname)
log.Warn("LoadPB() Unmarshal() err", err)
return fullname, err
}
return fullname, nil
}
func loadTEXT(pb proto.Message, fullname string) error {
var data []byte
var err error
SetFilename(pb, fullname)
if data, err = loadFile(fullname); err != nil {
log.Warn("config file failed to load", err)
// set pb.Filename that was attempted
return err
}
// don't even bother with Marshal()
if data == nil {
log.Warn("ConfigLoad() config file was empty", fullname)
return ErrEmpty // file is empty
}
// Unmarshal()
if err = prototext.Unmarshal(data, pb); err != nil {
log.Warn("ConfigLoad() file", fullname)
log.Warn("ConfigLoad() Unmarshal() err", err)
return ErrMarshal
}
if fn, err := GetFilename(pb); err != nil {
if fn != fullname {
log.Info("config.ConfigLoad() new filename:", fullname)
SetFilename(pb, fullname)
}
}
if os.Getenv("CONFIG_VERBOSE") == "true" {
log.Infof("ConfigLoad() %s len()=%d\n", fullname, len(data))
}
return nil
}
// json files are backup Marshal() data in case .text Unmarshal() fails
// they always should have the ".text" filename in them
func loadJSON(pb proto.Message, fullname string) error {
var data []byte
var err error
if data, err = loadFile(fullname); err != nil {
log.Warn("config file failed to load", err)
return err
}
// don't even bother with Marshal()
if data == nil {
log.Warn("ConfigLoad() config file was empty", fullname)
return ErrEmpty // file is empty
}
// Unmarshal()
if err = protojson.Unmarshal(data, pb); err != nil {
log.Warn("ConfigLoad() file", fullname)
log.Warn("ConfigLoad() Unmarshal() err", err)
return ErrMarshal
}
if fn, err := GetFilename(pb); err != nil {
if fn != fullname {
log.Info("config.ConfigLoad() new filename:", fullname)
SetFilename(pb, fullname)
}
}
if os.Getenv("CONFIG_VERBOSE") == "true" {
log.Infof("ConfigLoad() %s len()=%d\n", fullname, len(data))
}
return nil
}
// dumb but simple to read logic
func missingConfig(fullname string) error {
data1, err1 := os.ReadFile(fullname)
if !errors.Is(err1, os.ErrNotExist) {
return err1
}
data2, err2 := os.ReadFile(fullname + ".json")
if !errors.Is(err2, os.ErrNotExist) {
return err2
}
if errors.Is(err1, os.ErrNotExist) && errors.Is(err2, os.ErrNotExist) {
return os.ErrNotExist
}
if (len(data1) == 0) && (len(data2) == 0) {
return ErrEmpty
}
return nil
}
func loadFile(fullname string) ([]byte, error) {
data, err := os.ReadFile(fullname)
if errors.Is(err, os.ErrNotExist) {
// if file does not exist, just return nil. this
return nil, err
}
if err != nil {
// log.Info("open config file :", err)
return nil, err
}
if len(data) == 0 {
return data, ErrEmpty
}
return data, nil
}
|