86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
package main
|
|
|
|
import "log"
|
|
import "bytes"
|
|
import "os"
|
|
import "bufio"
|
|
import "io/ioutil"
|
|
|
|
import "google.golang.org/protobuf/proto"
|
|
import pb "go.wit.com/lib/protobuf/virtbuf"
|
|
|
|
//
|
|
// saves entries in a config file
|
|
//
|
|
|
|
func main() {
|
|
TestWriteCluster()
|
|
|
|
in, err := ioutil.ReadFile("/tmp/testing4.protobuf")
|
|
if err != nil {
|
|
log.Fatalln("Error reading file:", err)
|
|
}
|
|
|
|
var aCluster pb.Cluster
|
|
if err := proto.Unmarshal(in, &aCluster); err != nil {
|
|
log.Fatalln("Failed to parse droplet:", err)
|
|
}
|
|
|
|
// log.Println(aCluster)
|
|
// show the droplets to STDOUT
|
|
for _, d := range aCluster.Droplets {
|
|
log.Println("\tdroplet =", d.Hostname, "preffered host:", d.PreferredHypervisor)
|
|
}
|
|
|
|
// show the hypervisors to STDOUT
|
|
for _, h := range aCluster.Hypervisors {
|
|
log.Println("\thypervisor =", h.Hostname)
|
|
}
|
|
}
|
|
|
|
func marshalWriteToFile(myWriter *bufio.Writer, c *pb.Cluster) {
|
|
buf, err := proto.Marshal(c)
|
|
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(c)
|
|
tmp2, err := myWriter.Write(buf)
|
|
myWriter.Flush()
|
|
log.Println("bufio.Write() tmp2, err = ", tmp2, err)
|
|
}
|
|
|
|
func TestWriteCluster() {
|
|
buf := new(bytes.Buffer)
|
|
|
|
c := pb.CreateSampleCluster(7)
|
|
|
|
got := buf.String()
|
|
log.Println(got)
|
|
|
|
newfile, _ := os.Create("/tmp/testing4.protobuf")
|
|
myWriter := bufio.NewWriter(newfile)
|
|
marshalWriteToFile(myWriter, c)
|
|
|
|
// marshalUnmarshal()
|
|
}
|
|
|
|
func marshalUnmarshal() {
|
|
test := pb.CreateSampleCluster(7)
|
|
data, err := proto.Marshal(test)
|
|
if err != nil {
|
|
log.Fatal("marshaling error: ", err)
|
|
}
|
|
|
|
newTest := &pb.Cluster{}
|
|
err = proto.Unmarshal(data, newTest)
|
|
if err != nil {
|
|
log.Fatal("unmarshaling error: ", err)
|
|
} else {
|
|
log.Println("proto.Marshal() and proto.Unmarshal() worked")
|
|
}
|
|
}
|