Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check validity of an image tag #143

Merged
merged 1 commit into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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