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() { func init() {
hawser.Config.OutputWriter = OutputWriter
log.SetOutput(ErrorWriter) log.SetOutput(ErrorWriter)
} }

@ -3,6 +3,7 @@ package hawser
import ( import (
"fmt" "fmt"
"github.com/hawser/git-hawser/git" "github.com/hawser/git-hawser/git"
"io"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
@ -13,6 +14,7 @@ import (
type Configuration struct { type Configuration struct {
CurrentRemote string CurrentRemote string
OutputWriter io.Writer
gitConfig map[string]string gitConfig map[string]string
remotes []string remotes []string
httpClient *http.Client httpClient *http.Client
@ -28,7 +30,10 @@ var (
) )
func NewConfig() *Configuration { 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 { if len(os.Getenv("GIT_CURL_VERBOSE")) > 0 || len(os.Getenv("GIT_HTTP_VERBOSE")) > 0 {
c.isTracingHttp = true c.isTracingHttp = true
} }

@ -74,9 +74,9 @@ func traceHttpResponse(c *Configuration, res *http.Response) {
return return
} }
fmt.Println("<", res.Proto, res.Status) fmt.Fprintf(c.OutputWriter, "< %s %s\n", res.Proto, res.Status)
for key, _ := range res.Header { 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 traceBody := false
@ -88,18 +88,19 @@ func traceHttpResponse(c *Configuration, res *http.Response) {
} }
if traceBody { if traceBody {
fmt.Println() fmt.Fprintf(c.OutputWriter, "\n")
res.Body = newTracedBody(res.Body) res.Body = newTracedBody(res.Body, c.OutputWriter)
} }
} }
type tracedBody struct { type tracedBody struct {
body io.ReadCloser body io.ReadCloser
output io.Writer
} }
func (r *tracedBody) Read(p []byte) (int, error) { func (r *tracedBody) Read(p []byte) (int, error) {
n, err := r.body.Read(p) 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 return n, err
} }
@ -107,6 +108,6 @@ func (r *tracedBody) Close() error {
return r.body.Close() return r.body.Close()
} }
func newTracedBody(body io.ReadCloser) *tracedBody { func newTracedBody(body io.ReadCloser, output io.Writer) *tracedBody {
return &tracedBody{body} return &tracedBody{body, output}
} }