@@ -6,9 +6,11 @@ import (
6
6
"fmt"
7
7
"io"
8
8
"io/ioutil"
9
+ "net"
9
10
"net/http"
10
11
"net/http/httptrace"
11
12
"os"
13
+ "path/filepath"
12
14
"regexp"
13
15
"strconv"
14
16
"strings"
@@ -20,20 +22,21 @@ const AppVersion = "1.2.0"
20
22
var compileInfo string
21
23
22
24
var (
23
- version = flag .Bool ("version" , false , "Prints program version" )
24
- networkAddress = flag .String ("address" , "localhost" , "The address of the board" )
25
- networkPort = flag .String ("port" , "80" , "The board needs to be listening on this port" )
26
- username = flag .String ("username" , "" , "Username for authentication" )
27
- password = flag .String ("password" , "" , "Password for authentication" )
28
- sketchPath = flag .String ("sketch" , "" , "Sketch path" )
29
- uploadEndpoint = flag .String ("upload" , "" , "Upload endpoint" )
30
- resetEndpoint = flag .String ("reset" , "" , "Upload endpoint" )
31
- syncEndpoint = flag .String ("sync" , "" , "Upload endpoint" )
32
- binMode = flag .Bool ("b" , false , "Upload binary mode" )
33
- verbose = flag .Bool ("v" , true , "Verbose flag" )
34
- quiet = flag .Bool ("q" , false , "Quiet flag" )
35
- useSsl = flag .String ("ssl" , "" , "SSL flag" )
36
- syncRet = flag .String ("sync_exp" , "" , "sync expected return code in format code:string" )
25
+ version = flag .Bool ("version" , false , "Prints program version" )
26
+ networkAddress = flag .String ("address" , "localhost" , "The address of the board" )
27
+ networkPort = flag .String ("port" , "80" , "The board needs to be listening on this port" )
28
+ username = flag .String ("username" , "" , "Username for authentication" )
29
+ password = flag .String ("password" , "" , "Password for authentication" )
30
+ sketchPath = flag .String ("sketch" , "" , "Sketch path" )
31
+ uploadEndpoint = flag .String ("upload" , "" , "Upload endpoint" )
32
+ resetEndpoint = flag .String ("reset" , "" , "Upload endpoint" )
33
+ syncEndpoint = flag .String ("sync" , "" , "Upload endpoint" )
34
+ binMode = flag .Bool ("b" , false , "Upload binary mode" )
35
+ verbose = flag .Bool ("v" , true , "Verbose flag" )
36
+ quiet = flag .Bool ("q" , false , "Quiet flag" )
37
+ useSsl = flag .String ("ssl" , "" , "SSL flag" )
38
+ syncRet = flag .String ("sync_exp" , "" , "sync expected return code in format code:string" )
39
+ hasDownloadFile = flag .Bool ("d" , false , "set to true to take advantage of downloadFile API" )
37
40
)
38
41
39
42
type Item struct {
@@ -135,6 +138,15 @@ func main() {
135
138
sketchData = bytes .NewBufferString (str )
136
139
}
137
140
141
+ if * hasDownloadFile {
142
+ http .ListenAndServe (* networkPort , http .FileServer (http .Dir (filepath .Dir (* sketchPath ))))
143
+ // find my ip if not specified
144
+ ip := getMyIP (net .ParseIP (* networkAddress ))
145
+ url := "http://" + ip .String () + ":" + * networkPort + "/" + filepath .Base (* sketchPath )
146
+ sketchData = bytes .NewBufferString (url )
147
+ fmt .Println ("Serving sketch on " + url )
148
+ }
149
+
138
150
req , err := http .NewRequest ("POST" , httpheader + * networkAddress + ":" + * networkPort + * uploadEndpoint , sketchData )
139
151
if err != nil {
140
152
if * verbose {
@@ -228,3 +240,25 @@ func StreamToBytes(stream io.Reader) *bytes.Buffer {
228
240
func StreamToString (stream io.Reader ) string {
229
241
return StreamToBytes (stream ).String ()
230
242
}
243
+
244
+ func getMyIP (otherip net.IP ) net.IP {
245
+ ifaces , _ := net .Interfaces ()
246
+ // handle err
247
+ var ips []net.IP
248
+ for _ , i := range ifaces {
249
+ addrs , _ := i .Addrs ()
250
+ // handle err
251
+ for _ , addr := range addrs {
252
+ switch v := addr .(type ) {
253
+ case * net.IPNet :
254
+ if v .Contains (otherip ) {
255
+ return v .IP
256
+ }
257
+ case * net.IPAddr :
258
+ ips = append (ips , v .IP )
259
+ }
260
+ // process IP address
261
+ }
262
+ }
263
+ return nil
264
+ }
0 commit comments