blob: d250dc6451968c2d82905aacaafb14a4238f716c [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"
Matus Fabian5409d332024-05-28 13:39:13 +02006 "net/http"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00007 "os"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00008 "strings"
Matus Fabian5409d332024-05-28 13:39:13 +02009 "time"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000010)
11
Maros Ondrejickae7625d02023-02-28 16:55:01 +010012const networkTopologyDir string = "topo-network/"
13const containerTopologyDir string = "topo-containers/"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000014
15type Stanza struct {
16 content string
17 pad int
18}
19
20type ActionResult struct {
21 Err error
22 Desc string
23 ErrOutput string
24 StdOutput string
25}
26
27type JsonResult struct {
28 Code int
29 Desc string
30 ErrOutput string
31 StdOutput string
32}
33
Filip Tehlar229f5fc2022-08-09 14:44:47 +000034func assertFileSize(f1, f2 string) error {
35 fi1, err := os.Stat(f1)
36 if err != nil {
37 return err
38 }
39
40 fi2, err1 := os.Stat(f2)
41 if err1 != nil {
42 return err1
43 }
44
45 if fi1.Size() != fi2.Size() {
46 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
47 }
48 return nil
49}
50
Maros Ondrejickae7625d02023-02-28 16:55:01 +010051func (c *Stanza) newStanza(name string) *Stanza {
52 c.append("\n" + name + " {")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000053 c.pad += 2
54 return c
55}
56
Maros Ondrejickae7625d02023-02-28 16:55:01 +010057func (c *Stanza) append(name string) *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000058 c.content += strings.Repeat(" ", c.pad)
59 c.content += name + "\n"
60 return c
61}
62
Maros Ondrejickae7625d02023-02-28 16:55:01 +010063func (c *Stanza) close() *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000064 c.content += "}\n"
65 c.pad -= 2
66 return c
67}
68
Maros Ondrejickae7625d02023-02-28 16:55:01 +010069func (s *Stanza) toString() string {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000070 return s.content
71}
72
Maros Ondrejickae7625d02023-02-28 16:55:01 +010073func (s *Stanza) saveToFile(fileName string) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000074 fo, err := os.Create(fileName)
75 if err != nil {
76 return err
77 }
78 defer fo.Close()
79
80 _, err = io.Copy(fo, strings.NewReader(s.content))
81 return err
82}
Matus Fabian5409d332024-05-28 13:39:13 +020083
84// newHttpClient creates [http.Client] with disabled proxy and redirects, it also sets timeout to 30seconds.
85func newHttpClient() *http.Client {
86 transport := http.DefaultTransport
87 transport.(*http.Transport).Proxy = nil
88 transport.(*http.Transport).DisableKeepAlives = true
89 client := &http.Client{
90 Transport: transport,
91 Timeout: time.Second * 30,
92 CheckRedirect: func(req *http.Request, via []*http.Request) error {
93 return http.ErrUseLastResponse
94 }}
95 return client
96}