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"` // args to pass to make command
19 SdkID string `json:"sdkid"` // sdk ID to use for setting env
20 CmdTimeout int `json:"timeout"` // command completion timeout in Second
23 // MakeOutMsg Message send on each output (stdout+stderr) of make command
24 type MakeOutMsg struct {
25 CmdID string `json:"cmdID"`
26 Timestamp string `json:"timestamp"`
27 Stdout string `json:"stdout"`
28 Stderr string `json:"stderr"`
31 // MakeExitMsg Message send on make command exit
32 type MakeExitMsg struct {
33 CmdID string `json:"cmdID"`
34 Timestamp string `json:"timestamp"`
35 Code int `json:"code"`
36 Error error `json:"error"`
39 // MakeOutEvent Event send in WS when characters are received on stdout/stderr
40 const MakeOutEvent = "make:output"
42 // MakeExitEvent Event send in WS when command exited
43 const MakeExitEvent = "make:exit"
47 func (s *APIService) buildMake(c *gin.Context) {
50 if c.BindJSON(&args) != nil {
51 common.APIError(c, "Invalid arguments")
55 sess := s.sessions.Get(c)
57 common.APIError(c, "Unknown sessions")
62 common.APIError(c, "Websocket not established")
66 // Allow to pass id in url (/make/:id) or as JSON argument
72 common.APIError(c, "Invalid id")
76 prj := s.mfolder.GetFolderFromID(id)
78 common.APIError(c, "Unknown id")
82 execTmo := args.CmdTimeout
84 // TODO get default timeout from config.json file
85 execTmo = 24 * 60 * 60 // 1 day
88 cmd := "cd " + prj.GetFullPath(args.RPath) + " && make"
90 cmd += " " + args.Args
93 // Define callback for output
94 var oCB common.EmitOutputCB
95 oCB = func(sid string, id int, stdout, stderr string) {
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:%d", MakeOutEvent, sid, id)
102 s.log.Debugf("%s emitted - WS sid %s - id:%d", MakeOutEvent, sid, id)
104 // FIXME replace by .BroadcastTo a room
105 err := (*so).Emit(MakeOutEvent, MakeOutMsg{
106 CmdID: strconv.Itoa(id),
107 Timestamp: time.Now().String(),
112 s.log.Errorf("WS Emit : %v", err)
116 // Define callback for output
117 eCB := func(sid string, id int, code int, err error) {
118 s.log.Debugf("Command [Cmd ID %d] exited: code %d, error: %v", id, code, err)
120 // IO socket can be nil when disconnected
121 so := s.sessions.IOSocketGet(sid)
123 s.log.Infof("%s not emitted - WS closed (id:%d", MakeExitEvent, id)
127 // FIXME replace by .BroadcastTo a room
128 e := (*so).Emit(MakeExitEvent, MakeExitMsg{
129 CmdID: strconv.Itoa(id),
130 Timestamp: time.Now().String(),
135 s.log.Errorf("WS Emit : %v", e)
139 cmdID := makeCommandID
142 // Retrieve env command regarding Sdk ID
143 if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); envCmd != "" {
144 cmd = envCmd + " && " + cmd
147 s.log.Debugf("Execute [Cmd ID %d]: %v", cmdID, cmd)
148 err := common.ExecPipeWs(cmd, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB)
150 common.APIError(c, err.Error())
154 c.JSON(http.StatusOK,