diff --git a/sonar/cmd/layers_list.go b/sonar/cmd/layers_list.go index b0564c4..9592a94 100644 --- a/sonar/cmd/layers_list.go +++ b/sonar/cmd/layers_list.go @@ -20,6 +20,11 @@ var layersListCmd = &cobra.Command{ return fmt.Errorf("%s", err) } + if validity, err := image.Valid(); validity != true { + + return err + } + image.ShowTag = false dockerLayers, err := docker.GetAllLayers(image.String(), image.Tag) diff --git a/sonar/docker/image.go b/sonar/docker/image.go index c59a054..1ab7bfd 100644 --- a/sonar/docker/image.go +++ b/sonar/docker/image.go @@ -8,7 +8,10 @@ import ( "strings" ) -var ErrImageName = errors.New("Error: Invalid image name.") +var ( + ErrImageName = errors.New("Error: Invalid image name.") + ErrImageTag = errors.New("Error: Image tag doesn't exist.") +) type ImageRef struct { Namespace string `json:"namesapce"` @@ -26,6 +29,29 @@ func (this *ImageRef) String() string { return fmt.Sprintf("%s/%s", this.Namespace, this.Name) } +/* Returns true if the image tag exists for an image. */ +func (this *ImageRef) Valid() (bool, error) { + + reqURL := "https://hub.docker.com/v2/repositories/" + this.Namespace + "/" + this.Name + "/tags/" + this.Tag + + req, err := http.NewRequest("GET", reqURL, nil) + if err != nil { + return false, err + } + + resp, err := SendRequest(req, "", "") + if err != nil { + return false, err + } + defer resp.Body.Close() + + if resp.StatusCode == http.StatusOK { + return true, nil + } else { + return false, ErrImageTag + } +} + // NewImageRef creates a new reference to a Docker image. // Namespace and tag can be empty strings in order to use Docker defaults of 'library' and 'latest'. func NewImageRef(namespace, name, tag string) (*ImageRef, error) {