forgepb/settings.go

191 lines
4.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 (
2024-11-22 11:14:55 -06:00
"path/filepath"
"strings"
)
2024-11-20 23:51:28 -06:00
func (all *ForgeConfigs) 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-22 19:11:22 -06:00
// will attempt to match IsWritable("foo") against anything ending in "foo"
func (all *ForgeConfigs) IsReadOnly(gopath string) bool {
var match *ForgeConfig
2024-11-20 23:51:28 -06:00
loop := all.SortByPath() // get the list of repos
for loop.Scan() {
r := loop.Next()
2024-11-20 23:51:28 -06:00
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
}
}
2024-11-22 19:11:22 -06:00
// if gopath == "foo" will return false if "go.wit.com/apps/foo" is Writable
base := filepath.Base(r.GoPath)
if base == gopath {
if r.Writable {
return false
}
}
2024-11-20 23:51:28 -06:00
// 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
}
2024-11-25 00:46:16 -06:00
// returns the deb package name
// this let's you check a git tag version against a package .deb version
// allows gopath's to not need to match the .deb name
// this is important in lots of cases! It is normal and happens often enough.
func (all *ForgeConfigs) DebName(gopath string) string {
2024-11-25 00:46:16 -06:00
// get "zookeeper" from "go.wit.com/apps/zookeeper"
normalBase := filepath.Base(gopath)
loop := all.SortByPath()
for loop.Scan() {
r := loop.Next()
2024-11-25 00:46:16 -06:00
if r.GoPath == gopath {
// 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
}
2024-11-20 23:51:28 -06:00
// is this a non-publishable repo?
2024-11-22 19:11:22 -06:00
// 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 (all *ForgeConfigs) IsPrivate(thing string) bool {
var match *ForgeConfig
2024-11-20 23:51:28 -06:00
// 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.Next()
2024-11-22 19:11:22 -06:00
if r.GoPath == thing {
2024-11-20 23:51:28 -06:00
// if private is set here, then ok, otherwise
// still check if a Directory match exists
if r.Private {
return true
}
}
2024-11-22 19:11:22 -06:00
base := filepath.Base(r.GoPath)
if base == thing {
if r.Private {
return true
}
}
// check to see if IsPrivate("foo")
2024-11-20 23:51:28 -06:00
// search for potential dir matches
if r.Directory {
// test the dir
2024-11-22 19:11:22 -06:00
if strings.HasPrefix(thing, r.GoPath) {
2024-11-20 23:51:28 -06:00
match = r
}
}
}
if match == nil {
2024-11-22 19:11:22 -06:00
// log.Info("did not match in IsPrivate()", thing)
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
}
2024-11-22 11:14:55 -06:00
2024-11-25 00:46:16 -06:00
// 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 (all *ForgeConfigs) IsFavorite(thing string) bool {
var match *ForgeConfig
2024-11-22 11:14:55 -06:00
2024-11-25 00:46:16 -06:00
loop := all.SortByPath() // get the list of repos
2024-11-22 11:14:55 -06:00
for loop.Scan() {
r := loop.Next()
2024-11-25 00:46:16 -06:00
if r.GoPath == thing {
if r.Favorite {
return true
}
}
base := filepath.Base(r.GoPath)
if base == thing {
if r.Favorite {
return true
}
}
if r.Directory {
if strings.HasPrefix(thing, r.GoPath) {
match = r
2024-11-22 11:14:55 -06:00
}
}
}
2024-11-25 00:46:16 -06:00
if match == nil {
return false
}
return match.Favorite
2024-11-22 11:14:55 -06:00
}