-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
option.go
164 lines (148 loc) · 3.21 KB
/
option.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package grpcstub
import (
"io/fs"
"os"
"path/filepath"
"github.com/bmatcuk/doublestar/v4"
)
type config struct {
protos []string
importPaths []string
useTLS bool
cacert, cert, key []byte
healthCheck bool
disableReflection bool
bufDirs []string
bufLocks []string
bufConfigs []string
bufModules []string
}
type Option func(*config) error
// Proto append protos
func Proto(protos ...string) Option {
return func(c *config) error {
for _, p := range protos {
opt := proto(p)
if err := opt(c); err != nil {
return err
}
}
return nil
}
}
// ImportPath set import paths
func ImportPath(paths ...string) Option {
return func(c *config) error {
c.importPaths = unique(append(c.importPaths, paths...))
return nil
}
}
// UseTLS enable TLS
func UseTLS(cacert, cert, key []byte) Option {
return func(c *config) error {
c.useTLS = true
c.cacert = cacert
c.cert = cert
c.key = key
return nil
}
}
// EnableHealthCheck enable grpc.health.v1
func EnableHealthCheck() Option {
return func(c *config) error {
c.healthCheck = true
return nil
}
}
// DisableReflection disable Server Reflection Protocol
func DisableReflection() Option {
return func(c *config) error {
c.disableReflection = true
return nil
}
}
// BufDir use buf directory.
func BufDir(dirs ...string) Option {
return func(c *config) error {
c.bufDirs = unique(append(c.bufDirs, dirs...))
return nil
}
}
// BufLock use buf.lock for BSR.
func BufLock(locks ...string) Option {
return func(c *config) error {
c.bufLocks = unique(append(c.bufLocks, locks...))
return nil
}
}
// BufConfig use buf.yaml for BSR.
func BufConfig(configs ...string) Option {
return func(c *config) error {
c.bufConfigs = unique(append(c.bufConfigs, configs...))
return nil
}
}
// BufModule use buf modules for BSR.
func BufModule(module string) Option {
return func(c *config) error {
c.bufModules = unique(append(c.bufModules, module))
return nil
}
}
// BufModules use buf modules for BSR.
func BufModules(modules []string) Option {
return func(c *config) error {
for _, m := range modules {
opt := BufModule(m)
if err := opt(c); err != nil {
return err
}
}
return nil
}
}
func proto(proto string) Option {
return func(c *config) error {
protos := []string{}
if f, err := os.Stat(proto); err == nil {
if !f.IsDir() {
c.protos = unique(append(c.protos, proto))
return nil
}
proto = filepath.Join(proto, "*")
}
base, pattern := doublestar.SplitPattern(filepath.ToSlash(proto))
abs, err := filepath.Abs(base)
if err != nil {
return err
}
fsys := os.DirFS(abs)
if err := doublestar.GlobWalk(fsys, pattern, func(p string, d fs.DirEntry) error {
if d.IsDir() {
return nil
}
protos = unique(append(protos, filepath.Join(base, p)))
return nil
}); err != nil {
return err
}
if len(protos) == 0 {
c.protos = unique(append(c.protos, proto))
} else {
c.protos = unique(append(c.protos, protos...))
}
return nil
}
}
func unique(in []string) []string {
u := []string{}
m := map[string]struct{}{}
for _, s := range in {
if _, ok := m[s]; ok {
continue
}
u = append(u, s)
m[s] = struct{}{}
}
return u
}