Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 4 | "fmt" |
| 5 | "io" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 6 | "os" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 7 | "strings" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 8 | ) |
| 9 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 10 | const networkTopologyDir string = "topo-network/" |
| 11 | const containerTopologyDir string = "topo-containers/" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 12 | |
| 13 | type Stanza struct { |
| 14 | content string |
| 15 | pad int |
| 16 | } |
| 17 | |
| 18 | type ActionResult struct { |
| 19 | Err error |
| 20 | Desc string |
| 21 | ErrOutput string |
| 22 | StdOutput string |
| 23 | } |
| 24 | |
| 25 | type JsonResult struct { |
| 26 | Code int |
| 27 | Desc string |
| 28 | ErrOutput string |
| 29 | StdOutput string |
| 30 | } |
| 31 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 32 | func 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 Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 49 | func (c *Stanza) newStanza(name string) *Stanza { |
| 50 | c.append("\n" + name + " {") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 51 | c.pad += 2 |
| 52 | return c |
| 53 | } |
| 54 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 55 | func (c *Stanza) append(name string) *Stanza { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 56 | c.content += strings.Repeat(" ", c.pad) |
| 57 | c.content += name + "\n" |
| 58 | return c |
| 59 | } |
| 60 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 61 | func (c *Stanza) close() *Stanza { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 62 | c.content += "}\n" |
| 63 | c.pad -= 2 |
| 64 | return c |
| 65 | } |
| 66 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 67 | func (s *Stanza) toString() string { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 68 | return s.content |
| 69 | } |
| 70 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 71 | func (s *Stanza) saveToFile(fileName string) error { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 72 | 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 | } |