switched to autogenpb
This commit is contained in:
parent
5b883de7b9
commit
afd0bd6428
42
Makefile
42
Makefile
|
@ -3,27 +3,15 @@
|
|||
# cd ~/go/src/google.golang.org/protobuf/cmd/protoc-gen-go
|
||||
# go install
|
||||
|
||||
check-for-protoc-gen-go:
|
||||
@if [ -f "/usr/bin/protoc-gen-go" ]; then \
|
||||
echo "the protoc-gen-go package is old in debian sid right now"; \
|
||||
echo "for now, remove it"; \
|
||||
echo "and install protoc-gen-go-wit from mirrors.wit.com"; \
|
||||
fi
|
||||
make all
|
||||
all: droplet.pb.go hypervisor.pb.go event.pb.go experiments.pb.go vet
|
||||
|
||||
all: droplet.pb.go hypervisor.pb.go event.pb.go experiments.pb.go
|
||||
make -C example
|
||||
|
||||
vet: lint
|
||||
GO111MODULE=off go vet
|
||||
|
||||
lint:
|
||||
-buf lint droplet.proto
|
||||
vet:
|
||||
@GO111MODULE=off go vet
|
||||
@echo this go library package builds okay
|
||||
|
||||
# autofixes your import headers in your golang files
|
||||
goimports:
|
||||
goimports -w *.go
|
||||
make -C example goimports
|
||||
|
||||
redomod:
|
||||
rm -f go.*
|
||||
|
@ -33,32 +21,18 @@ redomod:
|
|||
clean:
|
||||
rm -f *.pb.go
|
||||
-rm -f go.*
|
||||
make -C example clean
|
||||
|
||||
droplet.pb.go: droplet.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/virtbuf \
|
||||
--go_opt=Mdroplet.proto=go.wit.com/lib/protobuf/virtbuf \
|
||||
droplet.proto
|
||||
autogenpb --proto droplet.proto --mutex
|
||||
|
||||
hypervisor.pb.go: hypervisor.proto
|
||||
cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/lib/protobuf/virtbuf \
|
||||
--go_opt=Mhypervisor.proto=go.wit.com/lib/protobuf/virtbuf \
|
||||
hypervisor.proto
|
||||
autogenpb --proto hypervisor.proto --mutex
|
||||
|
||||
event.pb.go: event.proto
|
||||
cd ~/go/src && protoc --go_out=. \
|
||||
--proto_path=go.wit.com/lib/protobuf/virtbuf \
|
||||
--go_opt=Mevent.proto=go.wit.com/lib/protobuf/virtbuf \
|
||||
event.proto
|
||||
autogenpb --proto event.proto --mutex
|
||||
|
||||
experiments.pb.go: experiments.proto
|
||||
cd ~/go/src && protoc --go_out=. \
|
||||
--proto_path=go.wit.com/lib/protobuf/virtbuf \
|
||||
--go_opt=Mexperiments.proto=go.wit.com/lib/protobuf/virtbuf \
|
||||
experiments.proto
|
||||
autogenpb --proto experiments.proto --no-marshal --no-sort
|
||||
|
||||
deps:
|
||||
apt install golang-goprotobuf-dev
|
||||
|
|
8
add.go
8
add.go
|
@ -51,9 +51,9 @@ func (x *Hypervisor) SetMemoryGB(gb int) {
|
|||
}
|
||||
|
||||
func (c *Cluster) FindDropletByName(name string) *Droplet {
|
||||
loop := c.DropletsAll() // get the list of droplets
|
||||
loop := c.d.All() // get the list of droplets
|
||||
for loop.Scan() {
|
||||
d := loop.Droplet()
|
||||
d := loop.Next()
|
||||
if d.Hostname == name {
|
||||
return d
|
||||
}
|
||||
|
@ -150,9 +150,9 @@ func (c *Cluster) AddDropletLocal(name string, hypername string) *Droplet {
|
|||
}
|
||||
|
||||
func (c *Cluster) BlankFields() {
|
||||
loop := c.DropletsAll() // get the list of droplets
|
||||
loop := c.d.All() // get the list of droplets
|
||||
for loop.Scan() {
|
||||
d := loop.Droplet()
|
||||
d := loop.Next()
|
||||
d.Current = nil
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ func backupDir(srcDir string, destDir string) error {
|
|||
// Create the destination directory
|
||||
err := os.MkdirAll(destDir, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Println("Failed to create directory: %v", err)
|
||||
log.Printf("Failed to create directory: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ func backupDir(srcDir string, destDir string) error {
|
|||
})
|
||||
|
||||
if err != nil {
|
||||
log.Println("Failed to copy files: %v", err)
|
||||
log.Printf("Failed to copy files: %v\n", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -34,7 +34,6 @@ func convertToAnypb(x any) *anypb.Any {
|
|||
return a
|
||||
default:
|
||||
log.Error(errors.New("convertToAnypb() unknown type"), "v =", v, "x =", x)
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -65,7 +64,6 @@ func convertToString(x any) string {
|
|||
default:
|
||||
log.Info("convertToSTring() unknown type", v)
|
||||
log.Error(errors.New("convertToSTring() unknown type"), "v =", v, "x =", x)
|
||||
return ""
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
|
17
config.go
17
config.go
|
@ -25,12 +25,10 @@ func (c *Cluster) ConfigSave() error {
|
|||
// make a new droplets struct
|
||||
var dcopy *Droplets
|
||||
dcopy = new(Droplets)
|
||||
loop := c.DropletsAll() // get the list of droplets
|
||||
loop := c.d.All() // get the list of droplets
|
||||
for loop.Scan() {
|
||||
d := loop.Droplet()
|
||||
var newd Droplet
|
||||
newd = *d
|
||||
dcopy.Droplets = append(dcopy.Droplets, &newd)
|
||||
d := loop.Next()
|
||||
dcopy.Droplets = append(dcopy.Droplets, d)
|
||||
}
|
||||
// delete all the Current data so it's not put in the config file
|
||||
for _, drop := range dcopy.Droplets {
|
||||
|
@ -184,13 +182,10 @@ func (c *Cluster) configWriteDroplets() error {
|
|||
fmt.Println("open config file :", err)
|
||||
return err
|
||||
}
|
||||
loop := c.DropletsAll() // get the list of droplets
|
||||
loop := c.d.All() // get the list of droplets
|
||||
for loop.Scan() {
|
||||
d := loop.Droplet()
|
||||
var newd Droplet
|
||||
newd = *d
|
||||
newd.Current = nil
|
||||
text := prototext.Format(&newd)
|
||||
d := loop.Next()
|
||||
text := prototext.Format(d)
|
||||
fmt.Fprintln(cfgfile, text)
|
||||
}
|
||||
return nil
|
||||
|
|
|
@ -4,15 +4,15 @@ package virtbuf;
|
|||
import "google/protobuf/duration.proto"; // Import the well-known type for Timestamp
|
||||
import "google/protobuf/timestamp.proto"; // Import the well-known type for Timestamp
|
||||
|
||||
message Droplets {
|
||||
message Droplets { // `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 Droplet droplets = 3;
|
||||
}
|
||||
|
||||
message Droplet {
|
||||
string uuid = 1; // should be unique across the cluster
|
||||
string hostname = 2; // should be unique and work in DNS
|
||||
message Droplet { // `autogenpb:marshal`
|
||||
string uuid = 1; // `autogenpb:unique` // should be unique across the cluster
|
||||
string hostname = 2; // `autogenpb:unique` // should be unique and work in DNS
|
||||
int64 cpus = 3; // what's the point of int64 vs int32
|
||||
int64 memory = 4; // in bytes
|
||||
Current current = 5; // what the state and values of the droplet is
|
||||
|
|
106
droplets.go
106
droplets.go
|
@ -1,106 +0,0 @@
|
|||
package virtbuf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
type DropletIterator struct {
|
||||
droplets []*Droplet
|
||||
index int
|
||||
}
|
||||
|
||||
// NewDropletIterator initializes a new iterator.
|
||||
func NewDropletIterator(droplets []*Droplet) *DropletIterator {
|
||||
return &DropletIterator{droplets: droplets}
|
||||
}
|
||||
|
||||
// Scan moves to the next element and returns false if there are no more droplets.
|
||||
func (it *DropletIterator) Scan() bool {
|
||||
if it.index >= len(it.droplets) {
|
||||
return false
|
||||
}
|
||||
it.index++
|
||||
return true
|
||||
}
|
||||
|
||||
// Droplet returns the current droplet.
|
||||
func (it *DropletIterator) Droplet() *Droplet {
|
||||
if it.droplets[it.index-1] == nil {
|
||||
for i, d := range it.droplets {
|
||||
fmt.Println("i =", i, d)
|
||||
}
|
||||
fmt.Println("len =", len(it.droplets))
|
||||
fmt.Println("droplet == nil", it.index, it.index-1)
|
||||
os.Exit(-1)
|
||||
}
|
||||
return it.droplets[it.index-1]
|
||||
}
|
||||
|
||||
// Use Scan() in a loop, similar to a while loop
|
||||
//
|
||||
// for iterator.Scan() {
|
||||
// d := iterator.Droplet()
|
||||
// fmt.Println("Droplet UUID:", d.Uuid)
|
||||
// }
|
||||
func (c *Cluster) GetDropletIterator() *DropletIterator {
|
||||
dropletPointers := c.SelectDropletPointers()
|
||||
|
||||
iterator := NewDropletIterator(dropletPointers)
|
||||
|
||||
return iterator
|
||||
}
|
||||
|
||||
func (c *Cluster) DropletsAll() *DropletIterator {
|
||||
dropletPointers := c.SelectDropletAll()
|
||||
|
||||
iterator := NewDropletIterator(dropletPointers)
|
||||
|
||||
return iterator
|
||||
}
|
||||
|
||||
// SelectDropletPointers safely returns a slice of pointers to Droplet records.
|
||||
func (c *Cluster) SelectDropletAll() []*Droplet {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
// Create a new slice to hold pointers to each Droplet
|
||||
// dropletPointers := make([]*Droplet, len(c.E.Droplets))
|
||||
var dropletPointers []*Droplet
|
||||
if c.d == nil {
|
||||
log.Info("SelectDropletsAll() c.d == nil")
|
||||
// os.Exit(-1)
|
||||
}
|
||||
for _, d := range c.d.Droplets {
|
||||
if d == nil {
|
||||
continue
|
||||
}
|
||||
if d.Archive != nil {
|
||||
continue
|
||||
}
|
||||
dropletPointers = append(dropletPointers, d) // Copy pointers for safe iteration
|
||||
}
|
||||
|
||||
return dropletPointers
|
||||
}
|
||||
|
||||
// SelectDropletPointers safely returns a slice of pointers to Droplet records.
|
||||
func (c *Cluster) SelectDropletPointers() []*Droplet {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
|
||||
// Create a new slice to hold pointers to each Droplet
|
||||
// dropletPointers := make([]*Droplet, len(c.E.Droplets))
|
||||
dropletPointers := make([]*Droplet, 1)
|
||||
if c.d == nil {
|
||||
log.Info("c.d == nil")
|
||||
os.Exit(-1)
|
||||
}
|
||||
for _, d := range c.d.Droplets {
|
||||
dropletPointers = append(dropletPointers, d) // Copy pointers for safe iteration
|
||||
}
|
||||
|
||||
return dropletPointers
|
||||
}
|
68
helpers.go
68
helpers.go
|
@ -3,12 +3,6 @@ package virtbuf
|
|||
// 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"
|
||||
)
|
||||
|
||||
func InitCluster() *Cluster {
|
||||
var c *Cluster
|
||||
c = new(Cluster)
|
||||
|
@ -18,64 +12,6 @@ func InitCluster() *Cluster {
|
|||
return c
|
||||
}
|
||||
|
||||
// human readable JSON
|
||||
func (d *Droplets) FormatJSON() string {
|
||||
return protojson.Format(d)
|
||||
}
|
||||
|
||||
func (d *Droplet) FormatJSON() string {
|
||||
return protojson.Format(d)
|
||||
}
|
||||
|
||||
func (e *Events) FormatJSON() string {
|
||||
return protojson.Format(e)
|
||||
}
|
||||
|
||||
func (h *Hypervisors) FormatJSON() string {
|
||||
return protojson.Format(h)
|
||||
}
|
||||
|
||||
// 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 (d *Droplets) FormatTEXT() string {
|
||||
return prototext.Format(d)
|
||||
}
|
||||
|
||||
func (d *Droplet) FormatTEXT() string {
|
||||
return prototext.Format(d)
|
||||
}
|
||||
|
||||
func (e *Events) FormatTEXT() string {
|
||||
return prototext.Format(e)
|
||||
}
|
||||
|
||||
// marshal
|
||||
func (d *Droplets) MarshalJSON() ([]byte, error) {
|
||||
return protojson.Marshal(d)
|
||||
}
|
||||
|
||||
func (d *Droplet) MarshalJSON() ([]byte, error) {
|
||||
return protojson.Marshal(d)
|
||||
}
|
||||
|
||||
func (e *Events) MarshalJSON() ([]byte, error) {
|
||||
return protojson.Marshal(e)
|
||||
}
|
||||
|
||||
// unmarshal
|
||||
func (d *Droplets) UnmarshalJSON(data []byte) error {
|
||||
return protojson.Unmarshal(data, d)
|
||||
}
|
||||
|
||||
func (d *Droplet) UnmarshalJSON(data []byte) error {
|
||||
return protojson.Unmarshal(data, d)
|
||||
}
|
||||
|
||||
func (e *Events) UnmarshalJSON(data []byte) error {
|
||||
return protojson.Unmarshal(data, e)
|
||||
}
|
||||
|
||||
func (d *Droplet) Unmarshal(data []byte) error {
|
||||
return proto.Unmarshal(data, d)
|
||||
func (c *Cluster) DropletsAll() *DropletIterator {
|
||||
return c.d.All()
|
||||
}
|
||||
|
|
2
human.go
2
human.go
|
@ -97,7 +97,7 @@ func FormatDuration(d time.Duration) string {
|
|||
ms := int(d.Milliseconds())
|
||||
if ms > 100 {
|
||||
// todo: print .3s, etc ?
|
||||
return fmt.Sprintf("%1.2fs", seconds/1000)
|
||||
return fmt.Sprintf("%1.2fs", float64(seconds)/1000)
|
||||
}
|
||||
if ms > 0 {
|
||||
result += fmt.Sprintf("%dms", ms)
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
package virtbuf
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
/*
|
||||
// MarshalJSON custom marshals the WhatInfo struct to JSON
|
||||
func (s WhatInfo) MarshalJSON() ([]byte, error) {
|
||||
capacityStr := fmt.Sprintf("%d GB", s.Capacity)
|
||||
|
@ -37,7 +32,6 @@ func (s *WhatInfo) UnmarshalJSON(data []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
func main() {
|
||||
info := WhatInfo{Capacity: 64}
|
||||
|
||||
|
|
Loading…
Reference in New Issue