2024-11-26 05:34:26 -06:00
|
|
|
package gitpb
|
|
|
|
|
|
|
|
// this is becoming a standard format
|
|
|
|
// todo: autogenerate this from the .proto file?
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
sync "sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// bad global lock until I figure out some other plan
|
|
|
|
var godeplock sync.RWMutex
|
|
|
|
|
|
|
|
type GoDepIterator struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
packs []*GoDep
|
|
|
|
index int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewGoDepGoDepIterator initializes a new iterator.
|
|
|
|
func NewGoDepIterator(packs []*GoDep) *GoDepIterator {
|
|
|
|
return &GoDepIterator{packs: packs}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan moves to the next element and returns false if there are no more packs.
|
|
|
|
func (it *GoDepIterator) Scan() bool {
|
|
|
|
if it.index >= len(it.packs) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
it.index++
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// GoDep returns the current repo.
|
|
|
|
func (it *GoDepIterator) GoDep() *GoDep {
|
|
|
|
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.GoDep()
|
|
|
|
// fmt.Println("GoDep UUID:", d.Uuid)
|
|
|
|
// }
|
|
|
|
|
2024-11-27 14:27:09 -06:00
|
|
|
func (r *Repo) AllGoDeps() *GoDepIterator {
|
2024-11-26 05:34:26 -06:00
|
|
|
repoPointers := r.selectAllGoDeps()
|
|
|
|
|
|
|
|
iterator := NewGoDepIterator(repoPointers)
|
|
|
|
return iterator
|
|
|
|
}
|
|
|
|
|
2024-11-27 14:27:09 -06:00
|
|
|
func (r *Repo) SortGoDepsByName() *GoDepIterator {
|
2024-11-26 05:34:26 -06:00
|
|
|
packs := r.selectAllGoDeps()
|
|
|
|
|
|
|
|
sort.Sort(GoDepByPath(packs))
|
|
|
|
|
|
|
|
iterator := NewGoDepIterator(packs)
|
|
|
|
return iterator
|
|
|
|
}
|
|
|
|
|
2024-11-27 14:27:09 -06:00
|
|
|
func (repo *Repo) Len() int {
|
2024-11-26 05:34:26 -06:00
|
|
|
refslock.RLock()
|
|
|
|
defer refslock.RUnlock()
|
|
|
|
|
2024-11-27 14:27:09 -06:00
|
|
|
return len(repo.GoDeps)
|
2024-11-26 05:34:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type GoDepByPath []*GoDep
|
|
|
|
|
|
|
|
func (a GoDepByPath) Len() int { return len(a) }
|
|
|
|
func (a GoDepByPath) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath }
|
|
|
|
func (a GoDepByPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
|
|
|
|
|
|
// safely returns a slice of pointers to the GoDep protobufs
|
2024-11-27 14:27:09 -06:00
|
|
|
func (r *Repo) selectAllGoDeps() []*GoDep {
|
2024-11-26 05:34:26 -06:00
|
|
|
refslock.RLock()
|
|
|
|
defer refslock.RUnlock()
|
|
|
|
|
|
|
|
// Create a new slice to hold pointers to each GoDep
|
|
|
|
var allPacks []*GoDep
|
|
|
|
allPacks = make([]*GoDep, len(r.GoDeps))
|
|
|
|
for i, p := range r.GoDeps {
|
|
|
|
allPacks[i] = p // Copy pointers for safe iteration
|
|
|
|
}
|
|
|
|
|
|
|
|
return allPacks
|
|
|
|
}
|