github.com/drone/runner-go@v1.12.0/logger/dumper.go (about)

     1  // Copyright 2019 Drone.IO Inc. All rights reserved.
     2  // Use of this source code is governed by the Polyform License
     3  // that can be found in the LICENSE file.
     4  
     5  package logger
     6  
     7  import (
     8  	"io"
     9  	"net/http"
    10  	"net/http/httputil"
    11  	"os"
    12  )
    13  
    14  // Dumper dumps the http.Request and http.Response
    15  // message payload for debugging purposes.
    16  type Dumper interface {
    17  	DumpRequest(*http.Request)
    18  	DumpResponse(*http.Response)
    19  }
    20  
    21  // DiscardDumper returns a no-op dumper.
    22  func DiscardDumper() Dumper {
    23  	return new(discardDumper)
    24  }
    25  
    26  type discardDumper struct{}
    27  
    28  func (*discardDumper) DumpRequest(*http.Request)   {}
    29  func (*discardDumper) DumpResponse(*http.Response) {}
    30  
    31  // StandardDumper returns a standard dumper.
    32  func StandardDumper(body bool) Dumper {
    33  	return &standardDumper{out: os.Stdout, body: body}
    34  }
    35  
    36  type standardDumper struct {
    37  	body bool
    38  	out  io.Writer
    39  }
    40  
    41  func (s *standardDumper) DumpRequest(req *http.Request) {
    42  	dump, _ := httputil.DumpRequestOut(req, s.body)
    43  	s.out.Write(dump)
    44  }
    45  
    46  func (s *standardDumper) DumpResponse(res *http.Response) {
    47  	dump, _ := httputil.DumpResponse(res, s.body)
    48  	s.out.Write(dump)
    49  }