From bba0738092c15122477e9abdbf8f0ff2b376891d Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Thu, 5 Mar 2015 13:04:00 -0700 Subject: [PATCH] let the commands package tell the hawser package where to send output --- commands/commands.go | 1 + hawser/config.go | 7 ++++++- hawser/http.go | 17 +++++++++-------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index 2e271bfc..ffb17784 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -163,5 +163,6 @@ type ErrorWithStack interface { } func init() { + hawser.Config.OutputWriter = OutputWriter log.SetOutput(ErrorWriter) } diff --git a/hawser/config.go b/hawser/config.go index 32274ac6..c13fe94a 100644 --- a/hawser/config.go +++ b/hawser/config.go @@ -3,6 +3,7 @@ package hawser import ( "fmt" "github.com/hawser/git-hawser/git" + "io" "net/http" "net/url" "os" @@ -13,6 +14,7 @@ import ( type Configuration struct { CurrentRemote string + OutputWriter io.Writer gitConfig map[string]string remotes []string httpClient *http.Client @@ -28,7 +30,10 @@ var ( ) func NewConfig() *Configuration { - c := &Configuration{CurrentRemote: defaultRemote} + c := &Configuration{ + CurrentRemote: defaultRemote, + OutputWriter: os.Stdout, + } if len(os.Getenv("GIT_CURL_VERBOSE")) > 0 || len(os.Getenv("GIT_HTTP_VERBOSE")) > 0 { c.isTracingHttp = true } diff --git a/hawser/http.go b/hawser/http.go index 2ed3ad39..68494563 100644 --- a/hawser/http.go +++ b/hawser/http.go @@ -74,9 +74,9 @@ func traceHttpResponse(c *Configuration, res *http.Response) { return } - fmt.Println("<", res.Proto, res.Status) + fmt.Fprintf(c.OutputWriter, "< %s %s\n", res.Proto, res.Status) for key, _ := range res.Header { - fmt.Printf("< %s: %s\n", key, res.Header.Get(key)) + fmt.Fprintf(c.OutputWriter, "< %s: %s\n", key, res.Header.Get(key)) } traceBody := false @@ -88,18 +88,19 @@ func traceHttpResponse(c *Configuration, res *http.Response) { } if traceBody { - fmt.Println() - res.Body = newTracedBody(res.Body) + fmt.Fprintf(c.OutputWriter, "\n") + res.Body = newTracedBody(res.Body, c.OutputWriter) } } type tracedBody struct { - body io.ReadCloser + body io.ReadCloser + output io.Writer } func (r *tracedBody) Read(p []byte) (int, error) { n, err := r.body.Read(p) - fmt.Println(string(p[0:n])) + fmt.Fprintf(r.output, "%s\n", string(p[0:n])) return n, err } @@ -107,6 +108,6 @@ func (r *tracedBody) Close() error { return r.body.Close() } -func newTracedBody(body io.ReadCloser) *tracedBody { - return &tracedBody{body} +func newTracedBody(body io.ReadCloser, output io.Writer) *tracedBody { + return &tracedBody{body, output} }