Repository: brooklyn-client Updated Branches: refs/heads/master 5dc3c6c26 -> 3dc6921fc
Add flags for verbose output of HTTP request data Project: http://git-wip-us.apache.org/repos/asf/brooklyn-client/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-client/commit/c88df991 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-client/tree/c88df991 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-client/diff/c88df991 Branch: refs/heads/master Commit: c88df99199dcacefb5f06faf5731e20db27d9b4d Parents: be80516 Author: Sam Corbett <[email protected]> Authored: Fri Jul 28 10:45:27 2017 +0100 Committer: Sam Corbett <[email protected]> Committed: Fri Jul 28 10:45:27 2017 +0100 ---------------------------------------------------------------------- cli/app/app.go | 8 ++++++++ cli/br/brooklyn.go | 12 +++++++++++- cli/net/net.go | 43 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 60 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-client/blob/c88df991/cli/app/app.go ---------------------------------------------------------------------- diff --git a/cli/app/app.go b/cli/app/app.go index 74627ee..20c6024 100644 --- a/cli/app/app.go +++ b/cli/app/app.go @@ -60,6 +60,14 @@ func NewApp(baseName string, cmdRunner command_runner.Runner, metadatas ...comma Name: "json, j", Usage: "Render value as json with json path selector as described at https://github.com/NodePrime/jsonpath. (Experimental, not supported on all commands at present) ", }, + cli.BoolFlag { + Name: "verbose", + Usage: "Print HTTP requests and responses", + }, + cli.BoolFlag { + Name: "vverbose", + Usage: "Print HTTP requests and responses and include body data", + }, } app.Commands = []cli.Command{} http://git-wip-us.apache.org/repos/asf/brooklyn-client/blob/c88df991/cli/br/brooklyn.go ---------------------------------------------------------------------- diff --git a/cli/br/brooklyn.go b/cli/br/brooklyn.go index 08c4473..f314235 100644 --- a/cli/br/brooklyn.go +++ b/cli/br/brooklyn.go @@ -39,7 +39,7 @@ func main() { } //target, username, password := "http://192.168.50.101:8081", "brooklyn", "Sns4Hh9j7l" - network := net.NewNetwork(target, username, password, skipSslChecks) + network := net.NewNetwork(target, username, password, skipSslChecks, verbosity(os.Args)) cmdFactory := command_factory.NewFactory(network, config) args, scope := scope.ScopeArguments(os.Args) @@ -72,6 +72,16 @@ func requiresLogin(args []string) bool { return true } +func verbosity(args []string) string { + if contains(args, "--vverbose") { + return "vverbose" + } else if contains(args, "--verbose") { + return "verbose" + } else { + return "normal" + } +} + func contains(slice []string, val string) bool { for _, a := range slice { if a == val { http://git-wip-us.apache.org/repos/asf/brooklyn-client/blob/c88df991/cli/net/net.go ---------------------------------------------------------------------- diff --git a/cli/net/net.go b/cli/net/net.go index 38f050e..3843673 100644 --- a/cli/net/net.go +++ b/cli/net/net.go @@ -22,9 +22,12 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "io" "io/ioutil" + "log" "net/http" + "net/http/httputil" "net/url" "os" "path/filepath" @@ -38,14 +41,16 @@ type Network struct { BrooklynUser string BrooklynPass string SkipSslChecks bool + Verbosity string } -func NewNetwork(brooklynUrl, brooklynUser, brooklynPass string, skipSslChecks bool) (net *Network) { +func NewNetwork(brooklynUrl, brooklynUser, brooklynPass string, skipSslChecks bool, verbose string) (net *Network) { net = new(Network) net.BrooklynUrl = brooklynUrl net.BrooklynUser = brooklynUser net.BrooklynPass = brooklynPass net.SkipSslChecks = skipSslChecks + net.Verbosity = verbose return } @@ -119,9 +124,43 @@ func (net *Network) makeClient() (*http.Client) { return client } +func debug(verbosity string, supp func(b bool) ([]byte, error)) { + writer := func(data []byte, err error) { + if err == nil { + fmt.Fprintf(os.Stderr, "%s", data) + // include newline if data doesn't have one + if data[len(data)-1] != '\n' { + fmt.Fprintln(os.Stderr, "") + } + } else { + log.Fatalf("%s\n", err) + } + } + switch verbosity { + case "verbose": + writer(supp(false)) + case "vverbose": + writer(supp(true)) + } +} + func (net *Network) SendRequestGetStatusCode(req *http.Request) ([]byte, int, error) { - client := net.makeClient() + client := net.makeClient() + debug(net.Verbosity, func (includeBody bool) ([]byte, error) { + var authHeader = req.Header.Get("Authorization") + if authHeader != "" { + req.Header.Set("Authorization", "******") + } + data, err := httputil.DumpRequestOut(req, includeBody) + if authHeader != "" { + req.Header.Set("Authorization", authHeader) + } + return data, err + }) resp, err := client.Do(req) + debug(net.Verbosity, func (includeBody bool) ([]byte, error) { + return httputil.DumpResponse(resp, includeBody) + }) if err != nil { return nil, 0, err }
