Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
This commit is contained in:
Silvin Lubecki 2024-02-27 16:18:20 +01:00
Родитель 1382c602c3
Коммит 7f68742f5d
25 изменённых файлов: 70 добавлений и 72 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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
)

Просмотреть файл

@ -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,

Просмотреть файл

@ -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,

Просмотреть файл

@ -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,

Просмотреть файл

@ -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"

Просмотреть файл

@ -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,

Просмотреть файл

@ -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

Просмотреть файл

@ -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"

Просмотреть файл

@ -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 "":

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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
}

Просмотреть файл

@ -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 (

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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)

Просмотреть файл

@ -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 {

Просмотреть файл

@ -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)