11 "github.com/gin-gonic/gin"
12 common "github.com/iotbzh/xds-common/golib"
15 // MakeArgs is the parameters (json format) of /make command
16 type MakeArgs struct {
18 SdkID string `json:"sdkid"` // sdk ID to use for setting env
19 Args []string `json:"args"` // args to pass to make command
20 Env []string `json:"env"`
21 RPath string `json:"rpath"` // relative path into project
22 ExitImmediate bool `json:"exitImmediate"` // when true, exit event sent immediately when command exited (IOW, don't wait file synchronization)
23 CmdTimeout int `json:"timeout"` // command completion timeout in Second
26 // MakeOutMsg Message send on each output (stdout+stderr) of make command
27 type MakeOutMsg struct {
28 CmdID string `json:"cmdID"`
29 Timestamp string `json:"timestamp"`
30 Stdout string `json:"stdout"`
31 Stderr string `json:"stderr"`
34 // MakeExitMsg Message send on make command exit
35 type MakeExitMsg struct {
36 CmdID string `json:"cmdID"`
37 Timestamp string `json:"timestamp"`
38 Code int `json:"code"`
39 Error error `json:"error"`
42 // MakeOutEvent Event send in WS when characters are received on stdout/stderr
43 const MakeOutEvent = "make:output"
45 // MakeExitEvent Event send in WS when command exited
46 const MakeExitEvent = "make:exit"
50 func (s *APIService) buildMake(c *gin.Context) {
53 if c.BindJSON(&args) != nil {
54 common.APIError(c, "Invalid arguments")
58 sess := s.sessions.Get(c)
60 common.APIError(c, "Unknown sessions")
65 common.APIError(c, "Websocket not established")
69 // Allow to pass id in url (/make/:id) or as JSON argument
75 common.APIError(c, "Invalid id")
79 prj := s.mfolder.GetFolderFromID(id)
81 common.APIError(c, "Unknown id")
85 execTmo := args.CmdTimeout
87 // TODO get default timeout from config.json file
88 execTmo = 24 * 60 * 60 // 1 day
91 // TODO merge all code below with exec.go
93 // Define callback for output
94 var oCB common.EmitOutputCB
95 oCB = func(sid string, cmdID string, stdout, stderr string, data *map[string]interface{}) {
96 // IO socket can be nil when disconnected
97 so := s.sessions.IOSocketGet(sid)
99 s.log.Infof("%s not emitted: WS closed - sid: %s - msg id:%s", MakeOutEvent, sid, cmdID)
103 // Retrieve project ID and RootPath
104 prjID := (*data)["ID"].(string)
105 prjRootPath := (*data)["RootPath"].(string)
107 // Cleanup any references to internal rootpath in stdout & stderr
108 stdout = strings.Replace(stdout, prjRootPath, "", -1)
109 stderr = strings.Replace(stderr, prjRootPath, "", -1)
111 s.log.Debugf("%s emitted - WS sid %s - id:%d - prjID:%s", MakeOutEvent, sid, id, prjID)
113 // FIXME replace by .BroadcastTo a room
114 err := (*so).Emit(MakeOutEvent, MakeOutMsg{
116 Timestamp: time.Now().String(),
121 s.log.Errorf("WS Emit : %v", err)
125 // Define callback for output
126 eCB := func(sid string, cmdID string, code int, err error, data *map[string]interface{}) {
127 s.log.Debugf("Command [Cmd ID %s] exited: code %d, error: %v", cmdID, code, err)
129 // IO socket can be nil when disconnected
130 so := s.sessions.IOSocketGet(sid)
132 s.log.Infof("%s not emitted - WS closed (id:%s", MakeExitEvent, cmdID)
136 // Retrieve project ID and RootPath
137 prjID := (*data)["ID"].(string)
138 exitImm := (*data)["ExitImmediate"].(bool)
140 // XXX - workaround to be sure that Syncthing detected all changes
141 if err := s.mfolder.ForceSync(prjID); err != nil {
142 s.log.Errorf("Error while syncing folder %s: %v", prjID, err)
145 // Wait end of file sync
146 // FIXME pass as argument
148 for t := tmo; t > 0; t-- {
149 s.log.Debugf("Wait file insync for %s (%d/%d)", prjID, t, tmo)
150 if sync, err := s.mfolder.IsFolderInSync(prjID); sync || err != nil {
152 s.log.Errorf("ERROR IsFolderInSync (%s): %v", prjID, err)
156 time.Sleep(time.Second)
160 // FIXME replace by .BroadcastTo a room
161 e := (*so).Emit(MakeExitEvent, MakeExitMsg{
163 Timestamp: time.Now().String(),
168 s.log.Errorf("WS Emit : %v", e)
172 cmdID := strconv.Itoa(makeCommandID)
176 // Retrieve env command regarding Sdk ID
177 if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); len(envCmd) > 0 {
178 cmd = append(cmd, envCmd...)
179 cmd = append(cmd, "&&")
182 cmd = append(cmd, "cd", prj.GetFullPath(args.RPath), "&&", "make")
183 if len(args.Args) > 0 {
184 cmd = append(cmd, args.Args...)
187 s.log.Debugf("Execute [Cmd ID %d]: %v", cmdID, cmd)
189 data := make(map[string]interface{})
191 data["RootPath"] = prj.RootPath
192 data["ExitImmediate"] = args.ExitImmediate
194 err := common.ExecPipeWs(cmd, args.Env, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB, &data)
196 common.APIError(c, err.Error())
200 c.JSON(http.StatusOK,