Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(245)

Delta Between Two Patch Sets: gotour/local.go

Issue 6945074: code review 6945074: gotour: automatically open browser window if possible (Closed)
Left Patch Set: diff -r 35aa8dc3dadc https://code.google.com/p/go-tour/ Created 11 years, 3 months ago
Right Patch Set: diff -r 35aa8dc3dadc https://code.google.com/p/go-tour/ Created 11 years, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2011 The Go Authors. All rights reserved. 1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 package main 5 package main
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "flag" 9 "flag"
10 "fmt" 10 "fmt"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 if err != nil { 74 if err != nil {
75 log.Fatal(err) 75 log.Fatal(err)
76 } 76 }
77 if host == "" { 77 if host == "" {
78 host = "localhost" 78 host = "localhost"
79 } 79 }
80 if host != "127.0.0.1" && host != "localhost" { 80 if host != "127.0.0.1" && host != "localhost" {
81 log.Print(localhostWarning) 81 log.Print(localhostWarning)
82 } 82 }
83 83
84 » go waitAndStartBrowser("http://" + host + ":" + port) 84 » httpAddr := host + ":" + port
adg 2012/12/19 00:28:01 this is kind of nitpicky, but I would prefer these
85 » log.Fatal(http.ListenAndServe(*httpListen, nil)) 85 » go func() {
86 » » url := "http://" + httpAddr
87 » » if waitServer(url) && *openBrowser && startBrowser(url) {
88 » » » log.Printf("A browser window should open. If not, please visit %s", url)
89 » » } else {
90 » » » log.Printf("Please open your web browser and visit %s", url)
91 » » }
92 » }()
93 » log.Fatal(http.ListenAndServe(httpAddr, nil))
86 } 94 }
87 95
88 const localhostWarning = ` 96 const localhostWarning = `
89 WARNING! WARNING! WARNING! 97 WARNING! WARNING! WARNING!
90 98
91 I appear to be listening on an address that is not localhost. 99 I appear to be listening on an address that is not localhost.
92 Anyone with access to this address and port will have access 100 Anyone with access to this address and port will have access
93 to this machine as the user running gotour. 101 to this machine as the user running gotour.
94 102
95 If you don't understand this message, hit Control-C to terminate this process. 103 If you don't understand this message, hit Control-C to terminate this process.
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 // Wait for the command. Clean up, 200 // Wait for the command. Clean up,
193 err := cmd.Wait() 201 err := cmd.Wait()
194 running.Lock() 202 running.Lock()
195 if running.cmd == cmd { 203 if running.cmd == cmd {
196 running.cmd = nil 204 running.cmd = nil
197 } 205 }
198 running.Unlock() 206 running.Unlock()
199 return buf.Bytes(), err 207 return buf.Bytes(), err
200 } 208 }
201 209
202 // waitAndStartBrowser tries to open the URL in a browser after waiting for 210 // waitServer waits some time for the http Server to start
203 // the http server starts 211 // serving url and returns whether it starts
204 func waitAndStartBrowser(url string) { 212 func waitServer(url string) bool {
adg 2012/12/19 00:28:01 return a bool
205 » // wait for the server start 213 » tries := 20
206 » tries := 100
adg 2012/12/19 00:28:01 10 seconds seems a long time to wait. reduce to 20
207 for tries > 0 { 214 for tries > 0 {
208 resp, err := http.Get(url) 215 resp, err := http.Get(url)
209 if err == nil { 216 if err == nil {
210 resp.Body.Close() 217 resp.Body.Close()
211 » » » break 218 » » » return true
212 } 219 }
213 time.Sleep(100 * time.Millisecond) 220 time.Sleep(100 * time.Millisecond)
214 tries-- 221 tries--
215 } 222 }
216 » if tries > 0 && *openBrowser && startBrowser(url) == nil { 223 » return false
217 » » log.Printf("A browser window should open. If not, please visit % s", url) 224 }
218 » } else { 225
219 » » log.Printf("Please open your web browser and visit %s", url) 226 // startBrowser tries to open the URL in a browser, and returns
220 » } 227 // whether it succeed.
221 } 228 func startBrowser(url string) bool {
222
223 // startBrowser tries to open the URL in a browser
224 func startBrowser(url string) error {
adg 2012/12/19 00:28:01 we don't use the error, so just return a bool
225 // try to start the browser 229 // try to start the browser
226 var args []string 230 var args []string
227 switch runtime.GOOS { 231 switch runtime.GOOS {
228 case "darwin": 232 case "darwin":
229 args = []string{"open"} 233 args = []string{"open"}
230 case "windows": 234 case "windows":
231 args = []string{"cmd", "/c", "start"} 235 args = []string{"cmd", "/c", "start"}
232 default: 236 default:
233 args = []string{"xdg-open"} 237 args = []string{"xdg-open"}
234 } 238 }
235 cmd := exec.Command(args[0], append(args[1:], url)...) 239 cmd := exec.Command(args[0], append(args[1:], url)...)
236 » return cmd.Start() 240 » return cmd.Start() == nil
237 } 241 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b