Make docker.Image unaware of genericImage internals

This will allow us to cleanly move genericImage into a separate package.

This costs an extra pointer, but also allows us to rely on the type
system and drop handling "certainly impossible" errors, worth it just
for this simplification anyway.
This commit is contained in:
Miloslav Trmač
2016-06-11 07:02:14 +02:00
parent daeb358572
commit e15276232e
2 changed files with 8 additions and 27 deletions

View File

@@ -68,10 +68,7 @@ var inspectCmd = cli.Command{
logrus.Fatalf("Error computing manifest digest: %s", err.Error())
}
if dockerImg, ok := img.(*docker.Image); ok {
outputData.Name, err = dockerImg.SourceRefFullName()
if err != nil {
logrus.Fatalf("Error getting expanded repository name: %s", err.Error())
}
outputData.Name = dockerImg.SourceRefFullName()
outputData.RepoTags, err = dockerImg.GetRepositoryTags()
if err != nil {
logrus.Fatalf("Error determining repository tags: %s", err.Error())

View File

@@ -11,7 +11,8 @@ import (
// Image is a Docker-specific implementation of types.Image with a few extra methods
// which are specific to Docker.
type Image struct {
genericImage
types.Image
src *dockerImageSource
}
// NewDockerImage returns a new Image interface type after setting up
@@ -21,35 +22,18 @@ func NewDockerImage(img, certPath string, tlsVerify bool) (types.Image, error) {
if err != nil {
return nil, err
}
return &Image{genericImage{src: s}}, nil
}
// By construction a, docker.Image.genericImage.src must be a dockerImageSource.
// dockerSource returns it.
func (i *Image) dockerSource() (*dockerImageSource, error) {
if src, ok := i.genericImage.src.(*dockerImageSource); ok {
return src, nil
}
return nil, fmt.Errorf("Unexpected internal inconsistency, docker.Image not based on dockerImageSource")
return &Image{Image: GenericImageFromSource(s), src: s}, nil
}
// SourceRefFullName returns a fully expanded name for the repository this image is in.
func (i *Image) SourceRefFullName() (string, error) {
src, err := i.dockerSource()
if err != nil {
return "", err
}
return src.ref.FullName(), nil
func (i *Image) SourceRefFullName() string {
return i.src.ref.FullName()
}
// GetRepositoryTags list all tags available in the repository. Note that this has no connection with the tag(s) used for this specific image, if any.
func (i *Image) GetRepositoryTags() ([]string, error) {
src, err := i.dockerSource()
if err != nil {
return nil, err
}
url := fmt.Sprintf(tagsURL, src.ref.RemoteName())
res, err := src.c.makeRequest("GET", url, nil, nil)
url := fmt.Sprintf(tagsURL, i.src.ref.RemoteName())
res, err := i.src.c.makeRequest("GET", url, nil, nil)
if err != nil {
return nil, err
}