2024-11-20 09:31:24 -06:00
|
|
|
package forgepb
|
|
|
|
|
|
|
|
// TODO: autogen this? (probably not feasible. need go-arglike tricks in proto)
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
sync "sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// bad global lock until I figure out some other plan
|
|
|
|
var reposLock sync.RWMutex
|
|
|
|
|
|
|
|
type RepoIterator struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
packs []*Repo
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRepoIterator initializes a new iterator.
|
|
|
|
func NewRepoIterator(packs []*Repo) *RepoIterator {
|
|
|
|
return &RepoIterator{packs: packs}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan moves to the next element and returns false if there are no more packs.
|
|
|
|
func (it *RepoIterator) Scan() bool {
|
|
|
|
if it.index >= len(it.packs) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
it.index++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repo returns the current repo.
|
|
|
|
func (it *RepoIterator) Repo() *Repo {
|
|
|
|
if it.packs[it.index-1] == nil {
|
|
|
|
for i, d := range it.packs {
|
|
|
|
fmt.Println("i =", i, d)
|
|
|
|
}
|
|
|
|
fmt.Println("len =", len(it.packs))
|
|
|
|
fmt.Println("repo == nil", it.index, it.index-1)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
return it.packs[it.index-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use Scan() in a loop, similar to a while loop
|
|
|
|
//
|
|
|
|
// for iterator.Scan() {
|
|
|
|
// d := iterator.Repo()
|
|
|
|
// fmt.Println("Repo UUID:", d.Uuid)
|
|
|
|
// }
|
|
|
|
|
|
|
|
func (r *Repos) All() *RepoIterator {
|
|
|
|
repoPointers := r.selectAllRepos()
|
|
|
|
|
|
|
|
iterator := NewRepoIterator(repoPointers)
|
|
|
|
return iterator
|
|
|
|
}
|
|
|
|
|
2024-11-20 21:22:05 -06:00
|
|
|
func (r *Repos) SortByPath() *RepoIterator {
|
2024-11-20 09:31:24 -06:00
|
|
|
packs := r.selectAllRepos()
|
|
|
|
|
2024-11-20 21:22:05 -06:00
|
|
|
sort.Sort(ByRepoPath(packs))
|
2024-11-20 09:31:24 -06:00
|
|
|
|
|
|
|
iterator := NewRepoIterator(packs)
|
|
|
|
return iterator
|
|
|
|
}
|
|
|
|
|
2024-11-20 21:22:05 -06:00
|
|
|
// enforces no duplicate repo paths
|
2024-11-20 09:31:24 -06:00
|
|
|
func (r *Repos) Append(newP *Repo) bool {
|
|
|
|
reposLock.Lock()
|
|
|
|
defer reposLock.Unlock()
|
|
|
|
|
|
|
|
for _, p := range r.Repos {
|
2024-11-20 21:22:05 -06:00
|
|
|
if p.GoPath == newP.GoPath {
|
2024-11-20 09:31:24 -06:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Repos = append(r.Repos, newP)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns time.Duration since last Update()
|
|
|
|
func (r *Repo) Age(newP *Repo) time.Duration {
|
|
|
|
t := time.Since(r.Verstamp.AsTime())
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2024-11-20 21:22:05 -06:00
|
|
|
// find a repo by path
|
|
|
|
func (r *Repos) FindByPath(gopath string) *Repo {
|
2024-11-20 09:31:24 -06:00
|
|
|
reposLock.RLock()
|
|
|
|
defer reposLock.RUnlock()
|
|
|
|
|
|
|
|
for _, p := range r.Repos {
|
2024-11-20 21:22:05 -06:00
|
|
|
if p.GoPath == gopath {
|
2024-11-20 09:31:24 -06:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Repos) Len() int {
|
|
|
|
reposLock.RLock()
|
|
|
|
defer reposLock.RUnlock()
|
|
|
|
|
|
|
|
return len(r.Repos)
|
|
|
|
}
|
|
|
|
|
2024-11-20 21:22:05 -06:00
|
|
|
type ByRepoPath []*Repo
|
2024-11-20 09:31:24 -06:00
|
|
|
|
2024-11-20 21:22:05 -06:00
|
|
|
func (a ByRepoPath) Len() int { return len(a) }
|
|
|
|
func (a ByRepoPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath }
|
|
|
|
func (a ByRepoPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
2024-11-20 09:31:24 -06:00
|
|
|
|
2024-11-20 21:22:05 -06:00
|
|
|
func (all *Repos) DeleteByPath(gopath string) *Repo {
|
2024-11-20 15:57:34 -06:00
|
|
|
reposLock.Lock()
|
|
|
|
defer reposLock.Unlock()
|
|
|
|
|
|
|
|
var newr Repo
|
|
|
|
|
|
|
|
for i, _ := range all.Repos {
|
2024-11-20 21:22:05 -06:00
|
|
|
if all.Repos[i].GoPath == gopath {
|
2024-11-20 15:57:34 -06:00
|
|
|
newr = *all.Repos[i]
|
|
|
|
all.Repos[i] = all.Repos[len(all.Repos)-1]
|
|
|
|
all.Repos = all.Repos[:len(all.Repos)-1]
|
|
|
|
return &newr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-20 09:31:24 -06:00
|
|
|
// safely returns a slice of pointers to the Repo protobufs
|
|
|
|
func (r *Repos) selectAllRepos() []*Repo {
|
|
|
|
reposLock.RLock()
|
|
|
|
defer reposLock.RUnlock()
|
|
|
|
|
|
|
|
// Create a new slice to hold pointers to each Repo
|
|
|
|
var allPacks []*Repo
|
|
|
|
allPacks = make([]*Repo, len(r.Repos))
|
|
|
|
for i, p := range r.Repos {
|
|
|
|
allPacks[i] = p // Copy pointers for safe iteration
|
|
|
|
}
|
|
|
|
|
|
|
|
return allPacks
|
|
|
|
}
|