Skip to content
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
13 changes: 10 additions & 3 deletions internal/commands/repo/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type rmOptions struct {
func newRmCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
var opts rmOptions
cmd := &cobra.Command{
Use: rmName + " [OPTIONS] REPOSITORY",
Use: rmName + " [OPTIONS] NAMESPACE/REPOSITORY",
Short: "Delete a repository",
Args: cli.ExactArgs(1),
DisableFlagsInUseLine: true,
Expand All @@ -73,7 +73,11 @@ func runRm(ctx context.Context, streams command.Streams, hubClient *hub.Client,
}
namedRef, ok := ref.(reference.Named)
if !ok {
return fmt.Errorf("invalid reference: repository not specified")
return errors.New("invalid reference: repository not specified")
}

if !strings.Contains(repository, "/") {
return fmt.Errorf("repository name must include username or organization name, example: hub-tool repo rm username/repository")
}

if !opts.force {
Expand Down Expand Up @@ -104,6 +108,9 @@ func runRm(ctx context.Context, streams command.Streams, hubClient *hub.Client,
if err := hubClient.RemoveRepository(namedRef.Name()); err != nil {
return err
}
fmt.Fprintln(streams.Out(), "Deleted", repository)
_, err = fmt.Fprintf(streams.Out(), "Repository %q was successfully deleted\n", repository)
if err != nil {
return err
}
return nil
}
5 changes: 5 additions & 0 deletions pkg/hub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ func (c *Client) doRequest(req *http.Request, reqOps ...RequestOp) ([]byte, erro
defer resp.Body.Close() //nolint:errcheck
}
log.Tracef("HTTP response: %+v", resp)

if resp.StatusCode == http.StatusNotFound {
return nil, &notFoundError{}
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
if resp.StatusCode == http.StatusForbidden {
return nil, &forbiddenError{}
Expand Down
12 changes: 12 additions & 0 deletions pkg/hub/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ func IsForbiddenError(err error) bool {
_, ok := err.(*forbiddenError)
return ok
}

type notFoundError struct{}

func (n notFoundError) Error() string {
return "resource not found"
}

// IsNotFoundError check if the error type is a not found error
func IsNotFoundError(err error) bool {
_, ok := err.(*notFoundError)
return ok
}
5 changes: 5 additions & 0 deletions pkg/hub/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ func TestIsForbiddenError(t *testing.T) {
assert.Assert(t, IsForbiddenError(&forbiddenError{}))
assert.Assert(t, !IsForbiddenError(errors.New("")))
}

func TestIsNotFoundError(t *testing.T) {
assert.Assert(t, IsNotFoundError(&notFoundError{}))
assert.Assert(t, !IsNotFoundError(errors.New("")))
}
18 changes: 11 additions & 7 deletions pkg/hub/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ import (
)

const (
// RepositoriesURL path to the Hub API listing the repositories
RepositoriesURL = "/v2/repositories/%s/"
// DeleteRepositoryURL path to the Hub API to remove a repository
DeleteRepositoryURL = "/v2/repositories/%s/"
// RepositoriesURL is the Hub API base URL
RepositoriesURL = "/v2/repositories/"
)

//Repository represents a Docker Hub repository
Expand All @@ -46,7 +44,8 @@ func (c *Client) GetRepositories(account string) ([]Repository, int, error) {
if account == "" {
account = c.account
}
u, err := url.Parse(c.domain + fmt.Sprintf(RepositoriesURL, account))
repositoriesURL := fmt.Sprintf("%s%s%s", c.domain, RepositoriesURL, account)
u, err := url.Parse(repositoriesURL)
if err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -77,12 +76,17 @@ func (c *Client) GetRepositories(account string) ([]Repository, int, error) {

//RemoveRepository removes a repository on Hub
func (c *Client) RemoveRepository(repository string) error {
req, err := http.NewRequest("DELETE", c.domain+fmt.Sprintf(DeleteRepositoryURL, repository), nil)
repositoryURL := fmt.Sprintf("%s%s%s/", c.domain, RepositoriesURL, repository)
req, err := http.NewRequest(http.MethodDelete, repositoryURL, nil)
if err != nil {
return err
}
_, err = c.doRequest(req, withHubToken(c.token))
return err
if err != nil {
return err
}

return nil
}

func (c *Client) getRepositoriesPage(url, account string) ([]Repository, int, string, error) {
Expand Down