From afd0bd642841f4dee9ca29c4913a26319aa28dd9 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 1 Dec 2024 18:41:00 -0600 Subject: [PATCH] switched to autogenpb --- Makefile | 42 ++++---------------- add.go | 8 ++-- backupDir.go | 4 +- change.go | 2 - config.go | 17 +++----- droplet.proto | 8 ++-- droplets.go | 106 ------------------------------------------------- helpers.go | 68 +------------------------------ human.go | 2 +- storageinfo.go | 8 +--- 10 files changed, 28 insertions(+), 237 deletions(-) delete mode 100644 droplets.go diff --git a/Makefile b/Makefile index 317ce8b..7540aeb 100644 --- a/Makefile +++ b/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 diff --git a/add.go b/add.go index d77077b..335e0a8 100644 --- a/add.go +++ b/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 } } diff --git a/backupDir.go b/backupDir.go index bd021fb..7a3341b 100644 --- a/backupDir.go +++ b/backupDir.go @@ -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 diff --git a/change.go b/change.go index bf0cfce..f068269 100644 --- a/change.go +++ b/change.go @@ -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 "" } diff --git a/config.go b/config.go index 0274fcf..082a2ab 100644 --- a/config.go +++ b/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 diff --git a/droplet.proto b/droplet.proto index dd1b30a..42eeec5 100644 --- a/droplet.proto +++ b/droplet.proto @@ -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 diff --git a/droplets.go b/droplets.go deleted file mode 100644 index 8408cef..0000000 --- a/droplets.go +++ /dev/null @@ -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 -} diff --git a/helpers.go b/helpers.go index 1533750..7d0173f 100644 --- a/helpers.go +++ b/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() } diff --git a/human.go b/human.go index 901c6b2..76a8704 100644 --- a/human.go +++ b/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) diff --git a/storageinfo.go b/storageinfo.go index ea08bc6..1e25870 100644 --- a/storageinfo.go +++ b/storageinfo.go @@ -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}