Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging to Hub client #118

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: %#v should have better formatting

%v the value in a default format when printing structs, the plus flag (%+v) adds field names
%#v a Go-syntax representation of the value

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, there are annoying things with #v though like it prints pointers instead of values... e.g.:

#v: URL:(*url.URL)(0xc00012a2d0)
+v: URL:https://hub.docker.com/v2/user/

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