Sfoglia il codice sorgente

'docker tag': assign a repository+tag to an image

Solomon Hykes 12 anni fa
parent
commit
bf7602bc09
4 ha cambiato i file con 25 aggiunte e 5 eliminazioni
  1. 14 1
      commands.go
  2. 1 1
      registry.go
  3. 1 1
      runtime.go
  4. 9 2
      tags.go

+ 14 - 1
commands.go

@@ -389,7 +389,7 @@ func (srv *Server) CmdImport(stdin io.ReadCloser, stdout io.Writer, args ...stri
 	// Optionally register the image at REPO/TAG
 	if repository := cmd.Arg(1); repository != "" {
 		tag := cmd.Arg(2) // Repository will handle an empty tag properly
-		if err := srv.runtime.repositories.Set(repository, tag, img.Id); err != nil {
+		if err := srv.runtime.repositories.Set(repository, tag, img.Id, true); err != nil {
 			return err
 		}
 	}
@@ -760,6 +760,19 @@ func (p *ports) Set(value string) error {
 	return nil
 }
 
+func (srv *Server) CmdTag(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
+	cmd := rcli.Subcmd(stdout, "tag", "[OPTIONS] IMAGE REPOSITORY [TAG]", "Tag an image into a repository")
+	force := cmd.Bool("f", false, "Force")
+	if err := cmd.Parse(args); err != nil {
+		return nil
+	}
+	if cmd.NArg() < 2 {
+		cmd.Usage()
+		return nil
+	}
+	return srv.runtime.repositories.Set(cmd.Arg(1), cmd.Arg(2), cmd.Arg(0), *force)
+}
+
 func (srv *Server) CmdRun(stdin io.ReadCloser, stdout io.Writer, args ...string) error {
 	cmd := rcli.Subcmd(stdout, "run", "[OPTIONS] IMAGE COMMAND [ARG...]", "Run a command in a new container")
 	fl_user := cmd.String("u", "", "Username or UID")

+ 1 - 1
registry.go

@@ -175,7 +175,7 @@ func (graph *Graph) PullRepository(user, repoName, askedTag string, repositories
 		if err = graph.PullImage(rev, authConfig); err != nil {
 			return err
 		}
-		if err = repositories.Set(repoName, tag, rev); err != nil {
+		if err = repositories.Set(repoName, tag, rev, true); err != nil {
 			return err
 		}
 	}

+ 1 - 1
runtime.go

@@ -201,7 +201,7 @@ func (runtime *Runtime) Commit(id, repository, tag string) (*Image, error) {
 	}
 	// Register the image if needed
 	if repository != "" {
-		if err := runtime.repositories.Set(repository, tag, img.Id); err != nil {
+		if err := runtime.repositories.Set(repository, tag, img.Id, true); err != nil {
 			return img, err
 		}
 	}

+ 9 - 2
tags.go

@@ -83,7 +83,11 @@ func (store *TagStore) LookupImage(name string) (*Image, error) {
 	return img, nil
 }
 
-func (store *TagStore) Set(repoName, tag, revision string) error {
+func (store *TagStore) Set(repoName, tag, imageName string, force bool) error {
+	img, err := store.LookupImage(imageName)
+	if err != nil {
+		return err
+	}
 	if tag == "" {
 		tag = DEFAULT_TAG
 	}
@@ -101,9 +105,12 @@ func (store *TagStore) Set(repoName, tag, revision string) error {
 		repo = r
 	} else {
 		repo = make(map[string]string)
+		if old, exists := store.Repositories[repoName]; exists && !force {
+			return fmt.Errorf("Tag %s:%s is already set to %s", repoName, tag, old)
+		}
 		store.Repositories[repoName] = repo
 	}
-	repo[tag] = revision
+	repo[tag] = img.Id
 	return store.Save()
 }