1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
package virtbuf
// functions to import and export the protobuf
// data to and from config files
import (
"fmt"
"os"
"path/filepath"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
)
func WriteConfig(d *Droplets, h *Hypervisors, e *Events) bool {
if !d.WriteConfigJSON() {
return false
}
if !d.WriteConfigTEXT() {
return false
}
if e.WriteConfigJSON() {
return false
}
if e.WriteConfigTEXT() {
return false
}
return true
}
// read in events.json
func ReadEventsConfig() (*Events, error) {
e := new(Events)
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.json")
data, err := os.ReadFile(fullname)
if err != nil {
// log.Info("open config file :", err)
return nil, err
}
err = e.UnmarshalJSON(data)
if err != nil {
// log.Info("read json failed", err)
return nil, err
}
return e, nil
}
// export as json
func (e *Events) WriteConfigJSON() bool {
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.json")
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
defer cfgfile.Close()
if err != nil {
fmt.Println("open config file :", err)
return false
}
text := e.FormatJSON()
fmt.Fprintln(cfgfile, text)
fmt.Println("Write:", fullname, "OK")
return true
}
// export as prototext
func (e *Events) WriteConfigTEXT() bool {
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "events.text")
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
defer cfgfile.Close()
if err != nil {
fmt.Println("open config file :", err)
return false
}
text := e.FormatTEXT()
fmt.Fprintln(cfgfile, text)
fmt.Println("Write:", fullname, "OK")
return true
}
// export as json
func (d *Droplets) WriteConfigJSON() bool {
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.json")
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
defer cfgfile.Close()
if err != nil {
fmt.Println("open config file :", err)
return false
}
text := d.FormatJSON()
fmt.Fprintln(cfgfile, text)
fmt.Println("Write:", fullname, "OK")
return true
}
// export as prototext
func (d *Droplets) WriteConfigTEXT() bool {
fullname := filepath.Join(os.Getenv("VIRTIGO_HOME"), "droplets.text")
cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666)
defer cfgfile.Close()
if err != nil {
fmt.Println("open config file :", err)
return false
}
text := d.FormatTEXT()
fmt.Fprintln(cfgfile, text)
fmt.Println("Write:", fullname, "OK")
return true
}
// human readable JSON
func (c *Cluster) FormatJSON() string {
return protojson.Format(c)
}
func (d *Droplets) FormatJSON() string {
return protojson.Format(d)
}
func (e *Events) FormatJSON() string {
return protojson.Format(e)
}
// 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 (c *Cluster) FormatTEXT() string {
return prototext.Format(c)
}
func (d *Droplets) FormatTEXT() string {
return prototext.Format(d)
}
func (e *Events) FormatTEXT() string {
return prototext.Format(e)
}
// marshal
func (c *Cluster) MarshalJSON() ([]byte, error) {
return protojson.Marshal(c)
}
func (d *Droplets) MarshalJSON() ([]byte, error) {
return protojson.Marshal(d)
}
func (e *Events) MarshalJSON() ([]byte, error) {
return protojson.Marshal(e)
}
// unmarshal
func (c *Cluster) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, c)
}
func (d *Droplets) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, d)
}
func (e *Events) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, e)
}
|