From 7f68742f5d011f3c55f21a0824586603206e3f95 Mon Sep 17 00:00:00 2001 From: Silvin Lubecki Date: Tue, 27 Feb 2024 16:18:20 +0100 Subject: [PATCH] make linter happy Signed-off-by: Silvin Lubecki --- Dockerfile | 2 +- e2e/helper_test.go | 5 ++--- internal/ansi/ansi.go | 30 +++++++++++++++--------------- internal/commands/account/cmd.go | 2 +- internal/commands/org/cmd.go | 2 +- internal/commands/repo/cmd.go | 2 +- internal/commands/repo/rm.go | 2 +- internal/commands/tag/cmd.go | 2 +- internal/commands/tag/inspect.go | 6 +++--- internal/commands/tag/rm.go | 2 +- internal/format/json.go | 8 ++++---- main.go | 2 +- pkg/hub/client.go | 9 ++++----- pkg/hub/consumption.go | 6 +++--- pkg/hub/instances.go | 2 +- pkg/hub/members.go | 4 ++-- pkg/hub/organizations.go | 6 +++--- pkg/hub/plan.go | 6 +++--- pkg/hub/ratelimiting.go | 4 ++-- pkg/hub/repositories.go | 8 ++++---- pkg/hub/tags.go | 10 +++++----- pkg/hub/teams.go | 6 +++--- pkg/hub/tokens.go | 8 ++++---- pkg/hub/user.go | 4 ++-- vars.mk | 4 ++-- 25 files changed, 70 insertions(+), 72 deletions(-) diff --git a/Dockerfile b/Dockerfile index fb86876..0b24e66 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,7 +19,7 @@ ARG GO_VERSION=1.22.0-alpine3.19 ARG CLI_VERSION=20.10.2 ARG ALPINE_VERSION=3.19.0 -ARG GOLANGCI_LINT_VERSION=v1.33.0-alpine +ARG GOLANGCI_LINT_VERSION=v1.56.2-alpine #### # BUILDER diff --git a/e2e/helper_test.go b/e2e/helper_test.go index 36f5efb..e0ead4b 100644 --- a/e2e/helper_test.go +++ b/e2e/helper_test.go @@ -26,7 +26,6 @@ import ( "github.com/docker/cli/cli/config/configfile" clitypes "github.com/docker/cli/cli/config/types" "gotest.tools/v3/assert" - "gotest.tools/v3/env" "gotest.tools/v3/fs" "gotest.tools/v3/icmd" ) @@ -48,10 +47,10 @@ func hubToolCmd(t *testing.T, args ...string) (icmd.Cmd, func()) { assert.NilError(t, err) hubTool := os.Getenv("BINARY") configDir := fs.NewDir(t, t.Name(), fs.WithFile("config.json", string(data))) - cleanup := env.Patch(t, "PATH", os.Getenv("PATH")+getPathSeparator()+filepath.Join(pwd, "..", "bin")) + t.Setenv("PATH", os.Getenv("PATH")+getPathSeparator()+filepath.Join(pwd, "..", "bin")) env := append(os.Environ(), "DOCKER_CONFIG="+configDir.Path()) - return icmd.Cmd{Command: append([]string{hubTool}, args...), Env: env}, func() { cleanup(); configDir.Remove() } + return icmd.Cmd{Command: append([]string{hubTool}, args...), Env: env}, func() { configDir.Remove() } } func getPathSeparator() string { diff --git a/internal/ansi/ansi.go b/internal/ansi/ansi.go index 2d61025..25342dd 100644 --- a/internal/ansi/ansi.go +++ b/internal/ansi/ansi.go @@ -27,10 +27,10 @@ import ( var ( // Outputs ANSI color if stdout is a tty - Red = makeColorFunc("red") - Yellow = makeColorFunc("yellow") - Blue = makeColorFunc("blue") - Green = makeColorFunc("green") + red = makeColorFunc("red") + yellow = makeColorFunc("yellow") + blue = makeColorFunc("blue") + green = makeColorFunc("green") ) func makeColorFunc(color string) func(string) string { @@ -56,32 +56,32 @@ func isColorEnabled() bool { } // TODO ignores cmd.OutOrStdout - return IsTerminal(os.Stdout) + return isTerminal(os.Stdout) } -var IsTerminal = func(f *os.File) bool { - return isatty.IsTerminal(f.Fd()) || IsCygwinTerminal(f) +var isTerminal = func(f *os.File) bool { + return isatty.IsTerminal(f.Fd()) || isCygwinTerminal(f) } -func IsCygwinTerminal(f *os.File) bool { +func isCygwinTerminal(f *os.File) bool { return isatty.IsCygwinTerminal(f.Fd()) } var ( // Title color should be used for any important title - Title = Green + Title = green // Header color should be used for all the listing column headers - Header = Blue + Header = blue // Key color should be used for all key title content - Key = Blue + Key = blue // Info color should be used when we prompt an info - Info = Blue + Info = blue // Warn color should be used when we warn the user - Warn = Yellow + Warn = yellow // Error color should be used when something bad happened - Error = Red + Error = red // Emphasise color should be used with important content - Emphasise = Green + Emphasise = green // NoColor doesn't add any colors to the output NoColor = noop ) diff --git a/internal/commands/account/cmd.go b/internal/commands/account/cmd.go index 8b44c5b..f67e6b0 100644 --- a/internal/commands/account/cmd.go +++ b/internal/commands/account/cmd.go @@ -28,7 +28,7 @@ const ( accountName = "account" ) -//NewAccountCmd configures the org manage command +// NewAccountCmd configures the org manage command func NewAccountCmd(streams command.Streams, hubClient *hub.Client) *cobra.Command { cmd := &cobra.Command{ Use: accountName, diff --git a/internal/commands/org/cmd.go b/internal/commands/org/cmd.go index da510c6..3d20a76 100644 --- a/internal/commands/org/cmd.go +++ b/internal/commands/org/cmd.go @@ -28,7 +28,7 @@ const ( orgName = "org" ) -//NewOrgCmd configures the org manage command +// NewOrgCmd configures the org manage command func NewOrgCmd(streams command.Streams, hubClient *hub.Client) *cobra.Command { cmd := &cobra.Command{ Use: orgName, diff --git a/internal/commands/repo/cmd.go b/internal/commands/repo/cmd.go index 6df1f40..5a7b096 100644 --- a/internal/commands/repo/cmd.go +++ b/internal/commands/repo/cmd.go @@ -28,7 +28,7 @@ const ( repoName = "repo" ) -//NewRepoCmd configures the repo manage command +// NewRepoCmd configures the repo manage command func NewRepoCmd(streams command.Streams, hubClient *hub.Client) *cobra.Command { cmd := &cobra.Command{ Use: repoName, diff --git a/internal/commands/repo/rm.go b/internal/commands/repo/rm.go index 7fe558e..3c92c72 100644 --- a/internal/commands/repo/rm.go +++ b/internal/commands/repo/rm.go @@ -26,9 +26,9 @@ import ( "github.com/docker/hub-tool/internal/errdef" + "github.com/distribution/reference" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" - "github.com/docker/distribution/reference" "github.com/spf13/cobra" "github.com/docker/hub-tool/internal/ansi" diff --git a/internal/commands/tag/cmd.go b/internal/commands/tag/cmd.go index 2fef79b..1d3cac8 100644 --- a/internal/commands/tag/cmd.go +++ b/internal/commands/tag/cmd.go @@ -28,7 +28,7 @@ const ( tagName = "tag" ) -//NewTagCmd configures the tag manage command +// NewTagCmd configures the tag manage command func NewTagCmd(streams command.Streams, hubClient *hub.Client) *cobra.Command { cmd := &cobra.Command{ Use: tagName, diff --git a/internal/commands/tag/inspect.go b/internal/commands/tag/inspect.go index bb5024c..e2e1de7 100644 --- a/internal/commands/tag/inspect.go +++ b/internal/commands/tag/inspect.go @@ -31,9 +31,9 @@ import ( "github.com/containerd/containerd/platforms" "github.com/containerd/containerd/remotes" "github.com/containerd/containerd/remotes/docker" + "github.com/distribution/reference" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" - "github.com/docker/distribution/reference" "github.com/docker/go-units" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/spf13/cobra" @@ -52,7 +52,7 @@ type inspectOptions struct { platform string } -//Image is the combination of a manifest and its config object +// Image is the combination of a manifest and its config object type Image struct { Name string Manifest ocispec.Manifest @@ -60,7 +60,7 @@ type Image struct { Descriptor ocispec.Descriptor } -//Index is the combination of an OCI index and its descriptor +// Index is the combination of an OCI index and its descriptor type Index struct { Name string Index ocispec.Index diff --git a/internal/commands/tag/rm.go b/internal/commands/tag/rm.go index bb156bf..2c0c185 100644 --- a/internal/commands/tag/rm.go +++ b/internal/commands/tag/rm.go @@ -22,9 +22,9 @@ import ( "fmt" "strings" + "github.com/distribution/reference" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" - "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/metrics" diff --git a/internal/format/json.go b/internal/format/json.go index 5a533cd..8448687 100644 --- a/internal/format/json.go +++ b/internal/format/json.go @@ -24,20 +24,20 @@ import ( "github.com/spf13/pflag" ) -//Option handles format flags and printing the values depending the format +// Option handles format flags and printing the values depending the format type Option struct { format string } -//PrettyPrinter prints all the values in a pretty print format +// PrettyPrinter prints all the values in a pretty print format type PrettyPrinter func(io.Writer, interface{}) error -//AddFormatFlag add the format flag to a command +// AddFormatFlag add the format flag to a command func (o *Option) AddFormatFlag(flags *pflag.FlagSet) { flags.StringVar(&o.format, "format", "", `Print values using a custom format ("json")`) } -//Print outputs values depending the given format +// Print outputs values depending the given format func (o *Option) Print(out io.Writer, values interface{}, prettyPrinter PrettyPrinter) error { switch o.format { case "": diff --git a/main.go b/main.go index 50f4679..b80aa93 100644 --- a/main.go +++ b/main.go @@ -77,7 +77,7 @@ func main() { func newSigContext() (context.Context, func()) { ctx, cancel := context.WithCancel(context.Background()) - s := make(chan os.Signal) + s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGTERM, syscall.SIGINT) go func() { <-s diff --git a/pkg/hub/client.go b/pkg/hub/client.go index 51f0fd2..4e97c01 100644 --- a/pkg/hub/client.go +++ b/pkg/hub/client.go @@ -22,7 +22,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/url" @@ -225,7 +224,7 @@ func (c *Client) Login(username string, password string, twoFactorCodeProvider f return "", "", err } defer func() { _ = resp.Body.Close() }() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { return "", "", err } @@ -282,7 +281,7 @@ func (c *Client) getTwoFactorToken(token string, twoFactorCodeProvider func() (s } defer func() { _ = resp.Body.Close() }() - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { return "", "", err } @@ -320,7 +319,7 @@ func (c *Client) doRequest(req *http.Request, reqOps ...RequestOp) ([]byte, erro if resp.StatusCode == http.StatusForbidden { return nil, &forbiddenError{} } - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) log.Debugf("bad status code %q: %s", resp.Status, buf) if err == nil { if ok, err := extractError(buf, resp); ok { @@ -329,7 +328,7 @@ func (c *Client) doRequest(req *http.Request, reqOps ...RequestOp) ([]byte, erro } return nil, fmt.Errorf("bad status code %q", resp.Status) } - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) log.Tracef("HTTP response body: %s", buf) if err != nil { return nil, err diff --git a/pkg/hub/consumption.go b/pkg/hub/consumption.go index f58ccb9..e5cadd6 100644 --- a/pkg/hub/consumption.go +++ b/pkg/hub/consumption.go @@ -22,14 +22,14 @@ import ( "golang.org/x/sync/errgroup" ) -//Consumption represents current user or org consumption +// Consumption represents current user or org consumption type Consumption struct { Seats int PrivateRepositories int Teams int } -//GetOrgConsumption return the current organization consumption +// GetOrgConsumption return the current organization consumption func (c *Client) GetOrgConsumption(org string) (*Consumption, error) { var ( members int @@ -78,7 +78,7 @@ func (c *Client) GetOrgConsumption(org string) (*Consumption, error) { }, nil } -//GetUserConsumption return the current user consumption +// GetUserConsumption return the current user consumption func (c *Client) GetUserConsumption(user string) (*Consumption, error) { c.fetchAllElements = true privateRepos := 0 diff --git a/pkg/hub/instances.go b/pkg/hub/instances.go index c802511..7225871 100644 --- a/pkg/hub/instances.go +++ b/pkg/hub/instances.go @@ -22,7 +22,7 @@ import ( "github.com/docker/docker/api/types/registry" ) -//Instance stores all the specific pieces needed to dialog with Hub +// Instance stores all the specific pieces needed to dialog with Hub type Instance struct { APIHubBaseURL string RegistryInfo *registry.IndexInfo diff --git a/pkg/hub/members.go b/pkg/hub/members.go index 4570689..f2d5c37 100644 --- a/pkg/hub/members.go +++ b/pkg/hub/members.go @@ -31,13 +31,13 @@ const ( MembersPerTeamURL = "/v2/orgs/%s/groups/%s/members/" ) -//Member is a user part of an organization +// Member is a user part of an organization type Member struct { Username string `json:"username"` FullName string `json:"full_name"` } -//GetMembers lists all the members in an organization +// GetMembers lists all the members in an organization func (c *Client) GetMembers(organization string) ([]Member, error) { u, err := url.Parse(c.domain + fmt.Sprintf(MembersURL, organization)) if err != nil { diff --git a/pkg/hub/organizations.go b/pkg/hub/organizations.go index 6d255e5..321a413 100644 --- a/pkg/hub/organizations.go +++ b/pkg/hub/organizations.go @@ -35,7 +35,7 @@ const ( OrganizationInfoURL = "/v2/orgs/%s" ) -//Organization represents a Docker Hub organization +// Organization represents a Docker Hub organization type Organization struct { Namespace string FullName string @@ -44,7 +44,7 @@ type Organization struct { Members []Member } -//GetOrganizations lists all the organizations a user has joined +// GetOrganizations lists all the organizations a user has joined func (c *Client) GetOrganizations(ctx context.Context) ([]Organization, error) { u, err := url.Parse(c.domain + OrganizationsURL) if err != nil { @@ -72,7 +72,7 @@ func (c *Client) GetOrganizations(ctx context.Context) ([]Organization, error) { return organizations, nil } -//GetOrganizationInfo returns organization info +// GetOrganizationInfo returns organization info func (c *Client) GetOrganizationInfo(orgname string) (*Account, error) { u, err := url.Parse(c.domain + fmt.Sprintf(OrganizationInfoURL, orgname)) if err != nil { diff --git a/pkg/hub/plan.go b/pkg/hub/plan.go index adcadf1..918c1cb 100644 --- a/pkg/hub/plan.go +++ b/pkg/hub/plan.go @@ -34,13 +34,13 @@ const ( FreePlan = "free" ) -//Plan represents the current account Hub plan +// Plan represents the current account Hub plan type Plan struct { Name string Limits Limits } -//Limits represents the current account limits +// Limits represents the current account limits type Limits struct { Seats int PrivateRepos int @@ -49,7 +49,7 @@ type Limits struct { ParallelBuilds int } -//GetHubPlan returns an account current Hub plan +// GetHubPlan returns an account current Hub plan func (c *Client) GetHubPlan(accountID string) (*Plan, error) { u, err := url.Parse(c.domain + fmt.Sprintf(HubPlanURL, accountID)) if err != nil { diff --git a/pkg/hub/ratelimiting.go b/pkg/hub/ratelimiting.go index 211ba70..7290e9a 100644 --- a/pkg/hub/ratelimiting.go +++ b/pkg/hub/ratelimiting.go @@ -21,7 +21,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "strconv" "strings" @@ -136,7 +136,7 @@ func (c *Client) getToken(password string, anonymous bool) (string, error) { return "", errors.New("unable to get authorization token") } - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { return "", err } diff --git a/pkg/hub/repositories.go b/pkg/hub/repositories.go index 30ff298..a3e2208 100644 --- a/pkg/hub/repositories.go +++ b/pkg/hub/repositories.go @@ -29,7 +29,7 @@ const ( RepositoriesURL = "/v2/repositories/" ) -//Repository represents a Docker Hub repository +// Repository represents a Docker Hub repository type Repository struct { Name string Description string @@ -39,7 +39,7 @@ type Repository struct { IsPrivate bool } -//GetRepositories lists all the repositories a user can access +// GetRepositories lists all the repositories a user can access func (c *Client) GetRepositories(account string) ([]Repository, int, error) { if account == "" { account = c.account @@ -74,7 +74,7 @@ func (c *Client) GetRepositories(account string) ([]Repository, int, error) { return repos, total, nil } -//RemoveRepository removes a repository on Hub +// RemoveRepository removes a repository on Hub func (c *Client) RemoveRepository(repository string) error { repositoryURL := fmt.Sprintf("%s%s%s/", c.domain, RepositoriesURL, repository) req, err := http.NewRequest(http.MethodDelete, repositoryURL, nil) @@ -140,7 +140,7 @@ type hubRepositoryResult struct { User string `json:"user"` } -//RepositoryType lists all the different repository types handled by the Docker Hub +// RepositoryType lists all the different repository types handled by the Docker Hub type RepositoryType string const ( diff --git a/pkg/hub/tags.go b/pkg/hub/tags.go index d59c879..922fbb9 100644 --- a/pkg/hub/tags.go +++ b/pkg/hub/tags.go @@ -23,7 +23,7 @@ import ( "net/url" "time" - "github.com/docker/distribution/reference" + "github.com/distribution/reference" ) const ( @@ -33,7 +33,7 @@ const ( DeleteTagURL = "/v2/repositories/%s/tags/%s/" ) -//Tag can point to a manifest or manifest list +// Tag can point to a manifest or manifest list type Tag struct { Name string FullSize int @@ -45,7 +45,7 @@ type Tag struct { Status string } -//Image represents the metadata of a manifest +// Image represents the metadata of a manifest type Image struct { Digest string Architecture string @@ -57,7 +57,7 @@ type Image struct { Status string } -//GetTags calls the hub repo API and returns all the information on all tags +// GetTags calls the hub repo API and returns all the information on all tags func (c *Client) GetTags(repository string, reqOps ...RequestOp) ([]Tag, int, error) { repoPath, err := getRepoPath(repository) if err != nil { @@ -90,7 +90,7 @@ func (c *Client) GetTags(repository string, reqOps ...RequestOp) ([]Tag, int, er return tags, total, nil } -//RemoveTag removes a tag in a repository on Hub +// RemoveTag removes a tag in a repository on Hub func (c *Client) RemoveTag(repository, tag string) error { req, err := http.NewRequest("DELETE", c.domain+fmt.Sprintf(DeleteTagURL, repository, tag), nil) if err != nil { diff --git a/pkg/hub/teams.go b/pkg/hub/teams.go index 95091f0..a459ce6 100644 --- a/pkg/hub/teams.go +++ b/pkg/hub/teams.go @@ -32,14 +32,14 @@ const ( GroupsURL = "/v2/orgs/%s/groups/" ) -//Team represents a hub group in an organization +// Team represents a hub group in an organization type Team struct { Name string Description string Members []Member } -//GetTeams lists all the teams in an organization +// GetTeams lists all the teams in an organization func (c *Client) GetTeams(organization string) ([]Team, error) { u, err := url.Parse(c.domain + fmt.Sprintf(GroupsURL, organization)) if err != nil { @@ -67,7 +67,7 @@ func (c *Client) GetTeams(organization string) ([]Team, error) { return teams, nil } -//GetTeamsCount returns the number of teams in an organization +// GetTeamsCount returns the number of teams in an organization func (c *Client) GetTeamsCount(organization string) (int, error) { u, err := url.Parse(c.domain + fmt.Sprintf(GroupsURL, organization)) if err != nil { diff --git a/pkg/hub/tokens.go b/pkg/hub/tokens.go index a283de1..45b71f4 100644 --- a/pkg/hub/tokens.go +++ b/pkg/hub/tokens.go @@ -34,7 +34,7 @@ const ( TokenURL = "/v2/api_tokens/%s" ) -//Token is a personal access token. The token field will only be filled at creation and can never been accessed again. +// Token is a personal access token. The token field will only be filled at creation and can never been accessed again. type Token struct { UUID uuid.UUID ClientID string @@ -74,7 +74,7 @@ func (c *Client) CreateToken(description string) (*Token, error) { return &token, nil } -//GetTokens calls the hub repo API and returns all the information on all tokens +// GetTokens calls the hub repo API and returns all the information on all tokens func (c *Client) GetTokens() ([]Token, int, error) { u, err := url.Parse(c.domain + TokensURL) if err != nil { @@ -103,7 +103,7 @@ func (c *Client) GetTokens() ([]Token, int, error) { return tokens, total, nil } -//GetToken calls the hub repo API and returns the information on one token +// GetToken calls the hub repo API and returns the information on one token func (c *Client) GetToken(tokenUUID string) (*Token, error) { req, err := http.NewRequest("GET", c.domain+fmt.Sprintf(TokenURL, tokenUUID), nil) if err != nil { @@ -154,7 +154,7 @@ func (c *Client) UpdateToken(tokenUUID, description string, isActive bool) (*Tok return &token, nil } -//RemoveToken deletes a token from personal access token +// RemoveToken deletes a token from personal access token func (c *Client) RemoveToken(tokenUUID string) error { //DELETE https://hub.docker.com/v2/api_tokens/8208674e-d08a-426f-b6f4-e3aba7058459 => 202 req, err := http.NewRequest("DELETE", c.domain+fmt.Sprintf(TokenURL, tokenUUID), nil) diff --git a/pkg/hub/user.go b/pkg/hub/user.go index c1b1b06..f35aa5f 100644 --- a/pkg/hub/user.go +++ b/pkg/hub/user.go @@ -28,7 +28,7 @@ const ( UserURL = "/v2/user/" ) -//Account represents a user or organization information +// Account represents a user or organization information type Account struct { ID string Name string @@ -38,7 +38,7 @@ type Account struct { Joined time.Time } -//GetUserInfo returns the information on the user retrieved from Hub +// GetUserInfo returns the information on the user retrieved from Hub func (c *Client) GetUserInfo() (*Account, error) { u, err := url.Parse(c.domain + UserURL) if err != nil { diff --git a/vars.mk b/vars.mk index a66d669..708f6f5 100644 --- a/vars.mk +++ b/vars.mk @@ -16,8 +16,8 @@ GO_VERSION=1.22.0-alpine3.19 CLI_VERSION=20.10.2 ALPINE_VERSION=3.12.2 -GOLANGCI_LINT_VERSION=v1.33.0-alpine -GOTESTSUM_VERSION=0.6.0 +GOLANGCI_LINT_VERSION=v1.56.2-alpine +GOTESTSUM_VERSION=1.11.0 GOOS?=$(shell go env GOOS) GOARCH?=$(shell go env GOARCH)