2024-10-18 20:58:15 -05:00
|
|
|
package main
|
|
|
|
|
2024-10-22 06:19:24 -05:00
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2024-10-18 20:58:15 -05:00
|
|
|
|
2024-10-22 06:19:24 -05:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
|
|
|
|
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-22 03:13:12 -05:00
|
|
|
TestWriteCluster()
|
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 06:19:24 -05:00
|
|
|
var aCluster pb.Cluster
|
2024-10-22 02:51:45 -05:00
|
|
|
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 04:18:40 -05:00
|
|
|
log.Println(aCluster.String())
|
2024-10-22 03:50:01 -05:00
|
|
|
// show the droplets to STDOUT
|
|
|
|
for _, d := range aCluster.Droplets {
|
2024-10-22 06:19:24 -05:00
|
|
|
fmt.Println("\tdroplet =", d.Hostname, "preffered host:", d.PreferredHypervisor)
|
2024-10-22 03:50:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// show the hypervisors to STDOUT
|
|
|
|
for _, h := range aCluster.Hypervisors {
|
2024-10-22 06:19:24 -05:00
|
|
|
fmt.Println("\thypervisor =", h.Hostname, h.GetMemoryPrintable())
|
2024-10-22 02:51:45 -05:00
|
|
|
}
|
2024-10-22 04:37:28 -05:00
|
|
|
|
2024-10-22 06:19:24 -05:00
|
|
|
json := aCluster.FormatJSON()
|
|
|
|
fmt.Println(json)
|
|
|
|
|
2024-10-23 00:20:22 -05:00
|
|
|
data, _ := aCluster.MarshalJSON()
|
|
|
|
fmt.Println(string(data))
|
|
|
|
|
2024-10-22 06:19:24 -05:00
|
|
|
text := aCluster.FormatTEXT()
|
|
|
|
fmt.Println(text)
|
2024-10-18 20:58:15 -05:00
|
|
|
}
|
|
|
|
|
2024-10-22 03:04:59 -05:00
|
|
|
func marshalWriteToFile(myWriter *bufio.Writer, c *pb.Cluster) {
|
|
|
|
buf, err := proto.Marshal(c)
|
2024-10-18 20:58:15 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("marshaling error: ", err)
|
|
|
|
}
|
|
|
|
tmp, err := myWriter.Write(buf)
|
|
|
|
myWriter.Flush()
|
|
|
|
log.Println("bufio.Write() tmp, err = ", tmp, err)
|
|
|
|
|
2024-10-22 03:04:59 -05:00
|
|
|
buf, err = proto.Marshal(c)
|
2024-10-18 20:58:15 -05:00
|
|
|
tmp2, err := myWriter.Write(buf)
|
|
|
|
myWriter.Flush()
|
|
|
|
log.Println("bufio.Write() tmp2, err = ", tmp2, err)
|
|
|
|
}
|
|
|
|
|
2024-10-22 03:13:12 -05:00
|
|
|
func TestWriteCluster() {
|
2024-10-18 20:58:15 -05:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
2024-10-22 03:50:01 -05:00
|
|
|
c := pb.CreateSampleCluster(7)
|
2024-10-18 20:58:15 -05:00
|
|
|
|
|
|
|
got := buf.String()
|
|
|
|
log.Println(got)
|
|
|
|
|
|
|
|
newfile, _ := os.Create("/tmp/testing4.protobuf")
|
|
|
|
myWriter := bufio.NewWriter(newfile)
|
2024-10-22 03:04:59 -05:00
|
|
|
marshalWriteToFile(myWriter, c)
|
2024-10-18 20:58:15 -05:00
|
|
|
|
2024-10-22 03:13:12 -05:00
|
|
|
// marshalUnmarshal()
|
2024-10-18 20:58:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func marshalUnmarshal() {
|
2024-10-22 03:04:59 -05:00
|
|
|
test := pb.CreateSampleCluster(7)
|
2024-10-18 20:58:15 -05:00
|
|
|
data, err := proto.Marshal(test)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("marshaling error: ", err)
|
|
|
|
}
|
|
|
|
|
2024-10-22 03:04:59 -05:00
|
|
|
newTest := &pb.Cluster{}
|
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")
|
|
|
|
}
|
|
|
|
}
|