Day 1
This commit is contained in:
commit
79e73f9abf
|
@ -0,0 +1,7 @@
|
|||
*.swp
|
||||
go.mod
|
||||
go.sum
|
||||
/files/*
|
||||
/work/*
|
||||
autogenpb
|
||||
test.sort.pb.go
|
|
@ -0,0 +1,34 @@
|
|||
VERSION = $(shell git describe --tags)
|
||||
BUILDTIME = $(shell date +%Y.%m.%d)
|
||||
|
||||
run: build
|
||||
./autogenpb
|
||||
|
||||
vet:
|
||||
@GO111MODULE=off go vet
|
||||
@echo this go library package builds okay
|
||||
|
||||
build:
|
||||
GO111MODULE=off go build \
|
||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||
|
||||
install:
|
||||
GO111MODULE=off go install \
|
||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||
|
||||
goimports:
|
||||
goimports -w *.go
|
||||
# // to globally reset paths:
|
||||
# // gofmt -w -r "go.wit.com/gui -> go.wit.com/gui/gui" .
|
||||
|
||||
redomod:
|
||||
rm -f go.*
|
||||
GO111MODULE= go mod init
|
||||
GO111MODULE= go mod tidy
|
||||
|
||||
reset:
|
||||
# clear your terminal
|
||||
reset
|
||||
|
||||
clean:
|
||||
-rm autogenpb
|
|
@ -0,0 +1,99 @@
|
|||
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)
|
||||
// }
|
||||
|
||||
func (r *Repo) AllGoDeps() *GoDepIterator {
|
||||
repoPointers := r.selectAllGoDeps()
|
||||
|
||||
iterator := NewGoDepIterator(repoPointers)
|
||||
return iterator
|
||||
}
|
||||
|
||||
func (r *Repo) SortGoDepsByName() *GoDepIterator {
|
||||
packs := r.selectAllGoDeps()
|
||||
|
||||
sort.Sort(GoDepByPath(packs))
|
||||
|
||||
iterator := NewGoDepIterator(packs)
|
||||
return iterator
|
||||
}
|
||||
|
||||
func (repo *Repo) Len() int {
|
||||
refslock.RLock()
|
||||
defer refslock.RUnlock()
|
||||
|
||||
return len(repo.GoDeps)
|
||||
}
|
||||
|
||||
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
|
||||
func (r *Repo) selectAllGoDeps() []*GoDep {
|
||||
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
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, _ := os.OpenFile("test.sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600)
|
||||
header(f, "GitRefs")
|
||||
}
|
||||
|
||||
func header(w io.Writer, name string) {
|
||||
fmt.Fprintln(w, "package gitpb")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, "// this is becoming a standard format")
|
||||
fmt.Fprintln(w, "// todo: autogenerate this from the .proto file?")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, "import (")
|
||||
fmt.Fprintln(w, " \"fmt\"")
|
||||
fmt.Fprintln(w, " \"os\"")
|
||||
fmt.Fprintln(w, " \"sort\"")
|
||||
fmt.Fprintln(w, " \"sync\"")
|
||||
fmt.Fprintln(w, ")")
|
||||
fmt.Fprintln(w, "")
|
||||
fmt.Fprintln(w, "// bad global lock until I figure out some other plan")
|
||||
fmt.Fprintln(w, "var godeplock sync.RWMutex")
|
||||
fmt.Fprintln(w, "")
|
||||
}
|
Loading…
Reference in New Issue