-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibprotoconf.go
144 lines (127 loc) · 3.37 KB
/
libprotoconf.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
package libprotoconf
import (
"fmt"
stdlog "log"
"os"
"path/filepath"
"sort"
"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
"github.com/go-logr/stdr"
"github.com/protoconf/libprotoconf/config/v1"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
type Config struct {
msg proto.Message
Logger logr.Logger
envKeyPrefix string
config *config.LibprotoconfConfig
}
func NewConfig(p proto.Message) *Config {
c := &Config{
msg: p,
Logger: funcr.New(func(prefix, args string) {}, funcr.Options{}),
config: &config.LibprotoconfConfig{},
}
c.SetEnvKeyPrefix(string(p.ProtoReflect().Descriptor().FullName()))
return c
}
func (c *Config) LoadFromDefaultDirs() {
c.LoadFromSystemDir()
c.LoadFromUserDir()
c.LoadFromWorkspace()
c.Logger.V(4).Info("looking for configs in", "dirs", c.config.ConfigDirs)
}
func (c *Config) LoadFromSystemDir() error {
for _, p := range c.getPackageTree() {
c.AppendLoadDir(100, filepath.Join("/etc", pkgToPath(p)))
}
return nil
}
func (c *Config) LoadFromUserDir() error {
for _, p := range c.getPackageTree() {
if home, err := os.UserHomeDir(); err == nil {
c.AppendLoadDir(90, filepath.Join(home, "."+pkgToPath(p)))
} else {
return err
}
}
return nil
}
func (c *Config) LoadFromWorkspace() error {
ws, err := c.DetectWorkspace()
if err == nil {
c.AppendLoadDir(80, ws)
}
return err
}
func (c *Config) DetectWorkspace() (string, error) {
tree := c.getPackageTree()
root := "." + tree[len(tree)-1]
cwd, err := os.Getwd()
if err != nil {
return "", err
}
for cwd != "/" {
c.Logger.V(4).Info("trying to detect workspace", "root", root, "cwd", cwd, "tree", tree)
if ret, err := os.Stat(filepath.Join(cwd, root)); err == nil {
return filepath.Abs(ret.Name())
}
cwd = filepath.Dir(cwd)
}
return "", fmt.Errorf("not in workspace")
}
func (c *Config) getPackageTree() []string {
var result []string
pkg := c.msg.ProtoReflect().Descriptor().ParentFile().Package()
result = append(result, string(pkg))
parent := pkg.Parent()
for parent != "" {
result = append(result, string(parent))
parent = parent.Parent()
}
return result
}
func (c *Config) AppendLoadDir(priority uint32, paths ...string) error {
for _, path := range paths {
if abs, err := filepath.Abs(path); err == nil {
c.config.ConfigDirs = append(c.config.ConfigDirs, &config.LibprotoconfConfig_Loadable{Priority: priority, Path: abs})
} else {
return err
}
}
sort.Stable(config.ByPrio(c.config.ConfigDirs))
return nil
}
func (c *Config) SetLogger(logger logr.Logger) logr.Logger {
c.Logger = logger
return c.Logger
}
func (c *Config) LoggerWithLevel(v int) logr.Logger {
stdr.SetVerbosity(v)
return c.SetLogger(
stdr.NewWithOptions(
stdlog.New(os.Stderr, "", stdlog.LstdFlags),
stdr.Options{LogCaller: stdr.None},
).WithName("libprotoconf"))
}
func (c *Config) DefaultLogger() logr.Logger {
return c.LoggerWithLevel(1)
}
func (c *Config) DebugLogger() logr.Logger {
return c.LoggerWithLevel(10)
}
func (c *Config) iterateFields(f func(protoreflect.FieldDescriptor) error) error {
return iterateFields(c.msg.ProtoReflect(), f)
}
func iterateFields(msg protoreflect.Message, f func(protoreflect.FieldDescriptor) error) error {
fields := msg.Descriptor().Fields()
for i := 0; i < fields.Len(); i++ {
err := f(fields.Get(i))
if err != nil {
return err
}
}
return nil
}