blob: 304dd4c241bb2fe2583123715d163a69aa1a0cbc [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
Filip Tehlar229f5fc2022-08-09 14:44:47 +00004 "fmt"
5 "io"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00006 "os"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00007 "strings"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00008)
9
Maros Ondrejickae7625d02023-02-28 16:55:01 +010010const networkTopologyDir string = "topo-network/"
11const containerTopologyDir string = "topo-containers/"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000012
13type Stanza struct {
14 content string
15 pad int
16}
17
18type ActionResult struct {
19 Err error
20 Desc string
21 ErrOutput string
22 StdOutput string
23}
24
25type JsonResult struct {
26 Code int
27 Desc string
28 ErrOutput string
29 StdOutput string
30}
31
Filip Tehlar229f5fc2022-08-09 14:44:47 +000032func assertFileSize(f1, f2 string) error {
33 fi1, err := os.Stat(f1)
34 if err != nil {
35 return err
36 }
37
38 fi2, err1 := os.Stat(f2)
39 if err1 != nil {
40 return err1
41 }
42
43 if fi1.Size() != fi2.Size() {
44 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
45 }
46 return nil
47}
48
Maros Ondrejickae7625d02023-02-28 16:55:01 +010049func (c *Stanza) newStanza(name string) *Stanza {
50 c.append("\n" + name + " {")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000051 c.pad += 2
52 return c
53}
54
Maros Ondrejickae7625d02023-02-28 16:55:01 +010055func (c *Stanza) append(name string) *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000056 c.content += strings.Repeat(" ", c.pad)
57 c.content += name + "\n"
58 return c
59}
60
Maros Ondrejickae7625d02023-02-28 16:55:01 +010061func (c *Stanza) close() *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000062 c.content += "}\n"
63 c.pad -= 2
64 return c
65}
66
Maros Ondrejickae7625d02023-02-28 16:55:01 +010067func (s *Stanza) toString() string {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000068 return s.content
69}
70
Maros Ondrejickae7625d02023-02-28 16:55:01 +010071func (s *Stanza) saveToFile(fileName string) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000072 fo, err := os.Create(fileName)
73 if err != nil {
74 return err
75 }
76 defer fo.Close()
77
78 _, err = io.Copy(fo, strings.NewReader(s.content))
79 return err
80}