let the commands package tell the hawser package where to send output

This commit is contained in:
Rick Olson 2015-03-05 13:04:00 -07:00
parent abcb4bac69
commit bba0738092
3 changed files with 16 additions and 9 deletions

@ -163,5 +163,6 @@ type ErrorWithStack interface {
}
func init() {
hawser.Config.OutputWriter = OutputWriter
log.SetOutput(ErrorWriter)
}

@ -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
}

@ -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}
}