11 "git.fd.io/govpp.git/api"
14 type CfgTable map[string]func([]string) *ActionResult
18 type ConfFn func(context.Context, api.Connection) error
20 func newVppContext() (context.Context, context.CancelFunc) {
21 ctx, cancel := signal.NotifyContext(
28 func Vppcli(runDir, command string) (string, error) {
29 cmd := exec.Command("vppctl", "-s", fmt.Sprintf("%s/var/run/vpp/cli.sock", runDir), command)
30 o, err := cmd.CombinedOutput()
32 fmt.Printf("failed to execute command: '%v'.\n", err)
34 fmt.Printf("Command output %s", string(o))
38 func exitOnErrCh(ctx context.Context, cancel context.CancelFunc, errCh <-chan error) {
39 // If we already have an error, log it and exit
45 go func(ctx context.Context, errCh <-chan error) {
51 func writeSyncFile(res *ActionResult) error {
52 syncFile := "/tmp/sync/rc"
54 var jsonRes JsonResult
56 jsonRes.ErrOutput = res.ErrOutput
57 jsonRes.StdOutput = res.StdOutput
60 jsonRes.Desc = fmt.Sprintf("%s :%v", res.Desc, res.Err)
65 str, err := json.Marshal(jsonRes)
67 return fmt.Errorf("error marshaling json result data! %v", err)
70 _, err = os.Open(syncFile)
72 // expecting the file does not exist
73 f, e := os.Create(syncFile)
75 return fmt.Errorf("failed to open sync file")
80 return fmt.Errorf("sync file exists, delete the file first")
85 func NewActionResult(err error, opts ...ActionResultOptionFn) *ActionResult {
89 for _, o := range opts {
95 type ActionResultOptionFn func(res *ActionResult)
97 func ActionResultWithDesc(s string) ActionResultOptionFn {
98 return func(res *ActionResult) {
103 func ActionResultWithStderr(s string) ActionResultOptionFn {
104 return func(res *ActionResult) {
109 func ActionResultWithStdout(s string) ActionResultOptionFn {
110 return func(res *ActionResult) {
115 func OkResult() *ActionResult {
116 return NewActionResult(nil)
119 func reg(key string, fn func([]string) *ActionResult) {
123 func processArgs() *ActionResult {
124 fn := cfgTable[os.Args[1]]
126 return NewActionResult(fmt.Errorf("internal: no config found for %s", os.Args[1]))
132 if len(os.Args) == 0 {
133 fmt.Println("args required")
137 if os.Args[1] == "rm" {
138 topology, err := LoadTopology(TopologyDir, os.Args[2])
140 fmt.Printf("falied to load topologies: %v\n", err)
143 topology.Unconfigure()
151 err = writeSyncFile(res)
153 fmt.Printf("failed to write to sync file: %v\n", err)