Skip to content

Commit 31f26dd

Browse files
matteosuppofacchinm
authored andcommitted
Add function to program
1 parent 242fa79 commit 31f26dd

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed

programmer/programmer.go

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package programmer
22

33
import (
4+
"bufio"
5+
"os/exec"
46
"path/filepath"
57
"regexp"
8+
"runtime"
69
"strings"
710
"time"
811

12+
"github.com/arduino/arduino-create-agent/tools"
913
"github.com/facchinm/go-serial"
1014
"github.com/pkg/errors"
1115
)
@@ -27,8 +31,8 @@ func info(l logger, args ...interface{}) {
2731
}
2832
}
2933

30-
// tools can return the location of a tool in the system
31-
type tools interface {
34+
// locater can return the location of a tool in the system
35+
type locater interface {
3236
GetLocation(command string) (string, error)
3337
}
3438

@@ -50,8 +54,8 @@ type Extra struct {
5054
}
5155

5256
// Resolve replaces some symbols in the commandline with the appropriate values
53-
// it can return an error when looking a variable in the tools
54-
func Resolve(port, board, file, commandline string, extra Extra, tools tools) (string, error) {
57+
// it can return an error when looking a variable in the locater
58+
func Resolve(port, board, file, commandline string, extra Extra, t locater) (string, error) {
5559
commandline = strings.Replace(commandline, "{build.path}", filepath.ToSlash(filepath.Dir(file)), -1)
5660
commandline = strings.Replace(commandline, "{build.project_name}", strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file))), -1)
5761
commandline = strings.Replace(commandline, "{serial.port}", port, -1)
@@ -63,13 +67,13 @@ func Resolve(port, board, file, commandline string, extra Extra, tools tools) (s
6367
commandline = strings.Replace(commandline, "{upload.verbose}", extra.ParamsQuiet, -1)
6468
}
6569

66-
// search for runtime variables and replace with values from globalToolsMap
70+
// search for runtime variables and replace with values from locater
6771
var runtimeRe = regexp.MustCompile("\\{(.*?)\\}")
6872
runtimeVars := runtimeRe.FindAllString(commandline, -1)
6973

7074
for _, element := range runtimeVars {
7175

72-
location, err := tools.GetLocation(element)
76+
location, err := t.GetLocation(element)
7377
if err != nil {
7478
return "", errors.Wrapf(err, "get location of %s", element)
7579
}
@@ -80,7 +84,7 @@ func Resolve(port, board, file, commandline string, extra Extra, tools tools) (s
8084
}
8185

8286
// Do performs a command on a port with a board attached to it
83-
func Do(port, board, file, commandline string, extra Extra, t tools, l logger) {
87+
func Do(port, board, file, commandline string, extra Extra, t locater, l logger) {
8488
debug(l, port, board, file, commandline)
8589
if extra.Network {
8690
doNetwork()
@@ -91,7 +95,7 @@ func Do(port, board, file, commandline string, extra Extra, t tools, l logger) {
9195

9296
func doNetwork() {}
9397

94-
func doSerial(port, board, file, commandline string, extra Extra, t tools, l logger) error {
98+
func doSerial(port, board, file, commandline string, extra Extra, t locater, l logger) error {
9599
// some boards needs to be resetted
96100
if extra.Use1200bpsTouch {
97101
var err error
@@ -193,3 +197,58 @@ func waitReset(beforeReset []string, l logger) string {
193197

194198
return port
195199
}
200+
201+
func program(binary string, args []string, l logger) error {
202+
// remove quotes form binary command and args
203+
binary = strings.Replace(binary, "\"", "", -1)
204+
205+
for i := range args {
206+
args[i] = strings.Replace(args[i], "\"", "", -1)
207+
}
208+
209+
// find extension
210+
extension := ""
211+
if runtime.GOOS == "windows" {
212+
extension = ".exe"
213+
}
214+
215+
oscmd := exec.Command(binary, args...)
216+
217+
tools.TellCommandNotToSpawnShell(oscmd)
218+
219+
stdout, err := oscmd.StdoutPipe()
220+
if err != nil {
221+
return err
222+
}
223+
224+
stderr, err := oscmd.StderrPipe()
225+
if err != nil {
226+
return err
227+
}
228+
229+
info(l, "Flashing with command:"+binary+extension+" "+strings.Join(args, " "))
230+
231+
err = oscmd.Start()
232+
233+
stdoutCopy := bufio.NewScanner(stdout)
234+
stderrCopy := bufio.NewScanner(stderr)
235+
236+
stdoutCopy.Split(bufio.ScanLines)
237+
stderrCopy.Split(bufio.ScanLines)
238+
239+
go func() {
240+
for stdoutCopy.Scan() {
241+
info(l, stdoutCopy.Text())
242+
}
243+
}()
244+
245+
go func() {
246+
for stderrCopy.Scan() {
247+
info(l, stdoutCopy.Text())
248+
}
249+
}()
250+
251+
err = oscmd.Wait()
252+
253+
return err
254+
}

0 commit comments

Comments
 (0)