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
|
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("file was empty")
// 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
homeDir, err := os.UserHomeDir()
if err != nil {
log.Warn("ConfigLoad() UserHomeDir() err", err)
return err
}
fullname = filepath.Join(homeDir, ".config", argname, protoname+".text")
if err := loadTEXT(pb, fullname); err == nil {
return nil
}
if strings.HasSuffix(fullname, ".text") {
fullname = strings.TrimSuffix(fullname, ".text")
fullname += ".json"
if err := loadJSON(pb, fullname); err != nil {
return err
}
return nil
}
log.Info("Config file load failed:", fullname)
return log.Errorf("could not load config file")
}
func loadTEXT(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)
// set pb.Filename that was attempted
SetFilename(pb, fullname)
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 err
}
if fn, ok := GetFilename(pb); ok {
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
}
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)
// set pb.Filename that was attempted
SetFilename(pb, fullname)
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 err
}
if fn, ok := GetFilename(pb); ok {
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
}
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
}
return data, nil
}
|