10 "github.com/gin-gonic/gin"
11 "github.com/iotbzh/xds-server/lib/common"
14 // MakeArgs is the parameters (json format) of /make command
15 type MakeArgs struct {
17 RPath string `json:"rpath"` // relative path into project
18 Args string `json:"args"`
19 CmdTimeout int `json:"timeout"` // command completion timeout in Second
22 // MakeOutMsg Message send on each output (stdout+stderr) of make command
23 type MakeOutMsg struct {
24 CmdID string `json:"cmdID"`
25 Timestamp string `json:"timestamp"`
26 Stdout string `json:"stdout"`
27 Stderr string `json:"stderr"`
30 // MakeExitMsg Message send on make command exit
31 type MakeExitMsg struct {
32 CmdID string `json:"cmdID"`
33 Timestamp string `json:"timestamp"`
34 Code int `json:"code"`
35 Error error `json:"error"`
38 // MakeOutEvent Event send in WS when characters are received on stdout/stderr
39 const MakeOutEvent = "make:output"
41 // MakeExitEvent Event send in WS when command exited
42 const MakeExitEvent = "make:exit"
46 func (s *APIService) buildMake(c *gin.Context) {
49 if c.BindJSON(&args) != nil {
50 common.APIError(c, "Invalid arguments")
54 sess := s.sessions.Get(c)
56 common.APIError(c, "Unknown sessions")
61 common.APIError(c, "Websocket not established")
65 // Allow to pass id in url (/make/:id) or as JSON argument
71 common.APIError(c, "Invalid id")
75 prj := s.cfg.GetFolderFromID(id)
77 common.APIError(c, "Unknown id")
81 execTmo := args.CmdTimeout
83 // TODO get default timeout from config.json file
84 execTmo = 24 * 60 * 60 // 1 day
87 cmd := "cd " + prj.GetFullPath(args.RPath) + " && make"
89 cmd += " " + args.Args
92 // Define callback for output
93 var oCB common.EmitOutputCB
94 oCB = func(sid string, id int, stdout, stderr string) {
95 // IO socket can be nil when disconnected
96 so := s.sessions.IOSocketGet(sid)
98 s.log.Infof("%s not emitted: WS closed - sid: %s - msg id:%d", MakeOutEvent, sid, id)
101 s.log.Debugf("%s emitted - WS sid %s - id:%d", MakeOutEvent, sid, id)
103 // FIXME replace by .BroadcastTo a room
104 err := (*so).Emit(MakeOutEvent, MakeOutMsg{
105 CmdID: strconv.Itoa(id),
106 Timestamp: time.Now().String(),
111 s.log.Errorf("WS Emit : %v", err)
115 // Define callback for output
116 eCB := func(sid string, id int, code int, err error) {
117 s.log.Debugf("Command [Cmd ID %d] exited: code %d, error: %v", id, code, err)
119 // IO socket can be nil when disconnected
120 so := s.sessions.IOSocketGet(sid)
122 s.log.Infof("%s not emitted - WS closed (id:%d", MakeExitEvent, id)
126 // FIXME replace by .BroadcastTo a room
127 e := (*so).Emit(MakeExitEvent, MakeExitMsg{
128 CmdID: strconv.Itoa(id),
129 Timestamp: time.Now().String(),
134 s.log.Errorf("WS Emit : %v", e)
138 cmdID := makeCommandID
141 s.log.Debugf("Execute [Cmd ID %d]: %v", cmdID, cmd)
142 err := common.ExecPipeWs(cmd, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB)
144 common.APIError(c, err.Error())
148 c.JSON(http.StatusOK,