@@ -3,6 +3,7 @@ package main
3
3
import (
4
4
"bufio"
5
5
"fmt"
6
+ "github.com/mattn/go-shellwords"
6
7
"io"
7
8
"io/ioutil"
8
9
"os"
@@ -21,16 +22,18 @@ func PrintlnVerbose(a ...interface{}) {
21
22
}
22
23
}
23
24
24
- func main () {
25
- fmt .Println ("Starting download script..." )
25
+ func main_load (args []string ) {
26
26
27
27
// ARG 1: Path to binaries
28
28
// ARG 2: BIN File to download
29
29
// ARG 3: TTY port to use.
30
30
// ARG 4: quiet/verbose
31
31
// path may contain \ need to change all to /
32
32
33
- args := os .Args [1 :]
33
+ if len (args ) < 4 {
34
+ fmt .Println ("Not enough arguments" )
35
+ os .Exit (1 )
36
+ }
34
37
35
38
bin_path := args [0 ]
36
39
dfu := bin_path + "/dfu-util"
@@ -105,6 +108,69 @@ func main() {
105
108
}
106
109
}
107
110
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
+
108
174
func launchCommandAndWaitForOutput (command []string , stringToSearch string , print_output bool ) (error , bool ) {
109
175
oscmd := exec .Command (command [0 ], command [1 :]... )
110
176
tellCommandNotToSpawnShell (oscmd )
@@ -128,3 +194,10 @@ func launchCommandAndWaitForOutput(command []string, stringToSearch string, prin
128
194
err = oscmd .Wait ()
129
195
return err , found
130
196
}
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