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 // Event name send in WS
39 const MakeOutEvent = "make:output"
40 const MakeExitEvent = "make:exit"
44 func (s *APIService) buildMake(c *gin.Context) {
47 if c.BindJSON(&args) != nil {
48 common.APIError(c, "Invalid arguments")
52 sess := s.sessions.Get(c)
54 common.APIError(c, "Unknown sessions")
59 common.APIError(c, "Websocket not established")
63 // Allow to pass id in url (/make/:id) or as JSON argument
69 common.APIError(c, "Invalid id")
73 prj := s.cfg.GetFolderFromID(id)
75 common.APIError(c, "Unknown id")
79 execTmo := args.CmdTimeout
81 // TODO get default timeout from config.json file
82 execTmo = 24 * 60 * 60 // 1 day
85 cmd := "cd " + prj.GetFullPath(args.RPath) + " && make"
87 cmd += " " + args.Args
90 // Define callback for output
91 var oCB common.EmitOutputCB
92 oCB = func(sid string, id int, stdout, stderr string) {
93 // IO socket can be nil when disconnected
94 so := s.sessions.IOSocketGet(sid)
96 s.log.Infof("%s not emitted: WS closed - sid: %s - msg id:%d", MakeOutEvent, sid, id)
99 s.log.Debugf("%s emitted - WS sid %s - id:%d", MakeOutEvent, sid, id)
101 // FIXME replace by .BroadcastTo a room
102 err := (*so).Emit(MakeOutEvent, MakeOutMsg{
103 CmdID: strconv.Itoa(id),
104 Timestamp: time.Now().String(),
109 s.log.Errorf("WS Emit : %v", err)
113 // Define callback for output
114 eCB := func(sid string, id int, code int, err error) {
115 s.log.Debugf("Command [Cmd ID %d] exited: code %d, error: %v", id, code, err)
117 // IO socket can be nil when disconnected
118 so := s.sessions.IOSocketGet(sid)
120 s.log.Infof("%s not emitted - WS closed (id:%d", MakeExitEvent, id)
124 // FIXME replace by .BroadcastTo a room
125 e := (*so).Emit(MakeExitEvent, MakeExitMsg{
126 CmdID: strconv.Itoa(id),
127 Timestamp: time.Now().String(),
132 s.log.Errorf("WS Emit : %v", e)
136 cmdID := makeCommandID
139 s.log.Debugf("Execute [Cmd ID %d]: %v", cmdID, cmd)
140 err := common.ExecPipeWs(cmd, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB)
142 common.APIError(c, err.Error())
146 c.JSON(http.StatusOK,