From 1321787a3b4c92ee9c9ac5c21a0c511c1c2eaff5 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 27 Nov 2024 21:41:57 -0600 Subject: rename. always use plural form for proto files? Signed-off-by: Jeff Carr --- repo.helpers.go | 46 -------------------------- repo.marshal.go | 42 ------------------------ repo.new.go | 74 ------------------------------------------ repo.sort.go | 99 -------------------------------------------------------- repos.helpers.go | 46 ++++++++++++++++++++++++++ repos.marshal.go | 42 ++++++++++++++++++++++++ repos.new.go | 74 ++++++++++++++++++++++++++++++++++++++++++ repos.sort.go | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 261 insertions(+), 261 deletions(-) delete mode 100644 repo.helpers.go delete mode 100644 repo.marshal.go delete mode 100644 repo.new.go delete mode 100644 repo.sort.go create mode 100644 repos.helpers.go create mode 100644 repos.marshal.go create mode 100644 repos.new.go create mode 100644 repos.sort.go diff --git a/repo.helpers.go b/repo.helpers.go deleted file mode 100644 index 3420526..0000000 --- a/repo.helpers.go +++ /dev/null @@ -1,46 +0,0 @@ -package gitpb - -// delete a gopath: -// myrepos.DeleteByPath("go.wit.com/apps/go-clone") -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 -} - -// find a package by gopath -func (all *Repos) FindByGoPath(gopath string) *Repo { - repolock.RLock() - defer repolock.RUnlock() - - for _, p := range all.Repos { - if p.GoPath == gopath { - return p - } - } - - return nil -} - -// enforces no duplicate gopath's -func (all *Repos) add(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 -} diff --git a/repo.marshal.go b/repo.marshal.go deleted file mode 100644 index 738ae35..0000000 --- a/repo.marshal.go +++ /dev/null @@ -1,42 +0,0 @@ -package gitpb - -// todo: autogen this -// functions to import and export the protobuf - -import ( - "google.golang.org/protobuf/encoding/protojson" - "google.golang.org/protobuf/encoding/prototext" - "google.golang.org/protobuf/proto" -) - -// human readable JSON -func (r *Repo) FormatJSON() string { - return protojson.Format(r) -} - -// apparently this isn't supposed to be used? -// https://protobuf.dev/reference/go/faq/#unstable-text -// this is a shame because this is much nicer output than JSON Format() -func (r *Repo) FormatTEXT() string { - return prototext.Format(r) -} - -// marshal json -func (r *Repo) MarshalJSON() ([]byte, error) { - return protojson.Marshal(r) -} - -// unmarshal -func (r *Repo) UnmarshalJSON(data []byte) error { - return protojson.Unmarshal(data, r) -} - -// marshal to wire -func (r *Repo) Marshal() ([]byte, error) { - return proto.Marshal(r) -} - -// unmarshal from wire -func (r *Repo) Unmarshal(data []byte) error { - return proto.Unmarshal(data, r) -} diff --git a/repo.new.go b/repo.new.go deleted file mode 100644 index bab2b25..0000000 --- a/repo.new.go +++ /dev/null @@ -1,74 +0,0 @@ -package gitpb - -import ( - "bufio" - "errors" - "os" - "path/filepath" - "strings" - - "go.wit.com/log" -) - -// scans in a new git repo. If it detects the repo is a golang project, -// then it parses the go.mod/go.sum files -// TODO: try adding python, rails, perl, rust, other language things? -// I probably will never have time to try that, but I'd take patches for anyone -// that might see this note and feel so inclined. -func (all *Repos) NewGoPath(basepath string, gopath string) (*Repo, error) { - if r := all.FindByGoPath(gopath); r != nil { - // already had this gopath - return r, nil - } - - // if .git doesn't exist, error out here - gitpath := filepath.Join(basepath, gopath, ".git") - _, err := os.Stat(gitpath) - if err != nil { - return nil, err - } - - // add a new one here - newr := Repo{ - FullPath: filepath.Join(basepath, gopath), - GoPath: gopath, - } - newr.UpdateGit() - - all.add(&newr) - return &newr, nil -} - -// Detect a 'Primative' package. Sets the isPrimative flag -// will return true if the repo is truly not dependent on _anything_ else -// like spew or lib/widget -// it assumes go mod ran init and tidy ran without error -func (repo *Repo) isPrimativeGoMod() (bool, error) { - // go mod init & go mod tidy ran without errors - log.Log(GITPB, "isPrimativeGoMod()", repo.FullPath) - tmp := filepath.Join(repo.FullPath, "go.mod") - gomod, err := os.Open(tmp) - if err != nil { - log.Log(GITPB, "missing go.mod", repo.FullPath) - repo.GoDeps = nil - return false, err - } - defer gomod.Close() - - scanner := bufio.NewScanner(gomod) - for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) - - parts := strings.Split(line, " ") - log.Log(GITPB, " gomod:", parts) - if len(parts) >= 1 { - log.Log(GITPB, " gomod: part[0] =", parts[0]) - if parts[0] == "require" { - log.Log(GITPB, " should return false here") - return false, errors.New("go.mod file is not primative") - } - - } - } - return true, nil -} diff --git a/repo.sort.go b/repo.sort.go deleted file mode 100644 index b4656e6..0000000 --- a/repo.sort.go +++ /dev/null @@ -1,99 +0,0 @@ -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 -} - -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 -} diff --git a/repos.helpers.go b/repos.helpers.go new file mode 100644 index 0000000..3420526 --- /dev/null +++ b/repos.helpers.go @@ -0,0 +1,46 @@ +package gitpb + +// delete a gopath: +// myrepos.DeleteByPath("go.wit.com/apps/go-clone") +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 +} + +// find a package by gopath +func (all *Repos) FindByGoPath(gopath string) *Repo { + repolock.RLock() + defer repolock.RUnlock() + + for _, p := range all.Repos { + if p.GoPath == gopath { + return p + } + } + + return nil +} + +// enforces no duplicate gopath's +func (all *Repos) add(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 +} diff --git a/repos.marshal.go b/repos.marshal.go new file mode 100644 index 0000000..738ae35 --- /dev/null +++ b/repos.marshal.go @@ -0,0 +1,42 @@ +package gitpb + +// todo: autogen this +// functions to import and export the protobuf + +import ( + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/encoding/prototext" + "google.golang.org/protobuf/proto" +) + +// human readable JSON +func (r *Repo) FormatJSON() string { + return protojson.Format(r) +} + +// apparently this isn't supposed to be used? +// https://protobuf.dev/reference/go/faq/#unstable-text +// this is a shame because this is much nicer output than JSON Format() +func (r *Repo) FormatTEXT() string { + return prototext.Format(r) +} + +// marshal json +func (r *Repo) MarshalJSON() ([]byte, error) { + return protojson.Marshal(r) +} + +// unmarshal +func (r *Repo) UnmarshalJSON(data []byte) error { + return protojson.Unmarshal(data, r) +} + +// marshal to wire +func (r *Repo) Marshal() ([]byte, error) { + return proto.Marshal(r) +} + +// unmarshal from wire +func (r *Repo) Unmarshal(data []byte) error { + return proto.Unmarshal(data, r) +} diff --git a/repos.new.go b/repos.new.go new file mode 100644 index 0000000..bab2b25 --- /dev/null +++ b/repos.new.go @@ -0,0 +1,74 @@ +package gitpb + +import ( + "bufio" + "errors" + "os" + "path/filepath" + "strings" + + "go.wit.com/log" +) + +// scans in a new git repo. If it detects the repo is a golang project, +// then it parses the go.mod/go.sum files +// TODO: try adding python, rails, perl, rust, other language things? +// I probably will never have time to try that, but I'd take patches for anyone +// that might see this note and feel so inclined. +func (all *Repos) NewGoPath(basepath string, gopath string) (*Repo, error) { + if r := all.FindByGoPath(gopath); r != nil { + // already had this gopath + return r, nil + } + + // if .git doesn't exist, error out here + gitpath := filepath.Join(basepath, gopath, ".git") + _, err := os.Stat(gitpath) + if err != nil { + return nil, err + } + + // add a new one here + newr := Repo{ + FullPath: filepath.Join(basepath, gopath), + GoPath: gopath, + } + newr.UpdateGit() + + all.add(&newr) + return &newr, nil +} + +// Detect a 'Primative' package. Sets the isPrimative flag +// will return true if the repo is truly not dependent on _anything_ else +// like spew or lib/widget +// it assumes go mod ran init and tidy ran without error +func (repo *Repo) isPrimativeGoMod() (bool, error) { + // go mod init & go mod tidy ran without errors + log.Log(GITPB, "isPrimativeGoMod()", repo.FullPath) + tmp := filepath.Join(repo.FullPath, "go.mod") + gomod, err := os.Open(tmp) + if err != nil { + log.Log(GITPB, "missing go.mod", repo.FullPath) + repo.GoDeps = nil + return false, err + } + defer gomod.Close() + + scanner := bufio.NewScanner(gomod) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + + parts := strings.Split(line, " ") + log.Log(GITPB, " gomod:", parts) + if len(parts) >= 1 { + log.Log(GITPB, " gomod: part[0] =", parts[0]) + if parts[0] == "require" { + log.Log(GITPB, " should return false here") + return false, errors.New("go.mod file is not primative") + } + + } + } + return true, nil +} diff --git a/repos.sort.go b/repos.sort.go new file mode 100644 index 0000000..b4656e6 --- /dev/null +++ b/repos.sort.go @@ -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 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 +} + +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 +} -- cgit v1.2.3