switch to autogenpb
This commit is contained in:
parent
a4f3f95196
commit
e4b150d6aa
24
Makefile
24
Makefile
|
@ -5,19 +5,16 @@
|
|||
# go install
|
||||
|
||||
|
||||
all: package.pb.go machine.pb.go
|
||||
make -C example
|
||||
all: package.pb.go machine.pb.go vet
|
||||
|
||||
vet: lint
|
||||
vet:
|
||||
@GO111MODULE=off go vet
|
||||
@echo this go library package builds okay
|
||||
GO111MODULE=off go vet
|
||||
|
||||
lint:
|
||||
-buf lint git.proto
|
||||
|
||||
# autofixes your import headers in your golang files
|
||||
goimports:
|
||||
goimports -w *.go
|
||||
make -C example goimports
|
||||
|
||||
redomod:
|
||||
rm -f go.*
|
||||
|
@ -27,18 +24,9 @@ redomod:
|
|||
clean:
|
||||
rm -f *.pb.go
|
||||
-rm -f go.*
|
||||
make -C example clean
|
||||
|
||||
package.pb.go: package.proto
|
||||
# protoc --go_out=. droplet.proto
|
||||
# This is switched over to use the new protoc-gen-go from google.golang.org/protobuf/cmd/protoc-gen-go
|
||||
# the debian one (2024/10/21) seems to be the older/original one from github.com/golang/protobuf/protoc-gen-go
|
||||
cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/zoopb \
|
||||
--go_opt=Mpackage.proto=go.wit.com/lib/protobuf/zoopb \
|
||||
package.proto
|
||||
autogenpb --proto package.proto --mutex
|
||||
|
||||
machine.pb.go: machine.proto
|
||||
cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/zoopb \
|
||||
--go_opt=Mpackage.proto=go.wit.com/lib/protobuf/zoopb \
|
||||
--go_opt=Mmachine.proto=go.wit.com/lib/protobuf/zoopb \
|
||||
machine.proto
|
||||
autogenpb --proto machine.proto --mutex
|
||||
|
|
7
apt.go
7
apt.go
|
@ -59,9 +59,10 @@ func (me *Machine) UpdatePackages() string {
|
|||
newCounter += 1
|
||||
} else {
|
||||
found.Version = version
|
||||
if me.Packages.Update(found) {
|
||||
changeCounter += 1
|
||||
}
|
||||
panic("redo this. broken after autogenpb. was never right anyway")
|
||||
//if me.Packages.Update(found) {
|
||||
// changeCounter += 1
|
||||
//}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,39 +0,0 @@
|
|||
package zoopb
|
||||
|
||||
// todo: autogen this
|
||||
|
||||
import (
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"google.golang.org/protobuf/encoding/prototext"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// marshal to wire
|
||||
func (m *Machine) Marshal() ([]byte, error) {
|
||||
return proto.Marshal(m)
|
||||
}
|
||||
|
||||
// marshal to TEXT
|
||||
func (m *Machine) MarshalTEXT() ([]byte, error) {
|
||||
return prototext.Marshal(m)
|
||||
}
|
||||
|
||||
// marshal to JSON
|
||||
func (m *Machine) MarshalJSON() ([]byte, error) {
|
||||
return protojson.Marshal(m)
|
||||
}
|
||||
|
||||
// unmarshal from wire
|
||||
func (m *Machine) Unmarshal(data []byte) error {
|
||||
return proto.Unmarshal(data, m)
|
||||
}
|
||||
|
||||
// marshal to wire
|
||||
func (m *Machines) Marshal() ([]byte, error) {
|
||||
return proto.Marshal(m)
|
||||
}
|
||||
|
||||
// unmarshal from wire
|
||||
func (m *Machines) Unmarshal(data []byte) error {
|
||||
return proto.Unmarshal(data, m)
|
||||
}
|
|
@ -5,8 +5,8 @@ package gitpb;
|
|||
import "package.proto";
|
||||
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
|
||||
|
||||
message Machine {
|
||||
string hostname = 1;
|
||||
message Machine { // `autogenpb:marshal`
|
||||
string hostname = 1; // `autogenpb:unique`
|
||||
int64 memory = 2;
|
||||
int64 cpus = 3;
|
||||
string distro = 4;
|
||||
|
@ -17,7 +17,7 @@ message Machine {
|
|||
Packages wit = 9; // packages that are available from mirrors.wit.com
|
||||
}
|
||||
|
||||
message Machines {
|
||||
message Machines { // `autogenpb:marshal`
|
||||
string uuid = 1; // I guess why not just have this on each file
|
||||
string version = 2; // maybe can be used for protobuf schema change violations
|
||||
repeated Machine machines = 3;
|
||||
|
|
146
machine.sort.go
146
machine.sort.go
|
@ -1,146 +0,0 @@
|
|||
package zoopb
|
||||
|
||||
// this is becoming a standard format
|
||||
// todo: autogenerate this from the .proto file?
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
sync "sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// bad global lock until I figure out some other plan
|
||||
var machinesLock sync.RWMutex
|
||||
|
||||
type MachineIterator struct {
|
||||
sync.RWMutex
|
||||
|
||||
packs []*Machine
|
||||
index int
|
||||
}
|
||||
|
||||
// NewMachineIterator initializes a new iterator.
|
||||
func NewMachineIterator(packs []*Machine) *MachineIterator {
|
||||
return &MachineIterator{packs: packs}
|
||||
}
|
||||
|
||||
// Scan moves to the next element and returns false if there are no more packs.
|
||||
func (it *MachineIterator) Scan() bool {
|
||||
if it.index >= len(it.packs) {
|
||||
return false
|
||||
}
|
||||
it.index++
|
||||
return true
|
||||
}
|
||||
|
||||
// Machine returns the current repo.
|
||||
func (it *MachineIterator) Machine() *Machine {
|
||||
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.Machine()
|
||||
// fmt.Println("Machine UUID:", d.Uuid)
|
||||
// }
|
||||
|
||||
func (r *Machines) All() *MachineIterator {
|
||||
repoPointers := r.selectAllMachines()
|
||||
|
||||
iterator := NewMachineIterator(repoPointers)
|
||||
return iterator
|
||||
}
|
||||
|
||||
func (r *Machines) SortByName() *MachineIterator {
|
||||
packs := r.selectAllMachines()
|
||||
|
||||
sort.Sort(ByMachineName(packs))
|
||||
|
||||
iterator := NewMachineIterator(packs)
|
||||
return iterator
|
||||
}
|
||||
|
||||
// enforces no duplicate package names
|
||||
func (r *Machines) Append(newP *Machine) bool {
|
||||
machinesLock.Lock()
|
||||
defer machinesLock.Unlock()
|
||||
|
||||
for _, p := range r.Machines {
|
||||
if p.Hostname == newP.Hostname {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
r.Machines = append(r.Machines, newP)
|
||||
return true
|
||||
}
|
||||
|
||||
// returns time.Duration since last Update()
|
||||
func (r *Machine) Age(newP *Machine) time.Duration {
|
||||
t := time.Since(r.Laststamp.AsTime())
|
||||
return t
|
||||
}
|
||||
|
||||
// find a machine by name
|
||||
func (r *Machines) FindByName(name string) *Machine {
|
||||
machinesLock.RLock()
|
||||
defer machinesLock.RUnlock()
|
||||
|
||||
for _, p := range r.Machines {
|
||||
if p.Hostname == name {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// find a package by name
|
||||
func (m *Machine) FindPackageByName(name string) *Package {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
if m.Packages == nil {
|
||||
return nil
|
||||
}
|
||||
return m.Packages.FindByName(name)
|
||||
}
|
||||
|
||||
func (r *Machines) Len() int {
|
||||
machinesLock.RLock()
|
||||
defer machinesLock.RUnlock()
|
||||
|
||||
return len(r.Machines)
|
||||
}
|
||||
|
||||
type ByMachineName []*Machine
|
||||
|
||||
func (a ByMachineName) Len() int { return len(a) }
|
||||
func (a ByMachineName) Less(i, j int) bool { return a[i].Hostname < a[j].Hostname }
|
||||
func (a ByMachineName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
|
||||
// safely returns a slice of pointers to the Machine protobufs
|
||||
func (r *Machines) selectAllMachines() []*Machine {
|
||||
machinesLock.RLock()
|
||||
defer machinesLock.RUnlock()
|
||||
|
||||
// Create a new slice to hold pointers to each Machine
|
||||
var allPacks []*Machine
|
||||
allPacks = make([]*Machine, len(r.Machines))
|
||||
for i, p := range r.Machines {
|
||||
allPacks[i] = p // Copy pointers for safe iteration
|
||||
}
|
||||
|
||||
return allPacks
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
package zoopb
|
||||
|
||||
// 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 (p *Packages) 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()
|
||||
func (p *Packages) FormatTEXT() string {
|
||||
return prototext.Format(p)
|
||||
}
|
||||
|
||||
// marshal json
|
||||
func (p *Packages) MarshalJSON() ([]byte, error) {
|
||||
return protojson.Marshal(p)
|
||||
}
|
||||
|
||||
// unmarshal
|
||||
func (p *Packages) UnmarshalJSON(data []byte) error {
|
||||
return protojson.Unmarshal(data, p)
|
||||
}
|
||||
|
||||
// marshal to wire
|
||||
func (p *Packages) Marshal() ([]byte, error) {
|
||||
return proto.Marshal(p)
|
||||
}
|
||||
|
||||
// unmarshal from wire
|
||||
func (p *Packages) Unmarshal(data []byte) error {
|
||||
return proto.Unmarshal(data, p)
|
||||
}
|
|
@ -6,7 +6,7 @@ package gitpb;
|
|||
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
|
||||
|
||||
message Package {
|
||||
string name = 1; // name: zookeeper-go
|
||||
string name = 1; // `autogenpb:unique` // name: zookeeper-go
|
||||
string version = 2; // version: 0.0.3
|
||||
google.protobuf.Timestamp laststamp = 3; // the last time this package was seen (used to timeout entries)
|
||||
string srcPath = 4; // path to the sources (go.wit.com/apps/zookeeper)
|
||||
|
@ -14,7 +14,7 @@ message Package {
|
|||
string pkgName = 6; // the apt filename pool/main/f/foo/foo_2.2.2_riscv64.deb
|
||||
}
|
||||
|
||||
message Packages {
|
||||
message Packages { // `autogenpb:marshal`
|
||||
string uuid = 1; // I guess why not just have this on each file
|
||||
string version = 2; // maybe can be used for protobuf schema change violations
|
||||
repeated Package packages = 3;
|
||||
|
|
196
package.sort.go
196
package.sort.go
|
@ -1,196 +0,0 @@
|
|||
package zoopb
|
||||
|
||||
// this is becoming a standard format
|
||||
// todo: autogenerate this from the .proto file?
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
sync "sync"
|
||||
"time"
|
||||
|
||||
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
// bad global lock until I figure out some other plan
|
||||
var lock sync.RWMutex
|
||||
|
||||
type PackageIterator struct {
|
||||
sync.RWMutex
|
||||
|
||||
packs []*Package
|
||||
index int
|
||||
}
|
||||
|
||||
// NewPackageIterator initializes a new iterator.
|
||||
func NewPackageIterator(packs []*Package) *PackageIterator {
|
||||
return &PackageIterator{packs: packs}
|
||||
}
|
||||
|
||||
// Scan moves to the next element and returns false if there are no more packs.
|
||||
func (it *PackageIterator) Scan() bool {
|
||||
if it.index >= len(it.packs) {
|
||||
return false
|
||||
}
|
||||
it.index++
|
||||
return true
|
||||
}
|
||||
|
||||
// Package returns the current repo.
|
||||
func (it *PackageIterator) Package() *Package {
|
||||
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.Package()
|
||||
// fmt.Println("Package UUID:", d.Uuid)
|
||||
// }
|
||||
|
||||
func (r *Packages) All() *PackageIterator {
|
||||
repoPointers := r.selectAllPackages()
|
||||
|
||||
iterator := NewPackageIterator(repoPointers)
|
||||
return iterator
|
||||
}
|
||||
|
||||
func (r *Packages) SortByName() *PackageIterator {
|
||||
packs := r.selectAllPackages()
|
||||
|
||||
sort.Sort(ByName(packs))
|
||||
|
||||
iterator := NewPackageIterator(packs)
|
||||
return iterator
|
||||
}
|
||||
|
||||
// enforces no duplicate package names
|
||||
func (r *Packages) Append(newP *Package) bool {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
for _, p := range r.Packages {
|
||||
if p.Name == newP.Name {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
r.Packages = append(r.Packages, newP)
|
||||
return true
|
||||
}
|
||||
|
||||
// Update version and timestamp.
|
||||
// returns ok (ok == true if not found)
|
||||
func (r *Packages) Update(newP *Package) bool {
|
||||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
var found *Package
|
||||
for _, p := range r.Packages {
|
||||
if p.Name == newP.Name {
|
||||
found = p
|
||||
}
|
||||
}
|
||||
if found == nil {
|
||||
// r.Append(newP) // update here?
|
||||
return true
|
||||
}
|
||||
|
||||
var changed bool = false
|
||||
if newP.Version != found.Version {
|
||||
changed = true
|
||||
found.Version = newP.Version
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
found.Laststamp = timestamppb.New(now)
|
||||
return changed
|
||||
}
|
||||
|
||||
// returns time.Duration since last Update()
|
||||
func (r *Package) Age(newP *Package) time.Duration {
|
||||
t := time.Since(r.Laststamp.AsTime())
|
||||
return t
|
||||
}
|
||||
|
||||
// find a package by name
|
||||
func (r *Packages) FindByName(name string) *Package {
|
||||
lock.RLock()
|
||||
defer lock.RUnlock()
|
||||
|
||||
for _, p := range r.Packages {
|
||||
if p.Name == name {
|
||||
return p
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Packages) Len() int {
|
||||
lock.RLock()
|
||||
defer lock.RUnlock()
|
||||
|
||||
return len(r.Packages)
|
||||
}
|
||||
|
||||
type ByName []*Package
|
||||
|
||||
func (a ByName) Len() int { return len(a) }
|
||||
func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
|
||||
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
|
||||
// safely returns a slice of pointers to the Package protobufs
|
||||
func (r *Packages) selectAllPackages() []*Package {
|
||||
lock.RLock()
|
||||
defer lock.RUnlock()
|
||||
|
||||
// Create a new slice to hold pointers to each Package
|
||||
var allPacks []*Package
|
||||
allPacks = make([]*Package, len(r.Packages))
|
||||
for i, p := range r.Packages {
|
||||
allPacks[i] = p // Copy pointers for safe iteration
|
||||
}
|
||||
|
||||
return allPacks
|
||||
}
|
||||
|
||||
/*
|
||||
func (r *Packages) UnmergedPackageRepos() *PackageRepoIterator {
|
||||
repoPointers := r.selectUnmergedPackageRepos()
|
||||
|
||||
sort.Sort(ByName(repoPointers))
|
||||
|
||||
iterator := NewPackageRepoIterator(repoPointers)
|
||||
|
||||
return iterator
|
||||
}
|
||||
*/
|
||||
|
||||
/*
|
||||
// this sort doesn't really work. I think it forgets to sort the last two
|
||||
// todo: sort this out. literally
|
||||
// SelectPackagePointers safely returns a slice of pointers to Package records.
|
||||
func (r *Packages) selectUnmergedPackages() []*PackageRow {
|
||||
r.RLock()
|
||||
defer r.RUnlock()
|
||||
|
||||
// Create a new slice to hold pointers to each Package
|
||||
// repoPointers := make([]*Package, len(c.E.Packages))
|
||||
var repoPointers []*PackageRow
|
||||
for _, repo := range me.allrepos {
|
||||
repoPointers = append(repoPointers, repo) // Copy pointers for safe iteration
|
||||
}
|
||||
|
||||
return repoPointers
|
||||
}
|
||||
*/
|
8
wit.go
8
wit.go
|
@ -13,7 +13,7 @@ var BUILDTIME string
|
|||
func (m *Machine) IsInstalled(name string) bool {
|
||||
loop := m.Packages.SortByName()
|
||||
for loop.Scan() {
|
||||
p := loop.Package()
|
||||
p := loop.Next()
|
||||
if name == p.Name {
|
||||
// log.Info("package installed:", p.Name, p.Version, p.PkgName)
|
||||
return true
|
||||
|
@ -27,7 +27,7 @@ func (m *Machine) IsInstalled(name string) bool {
|
|||
func (m *Machine) FindInstalledByName(name string) *Package {
|
||||
loop := m.Packages.SortByName()
|
||||
for loop.Scan() {
|
||||
p := loop.Package()
|
||||
p := loop.Next()
|
||||
if name == p.Name {
|
||||
// log.Info("package installed:", p.Name, p.Version, p.PkgName)
|
||||
return p
|
||||
|
@ -44,7 +44,7 @@ func (m *Machine) FindVersion(name string, version string) *Package {
|
|||
// first check all installed versions
|
||||
loop := m.Packages.SortByName()
|
||||
for loop.Scan() {
|
||||
p := loop.Package()
|
||||
p := loop.Next()
|
||||
if name == p.Name {
|
||||
if version == "" {
|
||||
return p
|
||||
|
@ -59,7 +59,7 @@ func (m *Machine) FindVersion(name string, version string) *Package {
|
|||
// check all wit packages
|
||||
loop = m.Wit.SortByName()
|
||||
for loop.Scan() {
|
||||
p := loop.Package()
|
||||
p := loop.Next()
|
||||
if name == p.Name {
|
||||
if version == "" {
|
||||
return p
|
||||
|
|
Loading…
Reference in New Issue