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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
package forgepb
/*
lookup settings for a particular *gitpb.Repo or namespace string
user settings are configured in ~/.config/forge/forge.text
// searchs by string
Configs.IsReadOnly(path) // user can't push commits
Configs.IsWritable(path) // the opposite, but maybe different so I put both here
IsPrivate(repo) // repo can't be published to the pkg.go.dev system
DebName() // for 'zookeeper' returns 'zookeeper-go'
This code is practical, not perfect
*/
import (
"path/filepath"
"strings"
"go.wit.com/lib/protobuf/gitpb"
)
// move away from IsReadOnly. Only use repo.Namespace
func (f *Forge) IsWritable(repo *gitpb.Repo) bool {
if f.Config.IsWritable(repo.Namespace) {
return true
}
// if f.Config.IsWritable(repo.FullPath) {
// return true
// }
// if !f.Config.IsWritable(repo.GetNamespace()) {
// return true
// }
return false
}
// returns true if namespace is readonly()
// will attempt to match IsWritable("foo") against anything ending in "foo"
func (fc *ForgeConfigs) IsReadOnly(namespace string) bool {
var match *ForgeConfig
all := fc.All() // get the list of repos
for all.Scan() {
r := all.Next()
if r.Namespace == namespace {
// exact namespace match
if r.Writable {
return false
}
if r.ReadOnly {
return true
}
// private is assumed to be r/w unless above is specifically set
if r.Private {
return false
}
}
// if namespace == "foo" will return false if "go.wit.com/apps/foo" is Writable
base := filepath.Base(r.Namespace)
if base == namespace {
if r.Writable {
return false
}
}
// search for potential dir matches
if r.Directory {
// test the dir
if strings.HasPrefix(namespace, r.Namespace) {
match = r
}
}
}
if match == nil {
// log.Info("did not match in IsReadOnly()", namespace)
return true
}
// take the settings from the directory match
if match.Writable {
return false
}
if match.ReadOnly {
return true
}
// private is assumed to be r/w unless above is specifically set
if match.Private {
return false
}
// always assume readonly
return true
}
// returns the deb package name
// this let's you check a git tag version against a package .deb version
// allows namespace's to not need to match the .deb name
// this is important in lots of cases! It is normal and happens often enough.
func (fc *ForgeConfigs) DebName(namespace string) string {
// get "zookeeper" from "go.wit.com/apps/zookeeper"
normalBase := filepath.Base(namespace)
all := fc.All()
for all.Scan() {
r := all.Next()
if r.Namespace == namespace {
// returns "zookeeper-go" for "go.wit.com/apps/zookeeper"
if r.DebName != "" {
// log.Info("FOUND DebName", r.DebName)
return r.DebName
} else {
return normalBase
}
}
}
return normalBase
}
// a work in progress
func (f *Forge) IsPrivate(repo *gitpb.Repo) bool {
namespace := repo.GetNamespace()
if namespace == "" {
// assume true
return true
}
return f.Config.IsPrivate(namespace)
}
// is this a non-publishable repo?
// matches package names from apt
//
// IsPrivate("foo") will match anything in the config file ending in "foo"
//
// IsPrivate("go.foo.com/jcarr/foo") returns true if private
// IsPrivate("foo") also returns true if "go.bar.com/jcarr/foo" is private
func (fc *ForgeConfigs) IsPrivate(thing string) bool {
var match *ForgeConfig
// sort by path means the simple 'match' logic
// here works in the sense the last directory match
// is the one that is used
all := fc.All() // get the list of repos
for all.Scan() {
r := all.Next()
if r.Namespace == thing {
// if private is set here, then ok, otherwise
// still check if a Directory match exists
if r.Private {
return true
}
}
base := filepath.Base(r.Namespace)
if base == thing {
if r.Private {
return true
}
}
// check to see if IsPrivate("foo")
// search for potential dir matches
if r.Directory {
// test the dir
if strings.HasPrefix(thing, r.Namespace) {
match = r
}
}
}
if match == nil {
// log.Info("did not match in IsPrivate()", thing)
return false
}
// otherwise, assume not private
return match.Private
}
// IsFavorite() -- fun option for the config
// file that lets you set things as favorites
// so you can just go-clone a bunch of common things
// on a new box or after you reset/delete your ~/go/src dir
func (fc *ForgeConfigs) IsFavorite(thing string) bool {
var match *ForgeConfig
all := fc.All() // get the list of repos
for all.Scan() {
r := all.Next()
if r.Namespace == thing {
if r.Favorite {
return true
}
}
base := filepath.Base(r.Namespace)
if base == thing {
if r.Favorite {
return true
}
}
if r.Directory {
if strings.HasPrefix(thing, r.Namespace) {
match = r
}
}
}
if match == nil {
return false
}
return match.Favorite
}
// IsWritable() checks your .config/forge/ settings
// looks for an exact match, then
// looks for a directory match
func (fc *ForgeConfigs) IsWritable(thing string) bool {
var match *ForgeConfig
all := fc.All() // get the list of repos
for all.Scan() {
r := all.Next()
if r.Namespace == thing {
if r.Writable {
return true
}
}
base := filepath.Base(r.Namespace)
if base == thing {
if r.Writable {
return true
}
}
if r.Directory {
if strings.HasPrefix(thing, r.Namespace) {
match = r
}
}
}
if match == nil {
return false
}
return match.Writable
}
// allows custom user branch names in the forge config
func (fc *ForgeConfigs) FindUserBranch(thing string) string {
var match *ForgeConfig
all := fc.All() // get the list of repos
for all.Scan() {
r := all.Next()
if r.Namespace == thing {
if r.UserBranchName != "" {
return r.UserBranchName
}
}
base := filepath.Base(r.Namespace)
if base == thing {
if r.UserBranchName != "" {
return r.UserBranchName
}
}
if r.Directory {
if strings.HasPrefix(thing, r.Namespace) {
match = r
}
}
}
if match == nil {
return ""
}
return match.UserBranchName
}
// allows custom devel branch names in the forge config
func (fc *ForgeConfigs) FindDevelBranch(thing string) string {
var match *ForgeConfig
all := fc.All() // get the list of repos
for all.Scan() {
r := all.Next()
if r.Namespace == thing {
if r.DevelBranchName != "" {
return r.DevelBranchName
}
}
base := filepath.Base(r.Namespace)
if base == thing {
if r.DevelBranchName != "" {
return r.DevelBranchName
}
}
if r.Directory {
if strings.HasPrefix(thing, r.Namespace) {
match = r
}
}
}
if match == nil {
return ""
}
return match.DevelBranchName
}
// allows custom devel branch names in the forge config
func (fc *ForgeConfigs) FindMasterBranch(thing string) string {
var match *ForgeConfig
all := fc.All() // get the list of repos
for all.Scan() {
r := all.Next()
if r.Namespace == thing {
if r.MasterBranchName != "" {
return r.MasterBranchName
}
}
base := filepath.Base(r.Namespace)
if base == thing {
if r.MasterBranchName != "" {
return r.MasterBranchName
}
}
if r.Directory {
if strings.HasPrefix(thing, r.Namespace) {
match = r
}
}
}
if match == nil {
return ""
}
return match.MasterBranchName
}
|