mirror of
https://github.com/containers/buildah.git
synced 2025-07-31 15:24:26 +03:00
Add a "tag" command
Add a "tag" command, for adding names to locally-stored images. Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> Closes: #110 Approved by: rhatdan
This commit is contained in:
committed by
Atomic Bot
parent
76395efa52
commit
3065ea3d16
@ -83,6 +83,7 @@ func main() {
|
||||
rmiCommand,
|
||||
budCommand,
|
||||
inspectCommand,
|
||||
tagCommand,
|
||||
}
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
|
39
cmd/buildah/tag.go
Normal file
39
cmd/buildah/tag.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/projectatomic/buildah/util"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
var (
|
||||
tagDescription = "Adds one or more additional names to locally-stored image"
|
||||
tagCommand = cli.Command{
|
||||
Name: "tag",
|
||||
Usage: "Add an additional name to a local image",
|
||||
Description: tagDescription,
|
||||
Action: tagCmd,
|
||||
ArgsUsage: "IMAGE-NAME [IMAGE-NAME ...]",
|
||||
}
|
||||
)
|
||||
|
||||
func tagCmd(c *cli.Context) error {
|
||||
args := c.Args()
|
||||
if len(args) < 2 {
|
||||
return fmt.Errorf("image name and at least one new name must be specified")
|
||||
}
|
||||
store, err := getStore(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
img, err := util.FindImage(store, args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("error finding local image %q: %v", args[0], err)
|
||||
}
|
||||
err = util.AddImageNames(store, img, args[1:])
|
||||
if err != nil {
|
||||
return fmt.Errorf("error adding names %v to image %q: %v", args[1:], args[0], err)
|
||||
}
|
||||
return nil
|
||||
}
|
38
commit.go
38
commit.go
@ -1,15 +1,15 @@
|
||||
package buildah
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/containers/image/copy"
|
||||
"github.com/containers/image/docker/reference"
|
||||
"github.com/containers/image/signature"
|
||||
"github.com/containers/image/storage"
|
||||
"github.com/containers/image/types"
|
||||
"github.com/containers/storage/pkg/archive"
|
||||
"github.com/projectatomic/buildah/util"
|
||||
)
|
||||
|
||||
// CommitOptions can be used to alter how an image is committed.
|
||||
@ -33,23 +33,6 @@ type CommitOptions struct {
|
||||
ReportWriter io.Writer
|
||||
}
|
||||
|
||||
func expandTags(tags []string) ([]string, error) {
|
||||
expanded := []string{}
|
||||
for _, tag := range tags {
|
||||
name, err := reference.ParseNormalizedNamed(tag)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing tag %q: %v", tag, err)
|
||||
}
|
||||
name = reference.TagNameOnly(name)
|
||||
tag = ""
|
||||
if tagged, ok := name.(reference.NamedTagged); ok {
|
||||
tag = ":" + tagged.Tag()
|
||||
}
|
||||
expanded = append(expanded, name.Name()+tag)
|
||||
}
|
||||
return expanded, nil
|
||||
}
|
||||
|
||||
// Commit writes the contents of the container, along with its updated
|
||||
// configuration, to a new image in the specified location, and if we know how,
|
||||
// add any additional tags that were specified.
|
||||
@ -67,20 +50,23 @@ func (b *Builder) Commit(dest types.ImageReference, options CommitOptions) error
|
||||
return err
|
||||
}
|
||||
err = copy.Image(policyContext, dest, src, getCopyOptions(options.ReportWriter))
|
||||
switch dest.Transport().Name() {
|
||||
case storage.Transport.Name():
|
||||
tags, err := expandTags(options.AdditionalTags)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(options.AdditionalTags) > 0 {
|
||||
switch dest.Transport().Name() {
|
||||
case storage.Transport.Name():
|
||||
img, err := storage.Transport.GetStoreImage(b.store, dest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = b.store.SetNames(img.ID, append(img.Names, tags...))
|
||||
err = util.AddImageNames(b.store, img, options.AdditionalTags)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error setting image names to %v: %v", append(img.Names, tags...), err)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
default:
|
||||
logrus.Warnf("don't know how to add tags to images stored in %q transport", dest.Transport().Name())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -537,6 +537,19 @@ return 1
|
||||
esac
|
||||
}
|
||||
|
||||
_buildah_tag() {
|
||||
local options_with_args="
|
||||
"
|
||||
|
||||
local all_options="$options_with_args"
|
||||
|
||||
case "$cur" in
|
||||
-*)
|
||||
COMPREPLY=($(compgen -W "$options_with_args" -- "$cur"))
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
_buildah_from() {
|
||||
local boolean_options="
|
||||
--help
|
||||
@ -583,6 +596,7 @@ return 1
|
||||
rm
|
||||
rmi
|
||||
run
|
||||
tag
|
||||
umount
|
||||
unmount
|
||||
)
|
||||
|
18
docs/buildah-tag.md
Normal file
18
docs/buildah-tag.md
Normal file
@ -0,0 +1,18 @@
|
||||
## buildah-tag "1" "May 2017" "buildah"
|
||||
|
||||
## NAME
|
||||
buildah tag - Add additional names to local images.
|
||||
|
||||
## SYNOPSIS
|
||||
**buildah** **tag** **name** **new-name** [...]
|
||||
|
||||
## DESCRIPTION
|
||||
Adds additional names to locally-stored images.
|
||||
|
||||
## EXAMPLE
|
||||
|
||||
buildah tag imageName firstNewName
|
||||
buildah tag imageName firstNewName SecondNewName
|
||||
|
||||
## SEE ALSO
|
||||
buildah(1)
|
19
tests/tag.bats
Normal file
19
tests/tag.bats
Normal file
@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load helpers
|
||||
|
||||
@test "tag" {
|
||||
cid=$(buildah from --pull=false --signature-policy ${TESTSDIR}/policy.json scratch)
|
||||
run buildah commit --signature-policy ${TESTSDIR}/policy.json "$cid" scratch-image
|
||||
[ "$status" -eq 0 ]
|
||||
run buildah inspect --type image tagged-image
|
||||
[ "$status" -ne 0 ]
|
||||
run buildah tag scratch-image tagged-image tagged-also-image named-image
|
||||
[ "$status" -eq 0 ]
|
||||
run buildah inspect --type image tagged-image
|
||||
[ "$status" -eq 0 ]
|
||||
run buildah inspect --type image tagged-also-image
|
||||
[ "$status" -eq 0 ]
|
||||
run buildah inspect --type image named-image
|
||||
[ "$status" -eq 0 ]
|
||||
}
|
54
util/util.go
Normal file
54
util/util.go
Normal file
@ -0,0 +1,54 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/containers/image/docker/reference"
|
||||
is "github.com/containers/image/storage"
|
||||
"github.com/containers/storage"
|
||||
)
|
||||
|
||||
// ExpandTags takes unqualified names, parses them as image names, and returns
|
||||
// the fully expanded result, including a tag.
|
||||
func ExpandTags(tags []string) ([]string, error) {
|
||||
expanded := []string{}
|
||||
for _, tag := range tags {
|
||||
name, err := reference.ParseNormalizedNamed(tag)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing tag %q: %v", tag, err)
|
||||
}
|
||||
name = reference.TagNameOnly(name)
|
||||
tag = ""
|
||||
if tagged, ok := name.(reference.NamedTagged); ok {
|
||||
tag = ":" + tagged.Tag()
|
||||
}
|
||||
expanded = append(expanded, name.Name()+tag)
|
||||
}
|
||||
return expanded, nil
|
||||
}
|
||||
|
||||
// FindImage locates the locally-stored image which corresponds to a given name.
|
||||
func FindImage(store storage.Store, image string) (*storage.Image, error) {
|
||||
ref, err := is.Transport.ParseStoreReference(store, image)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing reference to image %q: %v", image, err)
|
||||
}
|
||||
img, err := is.Transport.GetStoreImage(store, ref)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to locate image: %v", err)
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
|
||||
// AddImageNames adds the specified names to the specified image.
|
||||
func AddImageNames(store storage.Store, image *storage.Image, addNames []string) error {
|
||||
names, err := ExpandTags(addNames)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = store.SetNames(image.ID, append(image.Names, names...))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error adding names (%v) to image %q: %v", names, image.ID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user