summaryrefslogtreecommitdiff
path: root/repo.sort.go
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2024-11-27 13:57:17 -0600
committerJeff Carr <[email protected]>2024-11-27 13:57:17 -0600
commit2e3e4f98d33d8794a1c8a6aa13e6c9fa92fbcadb (patch)
treecd6222e4dc98e9de94981ade9f15f34030cc3d61 /repo.sort.go
parent44caec2e7fc59697ef01835aacf8a03171a42bea (diff)
start full refactor to have repo be here
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'repo.sort.go')
-rw-r--r--repo.sort.go150
1 files changed, 150 insertions, 0 deletions
diff --git a/repo.sort.go b/repo.sort.go
new file mode 100644
index 0000000..6495676
--- /dev/null
+++ b/repo.sort.go
@@ -0,0 +1,150 @@
+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 repolock 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.selectAllRepo()
+
+ iterator := NewRepoIterator(repoPointers)
+ return iterator
+}
+
+func (r *Repos) SortByName() *RepoIterator {
+ packs := r.selectAllRepo()
+
+ sort.Sort(RepoByName(packs))
+
+ iterator := NewRepoIterator(packs)
+ return iterator
+}
+
+// enforces no duplicate package names
+func (all *Repos) Append(newP *Repo) bool {
+ repolock.Lock()
+ defer repolock.Unlock()
+
+ for _, p := range all.Repos {
+ if p.GoPath == newP.GoPath {
+ return false
+ }
+ }
+
+ all.Repos = append(all.Repos, newP)
+ return true
+}
+
+/*
+// returns time.Duration since last Update()
+func (r *Repo) LastPull() time.Duration {
+ t := time.Since(r.LastPull.AsTime())
+ return t
+}
+*/
+
+// find a package by gopath
+func (all *Repos) FindByPath(gopath string) *Repo {
+ repolock.RLock()
+ defer repolock.RUnlock()
+
+ for _, p := range all.Repos {
+ if p.GoPath == gopath {
+ return p
+ }
+ }
+
+ return nil
+}
+
+func (all *Repos) Len() int {
+ repolock.RLock()
+ defer repolock.RUnlock()
+
+ return len(all.Repos)
+}
+
+type RepoByName []*Repo
+
+func (a RepoByName) Len() int { return len(a) }
+func (a RepoByName) Less(i, j int) bool { return a[i].GoPath < a[j].GoPath }
+func (a RepoByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+
+// safely returns a slice of pointers to the Repo protobufs
+func (all *Repos) selectAllRepo() []*Repo {
+ repolock.RLock()
+ defer repolock.RUnlock()
+
+ // Create a new slice to hold pointers to each Repo
+ var aRepos []*Repo
+ aRepos = make([]*Repo, len(all.Repos))
+ for i, p := range all.Repos {
+ aRepos[i] = p // Copy pointers for safe iteration
+ }
+
+ return aRepos
+}
+
+func (all *Repos) DeleteByPath(gopath string) *Repo {
+ repolock.Lock()
+ defer repolock.Unlock()
+
+ for i, _ := range all.Repos {
+ if all.Repos[i].GoPath == gopath {
+ all.Repos[i] = all.Repos[len(all.Repos)-1]
+ all.Repos = all.Repos[:len(all.Repos)-1]
+ return nil
+ }
+ }
+ return nil
+}