From 5bb5c33bdf1624395fc8b76776e81206167cac2c Mon Sep 17 00:00:00 2001 From: Jess Frazelle Date: Tue, 20 Sep 2016 18:16:34 -0700 Subject: [PATCH] cleanup Signed-off-by: Jess Frazelle --- README.md | 3 +- main.go | 116 +++++++++++++++++++++++++++---------------- registry/catalog.go | 2 +- registry/manifest.go | 13 ++++- registry/registry.go | 8 +-- registry/tags.go | 2 +- 6 files changed, 92 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index bb247faf..9907c9e2 100644 --- a/README.md +++ b/README.md @@ -50,9 +50,10 @@ AUTHOR(S): @jfrazelle COMMANDS: + delete delete a specific reference of a repository list, ls list all repositories - tags get the tags for a repository manifest get the json manifest for the specific reference of a repository + tags get the tags for a repository help, h Shows a list of commands or help for one command GLOBAL OPTIONS: diff --git a/main.go b/main.go index 16c50eea..b9c31e7c 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "errors" "fmt" "os" "strings" @@ -34,15 +35,17 @@ func preload(c *cli.Context) (err error) { } if len(c.Args()) > 0 { - auth, err = getAuthConfig(c) - if err != nil { - return err - } + if c.Args()[0] != "help" { + auth, err = getAuthConfig(c) + if err != nil { + return err + } - // create the registry client - r, err = registry.New(auth, c.GlobalBool("debug")) - if err != nil { - return err + // create the registry client + r, err = registry.New(auth, c.GlobalBool("debug")) + if err != nil { + return err + } } } @@ -76,6 +79,22 @@ func main() { }, } app.Commands = []cli.Command{ + { + Name: "delete", + Usage: "delete a specific reference of a repository", + Action: func(c *cli.Context) error { + repo, ref, err := getRepoAndRef(c) + if err != nil { + return err + } + + if err := r.Delete(repo, ref); err != nil { + return err + } + + return nil + }, + }, { Name: "list", Aliases: []string{"ls"}, @@ -109,45 +128,13 @@ func main() { return nil }, }, - { - Name: "tags", - Usage: "get the tags for a repository", - Action: func(c *cli.Context) error { - if len(c.Args()) < 1 { - return fmt.Errorf("pass the name of the repository") - } - - tags, err := r.Tags(c.Args()[0]) - if err != nil { - return err - } - - // print the tags - fmt.Println(strings.Join(tags, "\n")) - - return nil - }, - }, { Name: "manifest", Usage: "get the json manifest for the specific reference of a repository", Action: func(c *cli.Context) error { - if len(c.Args()) < 1 { - return fmt.Errorf("pass the name of the repository") - } - - arg := c.Args()[0] - parts := []string{} - if strings.Contains(arg, "@") { - parts = strings.Split(c.Args()[0], "@") - } else if strings.Contains(arg, ":") { - parts = strings.Split(c.Args()[0], ":") - } - - repo := parts[0] - ref := "latest" - if len(parts) > 1 { - ref = parts[1] + repo, ref, err := getRepoAndRef(c) + if err != nil { + return err } manifest, err := r.Manifest(repo, ref) @@ -163,6 +150,25 @@ func main() { // print the tags fmt.Println(string(b)) + return nil + }, + }, + { + Name: "tags", + Usage: "get the tags for a repository", + Action: func(c *cli.Context) error { + if len(c.Args()) < 1 { + return fmt.Errorf("pass the name of the repository") + } + + tags, err := r.Tags(c.Args()[0]) + if err != nil { + return err + } + + // print the tags + fmt.Println(strings.Join(tags, "\n")) + return nil }, }, @@ -210,3 +216,27 @@ func getAuthConfig(c *cli.Context) (types.AuthConfig, error) { return types.AuthConfig{}, fmt.Errorf("Could not find any authentication credentials") } + +func getRepoAndRef(c *cli.Context) (repo, ref string, err error) { + if len(c.Args()) < 1 { + return "", "", errors.New("pass the name of the repository") + } + + arg := c.Args()[0] + parts := []string{} + if strings.Contains(arg, "@") { + parts = strings.Split(c.Args()[0], "@") + } else if strings.Contains(arg, ":") { + parts = strings.Split(c.Args()[0], ":") + } else { + parts = []string{arg} + } + + repo = parts[0] + ref = "latest" + if len(parts) > 1 { + ref = parts[1] + } + + return +} diff --git a/registry/catalog.go b/registry/catalog.go index b65af7c9..376f1b4b 100644 --- a/registry/catalog.go +++ b/registry/catalog.go @@ -10,7 +10,7 @@ func (r *Registry) Catalog() ([]string, error) { r.Logf("registry.catalog url=%s", url) var response catalogResponse - if err := r.getJSON(url, &response); err != nil { + if _, err := r.getJSON(url, &response); err != nil { return nil, err } diff --git a/registry/manifest.go b/registry/manifest.go index 602dbbac..f7fd443e 100644 --- a/registry/manifest.go +++ b/registry/manifest.go @@ -1,6 +1,9 @@ package registry import ( + "log" + "strings" + "github.com/docker/distribution/manifest/schema1" "github.com/docker/distribution/manifest/schema2" ) @@ -11,10 +14,16 @@ func (r *Registry) Manifest(repository, ref string) (interface{}, error) { r.Logf("registry.manifests url=%s repository=%s ref=%s", url, repository, ref) var m schema2.Manifest - if err := r.getJSON(url, &m); err != nil { + h, err := r.getJSON(url, &m) + if err != nil { return m, err } + if !strings.Contains(ref, ":") { + // we got a tag, get the manifest for the ref + log.Printf("ref: %s", h.Get("Docker-Content-Digest")) + } + if m.Versioned.SchemaVersion == 1 { return r.v1Manifest(repository, ref) } @@ -27,7 +36,7 @@ func (r *Registry) v1Manifest(repository, ref string) (schema1.SignedManifest, e r.Logf("registry.manifests url=%s repository=%s ref=%s", url, repository, ref) var m schema1.SignedManifest - if err := r.getJSON(url, &m); err != nil { + if _, err := r.getJSON(url, &m); err != nil { return m, err } diff --git a/registry/registry.go b/registry/registry.go index 0ff5e7b2..1c744303 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -93,16 +93,16 @@ func (r *Registry) url(pathTemplate string, args ...interface{}) string { return url } -func (r *Registry) getJSON(url string, response interface{}) error { +func (r *Registry) getJSON(url string, response interface{}) (http.Header, error) { resp, err := r.Client.Get(url) if err != nil { - return err + return nil, err } defer resp.Body.Close() if err := json.NewDecoder(resp.Body).Decode(response); err != nil { - return err + return nil, err } - return nil + return resp.Header, nil } diff --git a/registry/tags.go b/registry/tags.go index be9184f9..8c320134 100644 --- a/registry/tags.go +++ b/registry/tags.go @@ -10,7 +10,7 @@ func (r *Registry) Tags(repository string) ([]string, error) { r.Logf("registry.tags url=%s repository=%s", url, repository) var response tagsResponse - if err := r.getJSON(url, &response); err != nil { + if _, err := r.getJSON(url, &response); err != nil { return nil, err }