15 func newVppContext() (context.Context, context.CancelFunc) {
16 ctx, cancel := signal.NotifyContext(
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()
27 fmt.Printf("failed to execute command: '%v'.\n", err)
29 fmt.Printf("Command output %s", string(o))
33 func exitOnErrCh(ctx context.Context, cancel context.CancelFunc, errCh <-chan error) {
34 // If we already have an error, log it and exit
40 go func(ctx context.Context, errCh <-chan error) {
46 func writeSyncFile(res *ActionResult) error {
47 syncFile := "/tmp/sync/rc"
49 var jsonRes JsonResult
51 jsonRes.ErrOutput = res.ErrOutput
52 jsonRes.StdOutput = res.StdOutput
55 jsonRes.Desc = fmt.Sprintf("%s :%v", res.Desc, res.Err)
60 str, err := json.Marshal(jsonRes)
62 return fmt.Errorf("error marshaling json result data! %v", err)
65 _, err = os.Open(syncFile)
67 // expecting the file does not exist
68 f, e := os.Create(syncFile)
70 return fmt.Errorf("failed to open sync file")
75 return fmt.Errorf("sync file exists, delete the file first")
80 func NewActionResult(err error, opts ...ActionResultOptionFn) *ActionResult {
84 for _, o := range opts {
90 type ActionResultOptionFn func(res *ActionResult)
92 func ActionResultWithDesc(s string) ActionResultOptionFn {
93 return func(res *ActionResult) {
98 func ActionResultWithStderr(s string) ActionResultOptionFn {
99 return func(res *ActionResult) {
104 func ActionResultWithStdout(s string) ActionResultOptionFn {
105 return func(res *ActionResult) {
110 func OkResult() *ActionResult {
111 return NewActionResult(nil)
114 func processArgs() *ActionResult {
115 nArgs := len(os.Args) - 1 // skip program name
117 return NewActionResult(fmt.Errorf("internal: no action specified!"))
120 methodValue := reflect.ValueOf(&actions).MethodByName(action)
121 if !methodValue.IsValid() {
122 return NewActionResult(fmt.Errorf("internal unknown action %s!", action))
124 methodIface := methodValue.Interface()
125 fn := methodIface.(func([]string) *ActionResult)
130 if len(os.Args) == 0 {
131 fmt.Println("args required")
135 if os.Args[1] == "rm" {
136 topology, err := LoadTopology(TopologyDir, os.Args[2])
138 fmt.Printf("falied to load topologies: %v\n", err)
141 topology.Unconfigure()
147 err = writeSyncFile(res)
149 fmt.Printf("failed to write to sync file: %v\n", err)