Skip to content

Commit

Permalink
Color the headers returned via --verbose.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Aug 12, 2023
1 parent fcdccb0 commit a9a5c54
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
32 changes: 31 additions & 1 deletion broom.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import (
"fmt"
"io"
"net/http"
"net/textproto"
"os"
"os/exec"
"slices"
"strings"

"github.com/fatih/color"
strip "github.com/grokify/html-strip-tags-go"
"github.com/tidwall/pretty"
"golang.org/x/net/http/httpguts"
)

// Version is the current version, replaced at build time.
Expand Down Expand Up @@ -90,7 +93,7 @@ func Execute(req *http.Request, verbose bool) (Result, error) {
if verbose {
sb.WriteString(resp.Status)
sb.WriteByte('\n')
resp.Header.WriteSubset(&sb, nil)
writeHeaders(&sb, resp.Header, nil)
sb.WriteByte('\n')
}
if IsJSON(resp.Header.Get("Content-Type")) {
Expand Down Expand Up @@ -147,3 +150,30 @@ func RunCommand(command string) (string, error) {
func Sanitize(s string) string {
return strings.Trim(strip.StripTags(s), "\n")
}

// writeHeaders writes sorted, colored headers to the given writer.
func writeHeaders(w io.StringWriter, headers http.Header, exclude []string) {
keys := make([]string, 0, len(headers))
for key := range headers {
if !slices.Contains(exclude, key) {
keys = append(keys, key)
}
}
slices.Sort(keys)

headerNewlineToSpace := strings.NewReplacer("\n", " ", "\r", " ")
for _, key := range keys {
if !httpguts.ValidHeaderFieldName(key) {
// Drop invalid headers the same way http.Header.WriteSubset() does.
continue
}
for _, v := range headers[key] {
v = headerNewlineToSpace.Replace(v)
v = textproto.TrimString(v)

w.WriteString(color.YellowString("%s: ", key))
w.WriteString(v)
w.WriteString("\n")
}
}
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/iancoleman/strcase v0.3.0
github.com/spf13/pflag v1.0.5
github.com/tidwall/pretty v1.2.1
golang.org/x/net v0.14.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -25,6 +26,7 @@ require (
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down

0 comments on commit a9a5c54

Please sign in to comment.