diff options
| author | Jeff Carr <[email protected]> | 2024-10-24 16:57:50 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2024-10-24 16:57:50 -0500 |
| commit | 87b7bc17b313c04806981408943923d8e66f960a (patch) | |
| tree | 2124e618a2dd06fc0aefa6b4a3a1d449e504bf8a /example/main.go | |
| parent | dac27e31b5cdf7f9aa382cd342dcfa79423a0f69 (diff) | |
seperate config files for droplets, hypervisors & events
Signed-off-by: Jeff Carr <[email protected]>
Diffstat (limited to 'example/main.go')
| -rw-r--r-- | example/main.go | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..d54803d --- /dev/null +++ b/example/main.go @@ -0,0 +1,98 @@ +package main + +import ( + "bufio" + "bytes" + "fmt" + "io/ioutil" + "log" + "os" + + "google.golang.org/protobuf/proto" + + 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.String()) + // show the droplets to STDOUT + for _, d := range aCluster.Droplets { + fmt.Println("\tdroplet =", d.Hostname, "preffered host:", d.PreferredHypervisor) + } + + // show the hypervisors to STDOUT + for _, h := range aCluster.Hypervisors { + fmt.Println("\thypervisor =", h.Hostname, h.GetMemoryPrintable()) + } + + json := aCluster.FormatJSON() + fmt.Println(json) + + data, _ := aCluster.MarshalJSON() + fmt.Println(string(data)) + + text := aCluster.FormatTEXT() + fmt.Println(text) +} + +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") + } +} |
