From 53ec1b496c3800ad2646e918d94e5a1e18a6c8cd Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 18 Feb 2014 21:24:52 -0800 Subject: [PATCH] camput: move debug flag registration to its own func, for use by tests later. Also, don't call TransportForConfig on client when we're in blobdir mode. Avoids a warning. Change-Id: I5d618b7ab7d701b4c9d9086c8adedef7b447f62b --- cmd/camput/camput.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/cmd/camput/camput.go b/cmd/camput/camput.go index 53a227ba8..8a23eb2cf 100644 --- a/cmd/camput/camput.go +++ b/cmd/camput/camput.go @@ -48,10 +48,16 @@ var ( uploader *Uploader // initialized by getUploader ) +var debugFlagOnce sync.Once + +func registerDebugFlags() { + flag.BoolVar(&flagProxyLocal, "proxy_local", false, "If true, the HTTP_PROXY environment is also used for localhost requests. This can be helpful during debugging.") + flag.BoolVar(&flagHaveCache, "havecache", true, "Use the 'have cache', a cache keeping track of what blobs the remote server should already have from previous uploads.") +} + func init() { if debug, _ := strconv.ParseBool(os.Getenv("CAMLI_DEBUG")); debug { - flag.BoolVar(&flagProxyLocal, "proxy_local", false, "If true, the HTTP_PROXY environment is also used for localhost requests. This can be helpful during debugging.") - flag.BoolVar(&flagHaveCache, "havecache", true, "Use the 'have cache', a cache keeping track of what blobs the remote server should already have from previous uploads.") + debugFlagOnce.Do(registerDebugFlags) } cmdmain.ExtraFlagRegistration = func() { client.AddFlags() @@ -125,6 +131,7 @@ func proxyFromEnvironment(req *http.Request) (*url.URL, error) { func newUploader() *Uploader { var cc *client.Client + var httpStats *httputil.StatsTransport if d := *flagBlobDir; d != "" { ss, err := dir.New(d) if err != nil { @@ -133,23 +140,22 @@ func newUploader() *Uploader { cc = client.NewStorageClient(ss) } else { cc = client.NewOrFail() + proxy := http.ProxyFromEnvironment + if flagProxyLocal { + proxy = proxyFromEnvironment + } + tr := cc.TransportForConfig( + &client.TransportConfig{ + Proxy: proxy, + Verbose: *flagHTTP, + }) + httpStats, _ = tr.(*httputil.StatsTransport) + cc.SetHTTPClient(&http.Client{Transport: tr}) } if !*cmdmain.FlagVerbose { cc.SetLogger(nil) } - proxy := http.ProxyFromEnvironment - if flagProxyLocal { - proxy = proxyFromEnvironment - } - tr := cc.TransportForConfig( - &client.TransportConfig{ - Proxy: proxy, - Verbose: *flagHTTP, - }) - httpStats, _ := tr.(*httputil.StatsTransport) - cc.SetHTTPClient(&http.Client{Transport: tr}) - pwd, err := os.Getwd() if err != nil { log.Fatalf("os.Getwd: %v", err)