14 func newVppContext() (context.Context, context.CancelFunc) {
15 ctx, cancel := signal.NotifyContext(
22 func exitOnErrCh(ctx context.Context, cancel context.CancelFunc, errCh <-chan error) {
23 // If we already have an error, log it and exit
29 go func(ctx context.Context, errCh <-chan error) {
35 func writeSyncFile(res *ActionResult) error {
36 syncFile := "/tmp/sync/rc"
38 var jsonRes JsonResult
40 jsonRes.ErrOutput = res.ErrOutput
41 jsonRes.StdOutput = res.StdOutput
44 jsonRes.Desc = fmt.Sprintf("%s :%v", res.Desc, res.Err)
49 str, err := json.Marshal(jsonRes)
51 return fmt.Errorf("error marshaling json result data! %v", err)
54 _, err = os.Open(syncFile)
56 // expecting the file does not exist
57 f, e := os.Create(syncFile)
59 return fmt.Errorf("failed to open sync file")
64 return fmt.Errorf("sync file exists, delete the file first")
69 func NewActionResult(err error, opts ...ActionResultOptionFn) *ActionResult {
73 for _, o := range opts {
79 type ActionResultOptionFn func(res *ActionResult)
81 func ActionResultWithDesc(s string) ActionResultOptionFn {
82 return func(res *ActionResult) {
87 func ActionResultWithStderr(s string) ActionResultOptionFn {
88 return func(res *ActionResult) {
93 func ActionResultWithStdout(s string) ActionResultOptionFn {
94 return func(res *ActionResult) {
99 func OkResult() *ActionResult {
100 return NewActionResult(nil)
103 func processArgs() *ActionResult {
104 nArgs := len(os.Args) - 1 // skip program name
106 return NewActionResult(fmt.Errorf("internal: no action specified!"))
109 methodValue := reflect.ValueOf(&actions).MethodByName(action)
110 if !methodValue.IsValid() {
111 return NewActionResult(fmt.Errorf("internal unknown action %s!", action))
113 methodIface := methodValue.Interface()
114 fn := methodIface.(func([]string) *ActionResult)
119 if len(os.Args) == 0 {
120 fmt.Println("args required")
124 if os.Args[1] == "rm" {
125 topology, err := LoadTopology(NetworkTopologyDir, os.Args[2])
127 fmt.Printf("falied to load topologies: %v\n", err)
130 topology.Unconfigure()
136 err = writeSyncFile(res)
138 fmt.Printf("failed to write to sync file: %v\n", err)