2024-10-18 20:58:15 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "log"
|
|
|
|
import "bytes"
|
|
|
|
import "os"
|
|
|
|
import "bufio"
|
|
|
|
import "io/ioutil"
|
|
|
|
|
2024-10-21 18:28:59 -05:00
|
|
|
import "google.golang.org/protobuf/proto"
|
2024-10-21 17:53:36 -05:00
|
|
|
import pb "go.wit.com/lib/protobuf/virtbuf"
|
2024-10-18 20:58:15 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// saves entries in a config file
|
|
|
|
//
|
|
|
|
|
|
|
|
func main() {
|
2024-10-21 22:21:22 -05:00
|
|
|
TestWriteDroplet()
|
2024-10-18 20:58:15 -05:00
|
|
|
|
|
|
|
in, err := ioutil.ReadFile("/tmp/testing4.protobuf")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("Error reading file:", err)
|
|
|
|
}
|
2024-10-21 22:19:07 -05:00
|
|
|
|
2024-10-22 02:51:45 -05:00
|
|
|
var aCluster pb.Cluster
|
|
|
|
if err := proto.Unmarshal(in, &aCluster); err != nil {
|
2024-10-21 22:21:22 -05:00
|
|
|
log.Fatalln("Failed to parse droplet:", err)
|
2024-10-18 20:58:15 -05:00
|
|
|
}
|
|
|
|
|
2024-10-22 02:51:45 -05:00
|
|
|
log.Println(aCluster)
|
|
|
|
for i, d := range aCluster.Droplets {
|
|
|
|
log.Println("\tdrop =", i, d)
|
|
|
|
}
|
2024-10-18 20:58:15 -05:00
|
|
|
}
|
|
|
|
|
2024-10-22 02:51:45 -05:00
|
|
|
func marshalWriteToFile(myWriter *bufio.Writer, e *pb.Cluster) {
|
2024-10-18 20:58:15 -05:00
|
|
|
buf, err := proto.Marshal(e)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("marshaling error: ", err)
|
|
|
|
}
|
|
|
|
tmp, err := myWriter.Write(buf)
|
|
|
|
myWriter.Flush()
|
|
|
|
log.Println("bufio.Write() tmp, err = ", tmp, err)
|
|
|
|
|
|
|
|
buf, err = proto.Marshal(e)
|
|
|
|
tmp2, err := myWriter.Write(buf)
|
|
|
|
myWriter.Flush()
|
|
|
|
log.Println("bufio.Write() tmp2, err = ", tmp2, err)
|
|
|
|
}
|
|
|
|
|
2024-10-21 22:21:22 -05:00
|
|
|
func TestWriteDroplet() {
|
2024-10-18 20:58:15 -05:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
2024-10-22 02:51:45 -05:00
|
|
|
e := pb.CreateSampleCluster(10)
|
2024-10-18 20:58:15 -05:00
|
|
|
|
|
|
|
got := buf.String()
|
|
|
|
log.Println(got)
|
|
|
|
|
|
|
|
newfile, _ := os.Create("/tmp/testing4.protobuf")
|
|
|
|
myWriter := bufio.NewWriter(newfile)
|
|
|
|
marshalWriteToFile(myWriter, e)
|
|
|
|
|
|
|
|
marshalUnmarshal()
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalUnmarshal() {
|
2024-10-22 02:51:45 -05:00
|
|
|
test := pb.CreateSampleCluster(10)
|
2024-10-18 20:58:15 -05:00
|
|
|
data, err := proto.Marshal(test)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("marshaling error: ", err)
|
|
|
|
}
|
|
|
|
|
2024-10-21 17:53:36 -05:00
|
|
|
newTest := &pb.Droplet{}
|
2024-10-18 20:58:15 -05:00
|
|
|
err = proto.Unmarshal(data, newTest)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("unmarshaling error: ", err)
|
|
|
|
} else {
|
|
|
|
log.Println("proto.Marshal() and proto.Unmarshal() worked")
|
|
|
|
}
|
|
|
|
}
|