Skip to content

Commit 1dd757f

Browse files
committed
add arduino101debug logic
1 parent 6ec3edd commit 1dd757f

File tree

1 file changed

+76
-3
lines changed

1 file changed

+76
-3
lines changed

main.go

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"bufio"
55
"fmt"
6+
"github.com/mattn/go-shellwords"
67
"io"
78
"io/ioutil"
89
"os"
@@ -21,16 +22,18 @@ func PrintlnVerbose(a ...interface{}) {
2122
}
2223
}
2324

24-
func main() {
25-
fmt.Println("Starting download script...")
25+
func main_load(args []string) {
2626

2727
// ARG 1: Path to binaries
2828
// ARG 2: BIN File to download
2929
// ARG 3: TTY port to use.
3030
// ARG 4: quiet/verbose
3131
// path may contain \ need to change all to /
3232

33-
args := os.Args[1:]
33+
if len(args) < 4 {
34+
fmt.Println("Not enough arguments")
35+
os.Exit(1)
36+
}
3437

3538
bin_path := args[0]
3639
dfu := bin_path + "/dfu-util"
@@ -105,6 +108,69 @@ func main() {
105108
}
106109
}
107110

111+
func main_debug(args []string) {
112+
113+
if len(args) < 1 {
114+
fmt.Println("Not enough arguments")
115+
os.Exit(1)
116+
}
117+
118+
type Command struct {
119+
command string
120+
background bool
121+
}
122+
123+
var commands []Command
124+
125+
fullcmdline := strings.Join(args[:], "")
126+
temp_commands := strings.Split(fullcmdline, ";")
127+
for _, command := range temp_commands {
128+
background_commands := strings.Split(command, "&")
129+
for i, command := range background_commands {
130+
var cmd Command
131+
cmd.background = (i < len(background_commands)-1)
132+
cmd.command = command
133+
commands = append(commands, cmd)
134+
}
135+
}
136+
137+
var err error
138+
139+
for _, command := range commands {
140+
fmt.Println("command: " + command.command)
141+
cmd, _ := shellwords.Parse(command.command)
142+
fmt.Println(cmd)
143+
if command.background == false {
144+
err, _ = launchCommandAndWaitForOutput(cmd, "", true)
145+
} else {
146+
err, _ = launchCommandBackground(cmd, "", true)
147+
}
148+
if err != nil {
149+
fmt.Println("ERROR: Command \" " + command.command + " \" failed")
150+
os.Exit(1)
151+
}
152+
}
153+
os.Exit(0)
154+
}
155+
156+
func main() {
157+
name := os.Args[0]
158+
args := os.Args[1:]
159+
160+
if strings.Contains(name, "load") {
161+
fmt.Println("Starting download script...")
162+
main_load(args)
163+
}
164+
165+
if strings.Contains(name, "debug") {
166+
fmt.Println("Starting debug script...")
167+
main_debug(args)
168+
}
169+
170+
fmt.Println("Wrong executable name")
171+
os.Exit(1)
172+
}
173+
108174
func launchCommandAndWaitForOutput(command []string, stringToSearch string, print_output bool) (error, bool) {
109175
oscmd := exec.Command(command[0], command[1:]...)
110176
tellCommandNotToSpawnShell(oscmd)
@@ -128,3 +194,10 @@ func launchCommandAndWaitForOutput(command []string, stringToSearch string, prin
128194
err = oscmd.Wait()
129195
return err, found
130196
}
197+
198+
func launchCommandBackground(command []string, stringToSearch string, print_output bool) (error, bool) {
199+
oscmd := exec.Command(command[0], command[1:]...)
200+
tellCommandNotToSpawnShell(oscmd)
201+
err := oscmd.Start()
202+
return err, false
203+
}

0 commit comments

Comments
 (0)