hs-test: remove exec flags from source files
[vpp.git] / extras / hs-test / main.go
1 package main
2
3 import (
4         "context"
5         "encoding/json"
6         "fmt"
7         "os"
8         "os/exec"
9         "os/signal"
10         "reflect"
11 )
12
13 var actions Actions
14
15 func newVppContext() (context.Context, context.CancelFunc) {
16         ctx, cancel := signal.NotifyContext(
17                 context.Background(),
18                 os.Interrupt,
19         )
20         return ctx, cancel
21 }
22
23 func Vppcli(runDir, command string) (string, error) {
24         cmd := exec.Command("vppctl", "-s", fmt.Sprintf("%s/var/run/vpp/cli.sock", runDir), command)
25         o, err := cmd.CombinedOutput()
26         if err != nil {
27                 fmt.Printf("failed to execute command: '%v'.\n", err)
28         }
29         fmt.Printf("Command output %s", string(o))
30         return string(o), err
31 }
32
33 func exitOnErrCh(ctx context.Context, cancel context.CancelFunc, errCh <-chan error) {
34         // If we already have an error, log it and exit
35         select {
36         case err := <-errCh:
37                 fmt.Printf("%v", err)
38         default:
39         }
40         go func(ctx context.Context, errCh <-chan error) {
41                 <-errCh
42                 cancel()
43         }(ctx, errCh)
44 }
45
46 func writeSyncFile(res *ActionResult) error {
47         syncFile := "/tmp/sync/rc"
48
49         var jsonRes JsonResult
50
51         jsonRes.ErrOutput = res.ErrOutput
52         jsonRes.StdOutput = res.StdOutput
53         if res.Err != nil {
54                 jsonRes.Code = 1
55                 jsonRes.Desc = fmt.Sprintf("%s :%v", res.Desc, res.Err)
56         } else {
57                 jsonRes.Code = 0
58         }
59
60         str, err := json.Marshal(jsonRes)
61         if err != nil {
62                 return fmt.Errorf("error marshaling json result data! %v", err)
63         }
64
65         _, err = os.Open(syncFile)
66         if err != nil {
67                 // expecting the file does not exist
68                 f, e := os.Create(syncFile)
69                 if e != nil {
70                         return fmt.Errorf("failed to open sync file")
71                 }
72                 defer f.Close()
73                 f.Write([]byte(str))
74         } else {
75                 return fmt.Errorf("sync file exists, delete the file first")
76         }
77         return nil
78 }
79
80 func NewActionResult(err error, opts ...ActionResultOptionFn) *ActionResult {
81         res := &ActionResult{
82                 Err: err,
83         }
84         for _, o := range opts {
85                 o(res)
86         }
87         return res
88 }
89
90 type ActionResultOptionFn func(res *ActionResult)
91
92 func ActionResultWithDesc(s string) ActionResultOptionFn {
93         return func(res *ActionResult) {
94                 res.Desc = s
95         }
96 }
97
98 func ActionResultWithStderr(s string) ActionResultOptionFn {
99         return func(res *ActionResult) {
100                 res.ErrOutput = s
101         }
102 }
103
104 func ActionResultWithStdout(s string) ActionResultOptionFn {
105         return func(res *ActionResult) {
106                 res.StdOutput = s
107         }
108 }
109
110 func OkResult() *ActionResult {
111         return NewActionResult(nil)
112 }
113
114 func processArgs() *ActionResult {
115         nArgs := len(os.Args) - 1 // skip program name
116         if nArgs < 1 {
117                 return NewActionResult(fmt.Errorf("internal: no action specified!"))
118         }
119         action := os.Args[1]
120         methodValue := reflect.ValueOf(&actions).MethodByName(action)
121         if !methodValue.IsValid() {
122                 return NewActionResult(fmt.Errorf("internal unknown action %s!", action))
123         }
124         methodIface := methodValue.Interface()
125         fn := methodIface.(func([]string) *ActionResult)
126         return fn(os.Args)
127 }
128
129 func main() {
130         if len(os.Args) == 0 {
131                 fmt.Println("args required")
132                 return
133         }
134
135         if os.Args[1] == "rm" {
136                 topology, err := LoadTopology(NetworkTopologyDir, os.Args[2])
137                 if err != nil {
138                         fmt.Printf("falied to load topologies: %v\n", err)
139                         os.Exit(1)
140                 }
141                 topology.Unconfigure()
142                 os.Exit(0)
143         }
144
145         var err error
146         res := processArgs()
147         err = writeSyncFile(res)
148         if err != nil {
149                 fmt.Printf("failed to write to sync file: %v\n", err)
150         }
151 }