Skip to content

Commit

Permalink
Check validity of an image tag (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelicianoTech authored Jun 19, 2022
1 parent 8bb10da commit ed1b539
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions sonar/cmd/layers_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 27 additions & 1 deletion sonar/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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) {
Expand Down

0 comments on commit ed1b539

Please sign in to comment.