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 SdkID string `json:"sdkid"` // sdk ID to use for setting env
18 Args []string `json:"args"` // args to pass to make command
19 Env []string `json:"env"`
20 RPath string `json:"rpath"` // relative path into project
21 CmdTimeout int `json:"timeout"` // command completion timeout in Second
24 // MakeOutMsg Message send on each output (stdout+stderr) of make command
25 type MakeOutMsg struct {
26 CmdID string `json:"cmdID"`
27 Timestamp string `json:"timestamp"`
28 Stdout string `json:"stdout"`
29 Stderr string `json:"stderr"`
32 // MakeExitMsg Message send on make command exit
33 type MakeExitMsg struct {
34 CmdID string `json:"cmdID"`
35 Timestamp string `json:"timestamp"`
36 Code int `json:"code"`
37 Error error `json:"error"`
40 // MakeOutEvent Event send in WS when characters are received on stdout/stderr
41 const MakeOutEvent = "make:output"
43 // MakeExitEvent Event send in WS when command exited
44 const MakeExitEvent = "make:exit"
48 func (s *APIService) buildMake(c *gin.Context) {
51 if c.BindJSON(&args) != nil {
52 common.APIError(c, "Invalid arguments")
56 sess := s.sessions.Get(c)
58 common.APIError(c, "Unknown sessions")
63 common.APIError(c, "Websocket not established")
67 // Allow to pass id in url (/make/:id) or as JSON argument
73 common.APIError(c, "Invalid id")
77 prj := s.mfolder.GetFolderFromID(id)
79 common.APIError(c, "Unknown id")
83 execTmo := args.CmdTimeout
85 // TODO get default timeout from config.json file
86 execTmo = 24 * 60 * 60 // 1 day
89 // Define callback for output
90 var oCB common.EmitOutputCB
91 oCB = func(sid string, id int, stdout, stderr string, data *map[string]interface{}) {
92 // IO socket can be nil when disconnected
93 so := s.sessions.IOSocketGet(sid)
95 s.log.Infof("%s not emitted: WS closed - sid: %s - msg id:%d", MakeOutEvent, sid, id)
98 s.log.Debugf("%s emitted - WS sid %s - id:%d", MakeOutEvent, sid, id)
100 // FIXME replace by .BroadcastTo a room
101 err := (*so).Emit(MakeOutEvent, MakeOutMsg{
102 CmdID: strconv.Itoa(id),
103 Timestamp: time.Now().String(),
108 s.log.Errorf("WS Emit : %v", err)
112 // Define callback for output
113 eCB := func(sid string, id int, code int, err error) {
114 s.log.Debugf("Command [Cmd ID %d] exited: code %d, error: %v", id, code, err)
116 // IO socket can be nil when disconnected
117 so := s.sessions.IOSocketGet(sid)
119 s.log.Infof("%s not emitted - WS closed (id:%d", MakeExitEvent, id)
123 // FIXME replace by .BroadcastTo a room
124 e := (*so).Emit(MakeExitEvent, MakeExitMsg{
125 CmdID: strconv.Itoa(id),
126 Timestamp: time.Now().String(),
131 s.log.Errorf("WS Emit : %v", e)
135 cmdID := makeCommandID
139 // Retrieve env command regarding Sdk ID
140 if envCmd := s.sdks.GetEnvCmd(args.SdkID, prj.DefaultSdk); len(envCmd) > 0 {
141 cmd = append(cmd, envCmd...)
142 cmd = append(cmd, "&&")
145 cmd = append(cmd, "cd", prj.GetFullPath(args.RPath), "&&", "make")
146 if len(args.Args) > 0 {
147 cmd = append(cmd, args.Args...)
150 s.log.Debugf("Execute [Cmd ID %d]: %v", cmdID, cmd)
151 err := common.ExecPipeWs(cmd, args.Env, sop, sess.ID, cmdID, execTmo, s.log, oCB, eCB, nil)
153 common.APIError(c, err.Error())
157 c.JSON(http.StatusOK,