Skip to content

Commit

Permalink
Merge pull request #118 from docker/logger
Browse files Browse the repository at this point in the history
Add logging to Hub client
  • Loading branch information
chris-crone authored Nov 13, 2020
2 parents 426d37f + df91019 commit 4e52218
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/miekg/pkcs11 v1.0.3 // indirect
github.com/opencontainers/image-spec v1.0.1
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208
Expand Down
14 changes: 14 additions & 0 deletions internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal"
Expand All @@ -34,6 +35,8 @@ import (

type options struct {
showVersion bool
trace bool
verbose bool
}

//NewRootCmd returns the main command
Expand All @@ -46,6 +49,14 @@ func NewRootCmd(streams command.Streams, hubClient *hub.Client, name string) *co
Annotations: map[string]string{},
SilenceUsage: true,
DisableFlagsInUseLine: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if flags.trace {
log.SetLevel(log.TraceLevel)
} else if flags.verbose {
log.SetLevel(log.DebugLevel)
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if flags.showVersion {
fmt.Fprintf(streams.Out(), "Docker Hub Tool %s, build %s\n", internal.Version, internal.GitCommit[:7])
Expand All @@ -55,6 +66,9 @@ func NewRootCmd(streams command.Streams, hubClient *hub.Client, name string) *co
},
}
cmd.Flags().BoolVar(&flags.showVersion, "version", false, "Display the version of this tool")
cmd.PersistentFlags().BoolVar(&flags.verbose, "verbose", false, "Print logs")
cmd.PersistentFlags().BoolVar(&flags.trace, "trace", false, "Print trace logs")
_ = cmd.PersistentFlags().MarkHidden("trace")

cmd.AddCommand(
account.NewAccountCmd(streams, hubClient),
Expand Down
8 changes: 6 additions & 2 deletions internal/hub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/registry"
log "github.com/sirupsen/logrus"

"github.com/docker/hub-tool/internal"
)
Expand Down Expand Up @@ -169,15 +170,19 @@ func (c *Client) doRequest(req *http.Request, reqOps ...RequestOp) ([]byte, erro
if c.Ctx != nil {
req = req.WithContext(c.Ctx)
}
log.Debugf("HTTP %s on: %s", req.Method, req.URL)
log.Tracef("HTTP request: %+v", req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.Body != nil {
defer resp.Body.Close() //nolint:errcheck
}
log.Tracef("HTTP response: %+v", resp)
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
buf, err := ioutil.ReadAll(resp.Body)
log.Debugf("bad status code %q: %s", resp.Status, buf)
if err == nil {
var body map[string]string
if err := json.Unmarshal(buf, &body); err == nil {
Expand All @@ -186,13 +191,12 @@ func (c *Client) doRequest(req *http.Request, reqOps ...RequestOp) ([]byte, erro
return nil, fmt.Errorf("bad status code %q: %s", resp.Status, msg)
}
}
// If not in our key list, print the whole JSON body
return nil, fmt.Errorf("bad status code %q: %s", resp.Status, buf)
}
}
return nil, fmt.Errorf("bad status code %q", resp.Status)
}
buf, err := ioutil.ReadAll(resp.Body)
log.Tracef("HTTP response body: %s", buf)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4e52218

Please sign in to comment.