-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (91 loc) · 2.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"log"
"mime/multipart"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
type Form struct {
Files []*multipart.FileHeader `form:"files" binding:"required"`
}
func main() {
r := gin.Default()
r.StaticFS("/content", http.Dir("html/"))
r.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "/content")
})
r.POST("/analyze", func(c *gin.Context) {
timestamp := c.DefaultQuery("ts", strconv.FormatInt(time.Now().Unix(), 10))
// Multipart form
//var form Form
//s := c.PostForm("files")
form, _ := c.MultipartForm()
files := form.File["files"]
fmt.Println(form)
//c.ShouldBind(&form)
s := []string{"lisa4ros2-0.1a1/bin/lisa4ros2"}
for _, file := range files {
log.Println(file.Filename)
//Get raw file bytes - no reader method
// Upload to disk
// `formFile` has io.reader method
c.SaveUploadedFile(file, "analysis/ros-sources/"+timestamp+"/"+file.Filename)
s = append(s, "analysis/ros-sources/"+timestamp+"/"+file.Filename)
}
s = append(s, "html/analysis/"+timestamp)
fmt.Println(strings.Join(s, " "))
cmd := exec.Command("/bin/sh", s...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
os.Mkdir("html/analysis/"+timestamp, os.ModePerm)
outfile, err := os.Create("html/analysis/" + timestamp + "/analysis.log")
if err != nil {
panic(err)
}
defer outfile.Close()
cmd.Stdout = outfile
if err != nil {
fmt.Println("Error 1")
c.JSON(http.StatusOK, gin.H{
"error": true,
})
return
}
if err = cmd.Start(); err != nil {
fmt.Println("Error 2")
c.JSON(http.StatusOK, gin.H{
"error": true,
})
return
}
if err = cmd.Wait(); err != nil {
fmt.Println("Error 4")
if exitError, ok := err.(*exec.ExitError); ok {
fmt.Printf("Exit code is %d\n", exitError.ExitCode())
c.JSON(http.StatusOK, gin.H{
"error": true,
})
return
}
}
if err != nil {
fmt.Printf("error %s", err)
c.JSON(http.StatusOK, gin.H{
"url": "analysis/" + timestamp + "/" + "report.html",
"error": false,
})
return
}
c.JSON(http.StatusOK, gin.H{
"url": "analysis/" + timestamp + "/" + "report.html",
//"analysisStatus": string(bytes),
})
})
r.Run(":9888")
}