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 CmdID string `json:"cmdID"` // command unique ID
20 Args []string `json:"args"` // args to pass to make command
21 Env []string `json:"env"`
22 RPath string `json:"rpath"` // relative path into project
23 ExitImmediate bool `json:"exitImmediate"` // when true, exit event sent immediately when command exited (IOW, don't wait file synchronization)
24 CmdTimeout int `json:"timeout"` // command completion timeout in Second
27 // MakeOutMsg Message send on each output (stdout+stderr) of make command
28 type MakeOutMsg struct {
29 CmdID string `json:"cmdID"`
30 Timestamp string `json:"timestamp"`
31 Stdout string `json:"stdout"`
32 Stderr string `json:"stderr"`
35 // MakeExitMsg Message send on make command exit
36 type MakeExitMsg struct {
37 CmdID string `json:"cmdID"`
38 Timestamp string `json:"timestamp"`
39 Code int `json:"code"`
40 Error error `json:"error"`
43 // MakeOutEvent Event send in WS when characters are received on stdout/stderr
44 const MakeOutEvent = "make:output"
46 // MakeExitEvent Event send in WS when command exited
47 const MakeExitEvent = "make:exit"
51 func (s *APIService) buildMake(c *gin.Context) {
54 if c.BindJSON(&args) != nil {
55 common.APIError(c, "Invalid arguments")
59 sess := s.sessions.Get(c)
61 common.APIError(c, "Unknown sessions")
66 common.APIError(c, "Websocket not established")
70 // Allow to pass id in url (/make/:id) or as JSON argument
76 common.APIError(c, "Invalid id")
80 pf := s.mfolders.Get(id)
82 common.APIError(c, "Unknown id")
86 prj := folder.GetConfig()
88 execTmo := args.CmdTimeout
90 // TODO get default timeout from config.json file
91 execTmo = 24 * 60 * 60 // 1 day
94 // TODO merge all code below with exec.go
96 // Define callback for output
97 var oCB common.EmitOutputCB
98 oCB = func(sid string, cmdID string, stdout, stderr string, data *map[string]interface{}) {
99 // IO socket can be nil when disconnected
100 so := s.sessions.IOSocketGet(sid)
102 s.log.Infof("%s not emitted: WS closed - sid: %s - msg id:%s", MakeOutEvent, sid, cmdID)
106 // Retrieve project ID and RootPath
107 prjID := (*data)["ID"].(string)
108 prjRootPath := (*data)["RootPath"].(string)
110 // Cleanup any references to internal rootpath in stdout & stderr
111 stdout = strings.Replace(stdout, prjRootPath, "", -1)
112 stderr = strings.Replace(stderr, prjRootPath, "", -1)
114 s.log.Debugf("%s emitted - WS sid %s - id:%d - prjID:%s", MakeOutEvent, sid, id, prjID)
116 // FIXME replace by .BroadcastTo a room
117 err := (*so).Emit(MakeOutEvent, MakeOutMsg{
119 Timestamp: time.Now().String(),
124 s.log.Errorf("WS Emit : %v", err)
128 // Define callback for output
129 eCB := func(sid string, cmdID string, code int, err error, data *map[string]interface{}) {
130 s.log.Debugf("Command [Cmd ID %s] exited: code %d, error: %v", cmdID, code, err)
132 // IO socket can be nil when disconnected
133 so := s.sessions.IOSocketGet(sid)
135 s.log.Infof("%s not emitted - WS closed (id:%s", MakeExitEvent, cmdID)
139 // Retrieve project ID and RootPath
140 prjID := (*data)["ID"].(string)
141 exitImm := (*data)["ExitImmediate"].(bool)
143 // XXX - workaround to be sure that Syncthing detected all changes
144 if err := s.mfolders.ForceSync(prjID); err != nil {
145 s.log.Errorf("Error while syncing folder %s: %v", prjID, err)
148 // Wait end of file sync
149 // FIXME pass as argument
151 for t := tmo; t > 0; t-- {
152 s.log.Debugf("Wait file insync for %s (%d/%d)", prjID, t, tmo)
153 if sync, err := s.mfolders.IsFolderInSync(prjID); sync || err != nil {
155 s.log.Errorf("ERROR IsFolderInSync (%s): %v", prjID, err)
159 time.Sleep(time.Second)
163 // FIXME replace by .BroadcastTo a room
164 e := (*so).Emit(MakeExitEvent, MakeExitMsg{
166 Timestamp: time.Now().String(),
171 s.log.Errorf("WS Emit : %v", e)
175 // Unique ID for each commands
176 if args.CmdID == "" {
177 args.CmdID = s.cfg.ServerUID[:18] + "_" + strconv.Itoa(makeCommandID)
182 // Retrieve env command regarding Sdk ID
183 if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); len(envCmd) > 0 {
184 cmd = append(cmd, envCmd...)
185 cmd = append(cmd, "&&")
188 cmd = append(cmd, "cd", folder.GetFullPath(args.RPath), "&&", "make")
189 if len(args.Args) > 0 {
190 cmd = append(cmd, args.Args...)
193 s.log.Debugf("Execute [Cmd ID %d]: %v", args.CmdID, cmd)
195 data := make(map[string]interface{})
197 data["RootPath"] = prj.RootPath
198 data["ExitImmediate"] = args.ExitImmediate
200 err := common.ExecPipeWs(cmd, args.Env, sop, sess.ID, args.CmdID, execTmo, s.log, oCB, eCB, &data)
202 common.APIError(c, err.Error())
206 c.JSON(http.StatusOK,