forgepb/settings.go

111 lines
2.2 KiB
Go
Raw Normal View History

2024-11-20 13:56:27 -06:00
package forgepb
/*
check settings for a particular gopath
this provides checks for:
IsReadOnly() // user can't push commits
IsPrivate() // repo can't be published to the pkg.go.dev system
DebName() // for 'zookeeper' returns 'zookeeper-go'
*/
import (
"strings"
)
2024-11-20 23:51:28 -06:00
func (all *Repos) UpdateGoPath(name string, gopath string) bool {
2024-11-20 21:22:05 -06:00
oldr := all.DeleteByPath(name)
if oldr == nil {
// nothing to update
return false
}
2024-11-20 13:56:27 -06:00
// update gopath and append it back to the list
oldr.GoPath = gopath
return all.Append(oldr)
2024-11-20 13:56:27 -06:00
}
2024-11-20 23:51:28 -06:00
// returns true if gopath is readonly()
2024-11-20 23:53:29 -06:00
func (all *Repos) IsReadOnly(gopath string) bool {
2024-11-20 23:51:28 -06:00
var match *Repo
loop := all.SortByPath() // get the list of repos
for loop.Scan() {
r := loop.Repo()
if r.GoPath == gopath {
// exact gopath 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
}
}
// search for potential dir matches
if r.Directory {
// test the dir
if strings.HasPrefix(gopath, r.GoPath) {
match = r
}
}
}
if match == nil {
// log.Info("did not match in IsReadOnly()", gopath)
return true
}
2024-11-20 23:51:28 -06:00
// 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
}
// is this a non-publishable repo?
2024-11-20 23:53:29 -06:00
func (all *Repos) IsPrivate(gopath string) bool {
2024-11-20 23:51:28 -06:00
var match *Repo
// sort by path means the simple 'match' logic
// here works in the sense the last directory match
// is the one that is used
2024-11-20 23:51:28 -06:00
loop := all.SortByPath() // get the list of repos
for loop.Scan() {
r := loop.Repo()
if r.GoPath == gopath {
// if private is set here, then ok, otherwise
// still check if a Directory match exists
if r.Private {
return true
}
}
// search for potential dir matches
if r.Directory {
// test the dir
if strings.HasPrefix(gopath, r.GoPath) {
match = r
}
}
}
if match == nil {
// log.Info("did not match in IsPrivate()", gopath)
return false
2024-11-20 23:51:28 -06:00
}
// otherwise, assume not private
return match.Private
2024-11-20 23:51:28 -06:00
}