Skip to content

Commit

Permalink
Merge pull request #182 from RomainBelorgey/import
Browse files Browse the repository at this point in the history
Move internal/hub to pkg/hub
  • Loading branch information
silvin-lubecki authored Sep 6, 2021
2 parents 5051946 + 6e40bf0 commit 133c07e
Show file tree
Hide file tree
Showing 41 changed files with 52 additions and 37 deletions.
2 changes: 1 addition & 1 deletion internal/commands/account/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/account/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/account/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"gotest.tools/v3/assert"
"gotest.tools/v3/golden"

"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/pkg/hub"
)

func TestInfoOutput(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/account/ratelimiting.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import (
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/credentials"
"github.com/docker/hub-tool/internal/errdef"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/login"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/org/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/org/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/format/tabwriter"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/org/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/format/tabwriter"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/org/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/format/tabwriter"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/repo/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/repo/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/format/tabwriter"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/repo/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import (
"github.com/docker/hub-tool/internal/commands/tag"
"github.com/docker/hub-tool/internal/commands/token"
"github.com/docker/hub-tool/internal/credentials"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/login"
"github.com/docker/hub-tool/pkg/hub"
)

type options struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/tag/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/tag/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import (
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/tag/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/format/tabwriter"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/tag/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"github.com/docker/distribution/reference"
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/errdef"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/token/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/token/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/token/deactivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/token/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/token/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/format"
"github.com/docker/hub-tool/internal/format/tabwriter"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/token/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"github.com/spf13/cobra"

"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/internal/metrics"
"github.com/docker/hub-tool/pkg/hub"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"github.com/docker/hub-tool/internal/ansi"
"github.com/docker/hub-tool/internal/credentials"
"github.com/docker/hub-tool/internal/errdef"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/pkg/hub"
)

// RunLogin logs the user and asks for the 2FA code if needed
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

"github.com/docker/hub-tool/internal/commands"
"github.com/docker/hub-tool/internal/credentials"
"github.com/docker/hub-tool/internal/hub"
"github.com/docker/hub-tool/pkg/hub"
)

func main() {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
37 changes: 26 additions & 11 deletions internal/hub/ratelimiting.go β†’ pkg/hub/ratelimiting.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ import (

// RateLimits ...
type RateLimits struct {
Limit *int `json:",omitempty"`
LimitWindow *int `json:",omitempty"`
Remaining *int `json:",omitempty"`
RemainingWindow *int `json:",omitempty"`
Limit *int `json:",omitempty"`
LimitWindow *int `json:",omitempty"`
Remaining *int `json:",omitempty"`
RemainingWindow *int `json:",omitempty"`
Source *string `json:",omitempty"`
}

const (
var (
first = "https://auth.docker.io/token?service=registry.docker.io&scope=repository:ratelimitpreview/test:pull"
second = "https://registry-1.docker.io/v2/ratelimitpreview/test/manifests/latest"
)

// SetURLs change the base urls used to check ratelimiting values
func SetURLs(newFirst, newSecond string) {
first = newFirst
second = newSecond
}

// GetRateLimits returns the rate limits for the authenticated user
func (c *Client) GetRateLimits() (*RateLimits, error) {
token, err := tryGetToken(c)
Expand Down Expand Up @@ -74,35 +81,43 @@ func (c *Client) GetRateLimits() (*RateLimits, error) {
return nil, err
}

source := resp.Header.Get("docker-Ratelimit-Source")

return &RateLimits{
Limit: &limit,
LimitWindow: &limitWindow,
Remaining: &remaining,
RemainingWindow: &remainingWindow,
Source: &source,
}, nil
}

func tryGetToken(c *Client) (string, error) {
token, err := c.getToken(c.password)
token, err := c.getToken("", true)
if err != nil {
token, err = c.getToken(c.refreshToken)
token, err = c.getToken(c.password, false)
if err != nil {
token, err = c.getToken(c.token)
token, err = c.getToken(c.refreshToken, false)
if err != nil {
return "", err
token, err = c.getToken(c.token, false)
if err != nil {
return "", err
}
}
}
}
return token, nil
}

func (c *Client) getToken(password string) (string, error) {
func (c *Client) getToken(password string, anonymous bool) (string, error) {
req, err := http.NewRequest("GET", first, nil)
if err != nil {
return "", err
}

req.Header.Add("Authorization", "Basic "+basicAuth(c.account, password))
if !anonymous {
req.Header.Add("Authorization", "Basic "+basicAuth(c.account, password))
}
resp, err := c.doRawRequest(req)
if err != nil {
return "", err
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 133c07e

Please sign in to comment.