8 "github.com/iotbzh/xds-agent/lib/apiv1"
9 "github.com/urfave/cli"
12 func initCmdExec(cmdDef *[]cli.Command) {
13 *cmdDef = append(*cmdDef, cli.Command{
15 Usage: "execute a command in XDS",
20 EnvVar: "XDS_PROJECT_ID",
21 Usage: "project ID you want to build (mandatory variable)",
26 Usage: "relative path into project",
31 Usage: "Cross Sdk ID to use to build project",
37 func exec(ctx *cli.Context) error {
38 prjID := ctx.String("id")
39 rPath := ctx.String("rPath")
40 sdkid := ctx.String("sdkid")
42 // Check mandatory args
44 return cli.NewExitError("project id must be set (see --id option)", 1)
47 argsCommand := make([]string, len(ctx.Args()))
48 copy(argsCommand, ctx.Args())
49 Log.Infof("Execute: /exec %v", argsCommand)
51 // Log useful info for debugging
52 ver := apiv1.XDSVersion{}
54 Log.Infof("XDS version: %v", ver)
56 // Process Socket IO events
57 type exitResult struct {
61 exitChan := make(chan exitResult, 1)
63 IOsk.On("disconnection", func(err error) {
64 exitChan <- exitResult{err, 2}
67 outFunc := func(timestamp, stdout, stderr string) {
69 if ctx.Bool("WithTimestamp") {
73 fmt.Printf("%s%s", tm, stdout)
76 fmt.Fprintf(os.Stderr, "%s%s", tm, stderr)
80 IOsk.On(apiv1.ExecOutEvent, func(ev apiv1.ExecOutMsg) {
81 outFunc(ev.Timestamp, ev.Stdout, ev.Stderr)
84 IOsk.On(apiv1.ExecExitEvent, func(ev apiv1.ExecExitMsg) {
85 exitChan <- exitResult{ev.Error, ev.Code}
88 // Retrieve the project definition
89 prj := apiv1.ProjectConfig{}
90 if err := HTTPCli.Get("/projects/"+prjID, &prj); err != nil {
91 return cli.NewExitError(err, 1)
94 // Auto setup rPath if needed
96 cwd, err := os.Getwd()
98 fldRp := prj.ClientPath
99 if !strings.HasPrefix(fldRp, "/") {
102 Log.Debugf("Try to auto-setup rPath: cwd=%s ; ClientPath=%s", cwd, fldRp)
103 if sp := strings.SplitAfter(cwd, fldRp); len(sp) == 2 {
104 rPath = strings.Trim(sp[1], "/")
105 Log.Debugf("Auto-setup rPath to: '%s'", rPath)
111 Log.Debugf("Command env: %v", EnvConfFileMap)
113 for k, v := range EnvConfFileMap {
114 env = append(env, k+"="+v)
117 // Send build command
118 args := apiv1.ExecArgs{
121 Cmd: strings.Trim(argsCommand[0], " "),
122 Args: argsCommand[1:],
128 LogPost("POST /exec %v", args)
129 if err := HTTPCli.Post("/exec", args, nil); err != nil {
130 return cli.NewExitError(err.Error(), 1)
135 case res := <-exitChan:
138 Log.Debugln("Exit successfully")
140 if res.error != nil {
141 Log.Debugln("Exit with ERROR: ", res.error.Error())
142 errStr = res.error.Error()
144 return cli.NewExitError(errStr, res.code)