Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 86fdff2

Browse files
Merge pull request #3 from sandeepmistry/auth
Various enhancements
2 parents 37ecb00 + c079b96 commit 86fdff2

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

‎main.go‎

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"io/ioutil"
99
"net/http"
10+
"net/http/httptrace"
1011
"os"
1112
"regexp"
1213
"strconv"
@@ -22,6 +23,8 @@ var (
2223
version = flag.Bool("version", false, "Prints program version")
2324
networkAddress = flag.String("address", "localhost", "The address of the board")
2425
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")
2528
sketchPath = flag.String("sketch", "", "Sketch path")
2629
uploadEndpoint = flag.String("upload", "", "Upload endpoint")
2730
resetEndpoint = flag.String("reset", "", "Upload endpoint")
@@ -46,6 +49,10 @@ func main() {
4649
os.Exit(0)
4750
}
4851

52+
var httpClient = &http.Client{
53+
Timeout: time.Second * 10,
54+
}
55+
4956
httpheader := "http://"
5057

5158
if *useSsl != "" {
@@ -68,7 +75,7 @@ func main() {
6875
fmt.Println("Resetting the board")
6976
}
7077

71-
resp, err := http.Post(httpheader+*networkAddress+":"+*networkPort+*syncEndpoint, "", nil)
78+
resp, err := httpClient.Post(httpheader+*networkAddress+":"+*networkPort+*syncEndpoint, "", nil)
7279
if err != nil || resp.StatusCode != syncRetCode {
7380
if *verbose {
7481
fmt.Println("Failed to reset the board, upload failed")
@@ -86,7 +93,7 @@ func main() {
8693
timeout := 0
8794

8895
for timeout < 10 {
89-
resp, err := http.Get(httpheader + *networkAddress + ":" + *networkPort + *syncEndpoint)
96+
resp, err := httpClient.Get(httpheader + *networkAddress + ":" + *networkPort + *syncEndpoint)
9097
if err != nil {
9198
if *verbose {
9299
fmt.Println("Failed to reset the board, upload failed")
@@ -108,10 +115,6 @@ func main() {
108115
}
109116

110117
if *uploadEndpoint != "" {
111-
if *verbose {
112-
fmt.Println("Uploading the sketch")
113-
}
114-
115118
f, err := os.Open(*sketchPath)
116119
if err != nil {
117120
if *verbose {
@@ -139,9 +142,44 @@ func main() {
139142
}
140143
os.Exit(1)
141144
}
142-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
143145

144-
resp, err := http.DefaultClient.Do(req)
146+
if *binMode {
147+
req.Header.Set("Content-Type", "application/octet-stream")
148+
} else {
149+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
150+
}
151+
152+
if len(*username) > 0 && len(*password) != 0 {
153+
req.SetBasicAuth(*username, *password)
154+
}
155+
156+
if *verbose {
157+
trace := &httptrace.ClientTrace{
158+
ConnectStart: func(network, addr string) {
159+
fmt.Print("Connecting to board ... ")
160+
},
161+
ConnectDone: func(network, addr string, err error) {
162+
if err != nil {
163+
fmt.Println("failed!")
164+
} else {
165+
fmt.Println(" done")
166+
}
167+
},
168+
WroteHeaders: func() {
169+
fmt.Print("Uploading sketch ... ")
170+
},
171+
WroteRequest: func(wri httptrace.WroteRequestInfo) {
172+
fmt.Println(" done")
173+
fmt.Print("Flashing sketch ... ")
174+
},
175+
GotFirstResponseByte: func() {
176+
fmt.Println(" done")
177+
},
178+
}
179+
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
180+
}
181+
182+
resp, err := httpClient.Do(req)
145183
if err != nil {
146184
if *verbose {
147185
fmt.Println("Error flashing the sketch")
@@ -170,7 +208,7 @@ func main() {
170208
fmt.Println("Resetting the board")
171209
}
172210

173-
resp, err := http.Post(httpheader+*networkAddress+":"+*networkPort+*resetEndpoint, "", nil)
211+
resp, err := httpClient.Post(httpheader+*networkAddress+":"+*networkPort+*resetEndpoint, "", nil)
174212
if err != nil {
175213
if *verbose {
176214
fmt.Println("Failed to reset the board, please reset maually")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /