diff --git a/conn.go b/conn.go index 95954dc21..dca6d44b4 100644 --- a/conn.go +++ b/conn.go @@ -21,7 +21,7 @@ import ( "github.com/arduino/arduino-create-agent/upload" "github.com/arduino/arduino-create-agent/utilities" "github.com/gin-gonic/gin" - "github.com/googollee/go-socket.io" + socketio "github.com/googollee/go-socket.io" ) type connection struct { @@ -72,6 +72,7 @@ type Upload struct { var uploadStatusStr string = "ProgrammerStatus" func uploadHandler(c *gin.Context) { + data := new(Upload) c.BindJSON(data) @@ -115,8 +116,12 @@ func uploadHandler(c *gin.Context) { return } + var filePaths []string + filePaths = append(filePaths, filePath) + for _, extraFile := range data.ExtraFiles { path := filepath.Join(filepath.Dir(filePath), extraFile.Filename) + filePaths = append(filePaths, path) log.Printf("Saving %s on %s", extraFile.Filename, path) err := ioutil.WriteFile(path, extraFile.Hex, 0644) if err != nil { @@ -141,7 +146,7 @@ func uploadHandler(c *gin.Context) { // Upload if data.Extra.Network { send(map[string]string{uploadStatusStr: "Starting", "Cmd": "Network"}) - err = upload.Network(data.Port, data.Board, filePath, commandline, data.Extra.Auth, l) + err = upload.Network(data.Port, data.Board, filePaths, commandline, data.Extra.Auth, l, data.Extra.SSH) } else { send(map[string]string{uploadStatusStr: "Starting", "Cmd": "Serial"}) err = upload.Serial(data.Port, commandline, data.Extra, l) diff --git a/main.go b/main.go index 3ff714759..cd5562284 100755 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ import ( "github.com/arduino/arduino-create-agent/tools" "github.com/arduino/arduino-create-agent/utilities" "github.com/gin-gonic/gin" - "github.com/itsjamie/gin-cors" + cors "github.com/itsjamie/gin-cors" "github.com/kardianos/osext" "github.com/vharitonsky/iniflags" //"github.com/sanbornm/go-selfupdate/selfupdate" #included in update.go to change heavily diff --git a/upload/upload.go b/upload/upload.go index 14a4c1055..278972b02 100644 --- a/upload/upload.go +++ b/upload/upload.go @@ -20,7 +20,7 @@ import ( shellwords "github.com/mattn/go-shellwords" "github.com/pkg/errors" "github.com/sfreiberg/simplessh" - "go.bug.st/serial.v1" + serial "go.bug.st/serial.v1" ) // Busy tells wether the programmer is doing something @@ -41,6 +41,7 @@ type Extra struct { Verbose bool `json:"verbose"` ParamsVerbose string `json:"params_verbose"` ParamsQuiet string `json:"params_quiet"` + SSH bool `json:"ssh", omitempty` } // PartiallyResolve replaces some symbols in the commandline with the appropriate values @@ -80,7 +81,7 @@ func fixupPort(port, commandline string) string { } // Network performs a network upload -func Network(port, board, file, commandline string, auth Auth, l Logger) error { +func Network(port, board string, files []string, commandline string, auth Auth, l Logger, SSH bool) error { Busy = true // Defaults @@ -93,11 +94,11 @@ func Network(port, board, file, commandline string, auth Auth, l Logger) error { commandline = fixupPort(port, commandline) - // try with a form - err := form(port, board, file, auth, l) + // try with ssh + err := ssh(port, files, commandline, auth, l, SSH) if err != nil { - // try with ssh - err = ssh(port, file, commandline, auth, l) + // fallback on form + err = form(port, board, files[0], auth, l) } Busy = false @@ -359,12 +360,13 @@ func form(port, board, file string, auth Auth, l Logger) error { // Check the response if res.StatusCode != http.StatusOK { - return errors.Wrapf(err, "Bad status: %s", res.Status) + body, _ := ioutil.ReadAll(res.Body) + return errors.New("Request error:" + string(body)) } return nil } -func ssh(port, file, commandline string, auth Auth, l Logger) error { +func ssh(port string, files []string, commandline string, auth Auth, l Logger, SSH bool) error { // Connect via ssh client, err := simplessh.ConnectWithPassword(port+":22", auth.Username, auth.Password) debug(l, "Connect via ssh ", client, err) @@ -374,10 +376,17 @@ func ssh(port, file, commandline string, auth Auth, l Logger) error { defer client.Close() // Copy the sketch - err = scp(client, file, "/tmp/sketch"+filepath.Ext(file)) - debug(l, "Copy the sketch ", err) - if err != nil { - return errors.Wrapf(err, "Copy sketch") + for _, file := range files { + fileName := "/tmp/sketch" + filepath.Ext(file) + if SSH { + // don't rename files + fileName = "/tmp/" + filepath.Base(file) + } + err = client.Upload(file, fileName) + debug(l, "Copy "+filepath.Ext(file), err) + if err != nil { + return errors.Wrapf(err, "Copy sketch") + } } // very special case for Yun (remove once AVR boards.txt is fixed) @@ -387,7 +396,8 @@ func ssh(port, file, commandline string, auth Auth, l Logger) error { // Execute commandline output, err := client.Exec(commandline) - debug(l, "Execute commandline ", commandline, output, err) + info(l, output) + debug(l, "Execute commandline ", commandline, string(output), err) if err != nil { return errors.Wrapf(err, "Execute commandline") }