clair/cmd/clairctl/docker/docker.go
Julien Garcia Gonzalez 3c1c89b17e fix dependencies
2017-02-27 16:00:13 +01:00

55 lines
1.7 KiB
Go

package docker
import (
"errors"
"reflect"
"github.com/docker/distribution"
"github.com/docker/distribution/manifest/schema1"
"github.com/docker/distribution/manifest/schema2"
"github.com/docker/docker/reference"
"github.com/coreos/clair/cmd/clairctl/config"
"github.com/coreos/clair/cmd/clairctl/docker/dockercli"
"github.com/coreos/clair/cmd/clairctl/docker/dockerdist"
"github.com/opencontainers/go-digest"
)
//RetrieveManifest get manifest from local or remote docker registry
func RetrieveManifest(imageName string, withExport bool) (image reference.NamedTagged, manifest distribution.Manifest, err error) {
if !config.IsLocal {
image, manifest, err = dockerdist.DownloadManifest(imageName, true)
} else {
image, manifest, err = dockercli.GetLocalManifest(imageName, withExport)
}
return
}
//GetLayerDigests return layer digests from manifest schema1 and schema2
func GetLayerDigests(manifest distribution.Manifest) ([]digest.Digest, error) {
layers := []digest.Digest{}
switch manifest.(type) {
case schema1.SignedManifest:
for _, l := range manifest.(schema1.SignedManifest).FSLayers {
layers = append(layers, l.BlobSum)
}
case *schema1.SignedManifest:
for _, l := range manifest.(*schema1.SignedManifest).FSLayers {
layers = append(layers, l.BlobSum)
}
case *schema2.DeserializedManifest:
for _, d := range manifest.(*schema2.DeserializedManifest).Layers {
layers = append(layers, d.Digest)
}
case schema2.DeserializedManifest:
for _, d := range manifest.(schema2.DeserializedManifest).Layers {
layers = append(layers, d.Digest)
}
default:
return nil, errors.New("Not supported manifest schema type: " + reflect.TypeOf(manifest).String())
}
return layers, nil
}