From 640026ec59d26172d5beddf97f3894d4374ddd4d Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Fri, 22 Mar 2013 16:07:13 -0700 Subject: [PATCH] Looking up a tag by repository name will default to REPOSITORY:latest. The empty tag '' is no longer allowed. --- commands.go | 2 +- runtime.go | 23 +-------------------- tags.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 54 insertions(+), 28 deletions(-) diff --git a/commands.go b/commands.go index d5fcacb9f7..26ed1db138 100644 --- a/commands.go +++ b/commands.go @@ -326,7 +326,7 @@ func (srv *Server) CmdHistory(stdin io.ReadCloser, stdout io.Writer, args ...str cmd.Usage() return nil } - image, err := srv.runtime.LookupImage(cmd.Arg(0)) + image, err := srv.runtime.repositories.LookupImage(cmd.Arg(0)) if err != nil { return err } diff --git a/runtime.go b/runtime.go index dcaaec6366..3b16479f3c 100644 --- a/runtime.go +++ b/runtime.go @@ -9,7 +9,6 @@ import ( "os" "path" "sort" - "strings" "sync" "time" ) @@ -63,29 +62,9 @@ func (runtime *Runtime) containerRoot(id string) string { return path.Join(runtime.repository, id) } -func (runtime *Runtime) LookupImage(name string) (*Image, error) { - img, err := runtime.graph.Get(name) - if err != nil { - // FIXME: standardize on returning nil when the image doesn't exist, and err for everything else - // (so we can pass all errors here) - repoAndTag := strings.SplitN(name, ":", 2) - if len(repoAndTag) == 1 { - repoAndTag = append(repoAndTag, "") - } - if i, err := runtime.repositories.GetImage(repoAndTag[0], repoAndTag[1]); err != nil { - return nil, err - } else if i == nil { - return nil, fmt.Errorf("No such image: %s", name) - } else { - img = i - } - } - return img, nil -} - func (runtime *Runtime) Create(command string, args []string, image string, config *Config) (*Container, error) { // Lookup image - img, err := runtime.LookupImage(image) + img, err := runtime.repositories.LookupImage(image) if err != nil { return nil, err } diff --git a/tags.go b/tags.go index a26a2f196d..ae8c1030cd 100644 --- a/tags.go +++ b/tags.go @@ -9,6 +9,8 @@ import ( "strings" ) +const DEFAULT_TAG = "latest" + type TagStore struct { path string graph *Graph @@ -61,12 +63,35 @@ func (store *TagStore) Reload() error { return nil } -func (store *TagStore) Set(repoName, tag, revision string) error { - if strings.Contains(repoName, ":") { - return fmt.Errorf("Illegal repository name: %s", repoName) +func (store *TagStore) LookupImage(name string) (*Image, error) { + img, err := store.graph.Get(name) + if err != nil { + // FIXME: standardize on returning nil when the image doesn't exist, and err for everything else + // (so we can pass all errors here) + repoAndTag := strings.SplitN(name, ":", 2) + if len(repoAndTag) == 1 { + repoAndTag = append(repoAndTag, DEFAULT_TAG) + } + if i, err := store.GetImage(repoAndTag[0], repoAndTag[1]); err != nil { + return nil, err + } else if i == nil { + return nil, fmt.Errorf("No such image: %s", name) + } else { + img = i + } } - if strings.Contains(repoName, ":") { - return fmt.Errorf("Illegal tag name: %s", tag) + return img, nil +} + +func (store *TagStore) Set(repoName, tag, revision string) error { + if tag == "" { + tag = DEFAULT_TAG + } + if err := validateRepoName(repoName); err != nil { + return err + } + if err := validateTagName(tag); err != nil { + return err } if err := store.Reload(); err != nil { return err @@ -104,3 +129,25 @@ func (store *TagStore) GetImage(repoName, tag string) (*Image, error) { } return nil, nil } + +// Validate the name of a repository +func validateRepoName(name string) error { + if name == "" { + return fmt.Errorf("Repository name can't be empty") + } + if strings.Contains(name, ":") { + return fmt.Errorf("Illegal repository name: %s", name) + } + return nil +} + +// Validate the name of a tag +func validateTagName(name string) error { + if name == "" { + return fmt.Errorf("Tag name can't be empty") + } + if strings.Contains(name, "/") || strings.Contains(name, ":") { + return fmt.Errorf("Illegal tag name: %s", name) + } + return nil +}