This commit is contained in:
Jeff Carr 2024-11-27 21:21:06 -06:00
parent 0fc7c2d753
commit f5ad4a7a41
7 changed files with 7 additions and 263 deletions

View File

@ -5,7 +5,7 @@
# go install
all: repo.pb.go forgeConfig.pb.go
all: forgeConfig.pb.go
make -C forgeConfig
vet: lint
@ -29,12 +29,6 @@ clean:
-rm -f go.*
make -C forgeConfig clean
repo.pb.go: repo.proto
# I'm using version v1.35.x from google.golang.org/protobuf/cmd/protoc-gen-go
cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/forgepb \
--go_opt=Mrepo.proto=go.wit.com/lib/protobuf/forgepb \
repo.proto
forgeConfig.pb.go: forgeConfig.proto
# I'm using version v1.35.x from google.golang.org/protobuf/cmd/protoc-gen-go
cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/forgepb \

View File

@ -48,7 +48,7 @@ func (c *ForgeConfigs) ConfigLoad() error {
var data []byte
var err error
if c == nil {
// can't safely do c = new(Repo) if c is in a struct from the caller. notsure why
// can't safely do c = new(ForgeConfig) if c is in a struct from the caller. notsure why
return errors.New("It's not safe to run ConfigLoad() on a nil")
}

View File

@ -31,7 +31,7 @@ message ForgeConfig {
google.protobuf.Timestamp verstamp = 12; // the git commit timestamp of the version
}
// TODO: autogen 'Repos'
// TODO: autogen 'sort', 'marshal'
message ForgeConfigs {
string uuid = 1; // could be useful for /usr/share/file/magic someday?
string version = 2; // could be used for protobuf schema change violations?

View File

@ -20,8 +20,8 @@ type ForgeConfigIterator struct {
index int
}
// NewForgeConfigIterator initializes a new iterator.
func NewForgeConfigIterator(packs []*ForgeConfig) *ForgeConfigIterator {
// newForgeConfigIterator initializes a new iterator.
func newForgeConfigIterator(packs []*ForgeConfig) *ForgeConfigIterator {
return &ForgeConfigIterator{packs: packs}
}
@ -57,7 +57,7 @@ func (it *ForgeConfigIterator) Next() *ForgeConfig {
func (r *ForgeConfigs) All() *ForgeConfigIterator {
forgeConfigPointers := r.selectAllForgeConfigs()
iterator := NewForgeConfigIterator(forgeConfigPointers)
iterator := newForgeConfigIterator(forgeConfigPointers)
return iterator
}
@ -66,7 +66,7 @@ func (r *ForgeConfigs) SortByPath() *ForgeConfigIterator {
sort.Sort(ByForgeConfigPath(packs))
iterator := NewForgeConfigIterator(packs)
iterator := newForgeConfigIterator(packs)
return iterator
}

View File

@ -1,60 +0,0 @@
package forgepb
// TODO: autogen this
// functions to import and export the protobuf
// data to and from config files
import (
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
// "google.golang.org/protobuf/proto"
)
// human readable JSON
func (p *Repos) FormatJSON() string {
return protojson.Format(p)
}
// 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()
// TODO: fix things so this is the default
func (p *Repos) FormatTEXT() string {
return prototext.Format(p)
}
// unmarshalTEXT
func (p *Repos) UnmarshalTEXT(data []byte) error {
return prototext.Unmarshal(data, p)
}
// marshal json
func (p *Repos) MarshalJSON() ([]byte, error) {
return protojson.Marshal(p)
}
// unmarshal
func (p *Repos) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, p)
}
// marshal to wire
func (m *Repo) Marshal() ([]byte, error) {
return proto.Marshal(m)
}
// unmarshal from wire
func (m *Repo) Unmarshal(data []byte) error {
return proto.Unmarshal(data, m)
}
// marshal to wire
func (m *Repos) Marshal() ([]byte, error) {
return proto.Marshal(m)
}
// unmarshal from wire
func (m *Repos) Unmarshal(data []byte) error {
return proto.Unmarshal(data, m)
}

View File

@ -1,39 +0,0 @@
syntax = "proto3";
package forgepb;
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
// define 3 branches. that is all that is supported
// the term 'master' is used in the code because 'main' is a reserved word in golang already
// allow 'read only' and 'private' flags
// package names sometimes must be different than the binary name
// for example 'zookeeper' is packaged as 'zookeeper-go'
// due to the prior apache foundation project. This happens and is ok!
message Repo {
string goPath = 1; // Examples: 'go.wit.com/apps/go-clone' or "~/mythings" or "/home/src/foo"
bool writable = 2; // if you have write access to the repo
bool readOnly = 3; // the opposite, but needed for now because I don't know what I'm doing
bool private = 4; // if the repo can be published
bool directory = 5; // everything in this directory should use these writable & private values
bool favorite = 6; // you like this. always git clone/go clone this repo
bool interesting = 7; // this is something interesting you found and want to remember it
string masterBranch = 8; // git 'main' or 'master' branch name
string develBranch = 9; // whatever the git 'devel' branch name is
string userBranch = 10; // whatever your username branch is
string debName = 11; // the actual name used with 'apt install' (or distro apt equivalent.
// todo: appeal to everyone to alias 'apt' on rhat, gentoo, arch, etc to alias 'apt install'
// so we can make easier instructions for new linux users. KISS
google.protobuf.Timestamp verstamp = 12; // the git commit timestamp of the version
}
// TODO: autogen 'Repos'
message Repos {
string uuid = 1; // could be useful for /usr/share/file/magic someday?
string version = 2; // could be used for protobuf schema change violations?
repeated Repo repos = 3;
}

151
repos.go
View File

@ -1,151 +0,0 @@
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
}
func (r *Repos) SortByPath() *RepoIterator {
packs := r.selectAllRepos()
sort.Sort(ByRepoPath(packs))
iterator := NewRepoIterator(packs)
return iterator
}
// enforces no duplicate repo paths
func (r *Repos) Append(newP *Repo) bool {
reposLock.Lock()
defer reposLock.Unlock()
for _, p := range r.Repos {
if p.GoPath == newP.GoPath {
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
}
// find a repo by path
func (r *Repos) FindByPath(gopath string) *Repo {
reposLock.RLock()
defer reposLock.RUnlock()
for _, p := range r.Repos {
if p.GoPath == gopath {
return p
}
}
return nil
}
func (r *Repos) Len() int {
reposLock.RLock()
defer reposLock.RUnlock()
return len(r.Repos)
}
type ByRepoPath []*Repo
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] }
func (all *Repos) DeleteByPath(gopath string) *Repo {
reposLock.Lock()
defer reposLock.Unlock()
var newr Repo
for i, _ := range all.Repos {
if all.Repos[i].GoPath == gopath {
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
}
// 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
}