2016-07-20 09:46:01 +00:00
|
|
|
package transports
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/containers/image/directory"
|
|
|
|
"github.com/containers/image/docker"
|
2016-11-22 19:32:10 +00:00
|
|
|
"github.com/containers/image/docker/daemon"
|
2016-09-17 13:50:35 +00:00
|
|
|
ociLayout "github.com/containers/image/oci/layout"
|
2016-07-20 09:46:01 +00:00
|
|
|
"github.com/containers/image/openshift"
|
2016-11-22 19:32:10 +00:00
|
|
|
"github.com/containers/image/storage"
|
2016-07-20 09:46:01 +00:00
|
|
|
"github.com/containers/image/types"
|
2016-10-17 13:53:40 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-07-20 09:46:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// KnownTransports is a registry of known ImageTransport instances.
|
|
|
|
var KnownTransports map[string]types.ImageTransport
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
KnownTransports = make(map[string]types.ImageTransport)
|
2016-11-22 19:32:10 +00:00
|
|
|
// NOTE: Make sure docs/policy.json.md is updated when adding or updating
|
|
|
|
// a transport.
|
2016-07-20 09:46:01 +00:00
|
|
|
for _, t := range []types.ImageTransport{
|
|
|
|
directory.Transport,
|
|
|
|
docker.Transport,
|
2016-11-22 19:32:10 +00:00
|
|
|
daemon.Transport,
|
2016-09-17 13:50:35 +00:00
|
|
|
ociLayout.Transport,
|
2016-07-20 09:46:01 +00:00
|
|
|
openshift.Transport,
|
2016-11-22 19:32:10 +00:00
|
|
|
storage.Transport,
|
2016-07-20 09:46:01 +00:00
|
|
|
} {
|
|
|
|
name := t.Name()
|
|
|
|
if _, ok := KnownTransports[name]; ok {
|
|
|
|
panic(fmt.Sprintf("Duplicate image transport name %s", name))
|
|
|
|
}
|
|
|
|
KnownTransports[name] = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseImageName converts a URL-like image name to a types.ImageReference.
|
|
|
|
func ParseImageName(imgName string) (types.ImageReference, error) {
|
|
|
|
parts := strings.SplitN(imgName, ":", 2)
|
|
|
|
if len(parts) != 2 {
|
2016-10-17 13:53:40 +00:00
|
|
|
return nil, errors.Errorf(`Invalid image name "%s", expected colon-separated transport:reference`, imgName)
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
|
|
|
transport, ok := KnownTransports[parts[0]]
|
|
|
|
if !ok {
|
2016-10-17 13:53:40 +00:00
|
|
|
return nil, errors.Errorf(`Invalid image name "%s", unknown transport "%s"`, imgName, parts[0])
|
2016-07-20 09:46:01 +00:00
|
|
|
}
|
|
|
|
return transport.ParseReference(parts[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImageName converts a types.ImageReference into an URL-like image name, which MUST be such that
|
|
|
|
// ParseImageName(ImageName(reference)) returns an equivalent reference.
|
|
|
|
//
|
|
|
|
// This is the generally recommended way to refer to images in the UI.
|
|
|
|
//
|
|
|
|
// NOTE: The returned string is not promised to be equal to the original input to ParseImageName;
|
|
|
|
// e.g. default attribute values omitted by the user may be filled in in the return value, or vice versa.
|
|
|
|
func ImageName(ref types.ImageReference) string {
|
|
|
|
return ref.Transport().Name() + ":" + ref.StringWithinTransport()
|
|
|
|
}
|