Browse code

Looking up a tag by repository name will default to REPOSITORY:latest. The empty tag '' is no longer allowed.

Solomon Hykes authored on 2013/03/23 08:07:13
Showing 3 changed files
... ...
@@ -326,7 +326,7 @@ func (srv *Server) CmdHistory(stdin io.ReadCloser, stdout io.Writer, args ...str
326 326
 		cmd.Usage()
327 327
 		return nil
328 328
 	}
329
-	image, err := srv.runtime.LookupImage(cmd.Arg(0))
329
+	image, err := srv.runtime.repositories.LookupImage(cmd.Arg(0))
330 330
 	if err != nil {
331 331
 		return err
332 332
 	}
... ...
@@ -9,7 +9,6 @@ import (
9 9
 	"os"
10 10
 	"path"
11 11
 	"sort"
12
-	"strings"
13 12
 	"sync"
14 13
 	"time"
15 14
 )
... ...
@@ -63,29 +62,9 @@ func (runtime *Runtime) containerRoot(id string) string {
63 63
 	return path.Join(runtime.repository, id)
64 64
 }
65 65
 
66
-func (runtime *Runtime) LookupImage(name string) (*Image, error) {
67
-	img, err := runtime.graph.Get(name)
68
-	if err != nil {
69
-		// FIXME: standardize on returning nil when the image doesn't exist, and err for everything else
70
-		// (so we can pass all errors here)
71
-		repoAndTag := strings.SplitN(name, ":", 2)
72
-		if len(repoAndTag) == 1 {
73
-			repoAndTag = append(repoAndTag, "")
74
-		}
75
-		if i, err := runtime.repositories.GetImage(repoAndTag[0], repoAndTag[1]); err != nil {
76
-			return nil, err
77
-		} else if i == nil {
78
-			return nil, fmt.Errorf("No such image: %s", name)
79
-		} else {
80
-			img = i
81
-		}
82
-	}
83
-	return img, nil
84
-}
85
-
86 66
 func (runtime *Runtime) Create(command string, args []string, image string, config *Config) (*Container, error) {
87 67
 	// Lookup image
88
-	img, err := runtime.LookupImage(image)
68
+	img, err := runtime.repositories.LookupImage(image)
89 69
 	if err != nil {
90 70
 		return nil, err
91 71
 	}
... ...
@@ -9,6 +9,8 @@ import (
9 9
 	"strings"
10 10
 )
11 11
 
12
+const DEFAULT_TAG = "latest"
13
+
12 14
 type TagStore struct {
13 15
 	path         string
14 16
 	graph        *Graph
... ...
@@ -61,12 +63,35 @@ func (store *TagStore) Reload() error {
61 61
 	return nil
62 62
 }
63 63
 
64
+func (store *TagStore) LookupImage(name string) (*Image, error) {
65
+	img, err := store.graph.Get(name)
66
+	if err != nil {
67
+		// FIXME: standardize on returning nil when the image doesn't exist, and err for everything else
68
+		// (so we can pass all errors here)
69
+		repoAndTag := strings.SplitN(name, ":", 2)
70
+		if len(repoAndTag) == 1 {
71
+			repoAndTag = append(repoAndTag, DEFAULT_TAG)
72
+		}
73
+		if i, err := store.GetImage(repoAndTag[0], repoAndTag[1]); err != nil {
74
+			return nil, err
75
+		} else if i == nil {
76
+			return nil, fmt.Errorf("No such image: %s", name)
77
+		} else {
78
+			img = i
79
+		}
80
+	}
81
+	return img, nil
82
+}
83
+
64 84
 func (store *TagStore) Set(repoName, tag, revision string) error {
65
-	if strings.Contains(repoName, ":") {
66
-		return fmt.Errorf("Illegal repository name: %s", repoName)
85
+	if tag == "" {
86
+		tag = DEFAULT_TAG
67 87
 	}
68
-	if strings.Contains(repoName, ":") {
69
-		return fmt.Errorf("Illegal tag name: %s", tag)
88
+	if err := validateRepoName(repoName); err != nil {
89
+		return err
90
+	}
91
+	if err := validateTagName(tag); err != nil {
92
+		return err
70 93
 	}
71 94
 	if err := store.Reload(); err != nil {
72 95
 		return err
... ...
@@ -104,3 +129,25 @@ func (store *TagStore) GetImage(repoName, tag string) (*Image, error) {
104 104
 	}
105 105
 	return nil, nil
106 106
 }
107
+
108
+// Validate the name of a repository
109
+func validateRepoName(name string) error {
110
+	if name == "" {
111
+		return fmt.Errorf("Repository name can't be empty")
112
+	}
113
+	if strings.Contains(name, ":") {
114
+		return fmt.Errorf("Illegal repository name: %s", name)
115
+	}
116
+	return nil
117
+}
118
+
119
+// Validate the name of a tag
120
+func validateTagName(name string) error {
121
+	if name == "" {
122
+		return fmt.Errorf("Tag name can't be empty")
123
+	}
124
+	if strings.Contains(name, "/") || strings.Contains(name, ":") {
125
+		return fmt.Errorf("Illegal tag name: %s", name)
126
+	}
127
+	return nil
128
+}