Skip to content

Commit 89d966e

Browse files
Merge pull request #203 from converge/fix_178_issue
Fixes #178
2 parents 0fee32c + 0610751 commit 89d966e

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

internal/commands/repo/rm.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type rmOptions struct {
4747
func newRmCmd(streams command.Streams, hubClient *hub.Client, parent string) *cobra.Command {
4848
var opts rmOptions
4949
cmd := &cobra.Command{
50-
Use: rmName + " [OPTIONS] REPOSITORY",
50+
Use: rmName + " [OPTIONS] NAMESPACE/REPOSITORY",
5151
Short: "Delete a repository",
5252
Args: cli.ExactArgs(1),
5353
DisableFlagsInUseLine: true,
@@ -73,7 +73,11 @@ func runRm(ctx context.Context, streams command.Streams, hubClient *hub.Client,
7373
}
7474
namedRef, ok := ref.(reference.Named)
7575
if !ok {
76-
return fmt.Errorf("invalid reference: repository not specified")
76+
return errors.New("invalid reference: repository not specified")
77+
}
78+
79+
if !strings.Contains(repository, "/") {
80+
return fmt.Errorf("repository name must include username or organization name, example: hub-tool repo rm username/repository")
7781
}
7882

7983
if !opts.force {
@@ -104,6 +108,9 @@ func runRm(ctx context.Context, streams command.Streams, hubClient *hub.Client,
104108
if err := hubClient.RemoveRepository(namedRef.Name()); err != nil {
105109
return err
106110
}
107-
fmt.Fprintln(streams.Out(), "Deleted", repository)
111+
_, err = fmt.Fprintf(streams.Out(), "Repository %q was successfully deleted\n", repository)
112+
if err != nil {
113+
return err
114+
}
108115
return nil
109116
}

pkg/hub/client.go

+5
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ func (c *Client) doRequest(req *http.Request, reqOps ...RequestOp) ([]byte, erro
310310
defer resp.Body.Close() //nolint:errcheck
311311
}
312312
log.Tracef("HTTP response: %+v", resp)
313+
314+
if resp.StatusCode == http.StatusNotFound {
315+
return nil, &notFoundError{}
316+
}
317+
313318
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
314319
if resp.StatusCode == http.StatusForbidden {
315320
return nil, &forbiddenError{}

pkg/hub/error.go

+12
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,15 @@ func IsForbiddenError(err error) bool {
5656
_, ok := err.(*forbiddenError)
5757
return ok
5858
}
59+
60+
type notFoundError struct{}
61+
62+
func (n notFoundError) Error() string {
63+
return "resource not found"
64+
}
65+
66+
// IsNotFoundError check if the error type is a not found error
67+
func IsNotFoundError(err error) bool {
68+
_, ok := err.(*notFoundError)
69+
return ok
70+
}

pkg/hub/error_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,8 @@ func TestIsForbiddenError(t *testing.T) {
3737
assert.Assert(t, IsForbiddenError(&forbiddenError{}))
3838
assert.Assert(t, !IsForbiddenError(errors.New("")))
3939
}
40+
41+
func TestIsNotFoundError(t *testing.T) {
42+
assert.Assert(t, IsNotFoundError(&notFoundError{}))
43+
assert.Assert(t, !IsNotFoundError(errors.New("")))
44+
}

pkg/hub/repositories.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ import (
2525
)
2626

2727
const (
28-
// RepositoriesURL path to the Hub API listing the repositories
29-
RepositoriesURL = "/v2/repositories/%s/"
30-
// DeleteRepositoryURL path to the Hub API to remove a repository
31-
DeleteRepositoryURL = "/v2/repositories/%s/"
28+
// RepositoriesURL is the Hub API base URL
29+
RepositoriesURL = "/v2/repositories/"
3230
)
3331

3432
//Repository represents a Docker Hub repository
@@ -46,7 +44,8 @@ func (c *Client) GetRepositories(account string) ([]Repository, int, error) {
4644
if account == "" {
4745
account = c.account
4846
}
49-
u, err := url.Parse(c.domain + fmt.Sprintf(RepositoriesURL, account))
47+
repositoriesURL := fmt.Sprintf("%s%s%s", c.domain, RepositoriesURL, account)
48+
u, err := url.Parse(repositoriesURL)
5049
if err != nil {
5150
return nil, 0, err
5251
}
@@ -77,12 +76,17 @@ func (c *Client) GetRepositories(account string) ([]Repository, int, error) {
7776

7877
//RemoveRepository removes a repository on Hub
7978
func (c *Client) RemoveRepository(repository string) error {
80-
req, err := http.NewRequest("DELETE", c.domain+fmt.Sprintf(DeleteRepositoryURL, repository), nil)
79+
repositoryURL := fmt.Sprintf("%s%s%s/", c.domain, RepositoriesURL, repository)
80+
req, err := http.NewRequest(http.MethodDelete, repositoryURL, nil)
8181
if err != nil {
8282
return err
8383
}
8484
_, err = c.doRequest(req, withHubToken(c.token))
85-
return err
85+
if err != nil {
86+
return err
87+
}
88+
89+
return nil
8690
}
8791

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

0 commit comments

Comments
 (0)