Skip to content

Instantly share code, notes, and snippets.

@prasincs
Last active May 14, 2017 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prasincs/dc41782096bb4baf71e8dc4aade513c7 to your computer and use it in GitHub Desktop.
Save prasincs/dc41782096bb4baf71e8dc4aade513c7 to your computer and use it in GitHub Desktop.
ECR list images
package aws
import (
"fmt"
"sort"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
)
type SortImageIds []*ecr.ImageIdentifier
func (c SortImageIds) Len() int { return len(c) }
func (c SortImageIds) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c SortImageIds) Less(i, j int) bool {
//fmt.Println(*c[i].ImageTag, *c[j].ImageTag)
if c[i].ImageTag == nil {
return true
}
if c[j].ImageTag == nil {
return false
}
return strings.Compare(*c[i].ImageTag, *c[j].ImageTag) == -1
}
func GetSortedImageIds(region string, registryId string, repositoryName string) []*ecr.ImageIdentifier {
ecrSvc := ecr.New(session.New(), &aws.Config{Region: aws.String(region)})
done := false
var imageIds []*ecr.ImageIdentifier
params := &ecr.ListImagesInput{
RepositoryName: aws.String(repositoryName),
MaxResults: aws.Int64(100),
RegistryId: aws.String(registryId),
}
for !done {
resp, err := ecrSvc.ListImages(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return nil
}
//fmt.Println(resp)
for _, imageID := range resp.ImageIds {
imageIds = append(imageIds, imageID)
}
if resp.NextToken == nil {
done = true
} else {
params.NextToken = resp.NextToken
}
}
sort.Sort(SortImageIds(imageIds))
return imageIds
}
package main
func main(){
imageIds := awsapi.GetSortedImageIds("us-east-1", "<account Id>", "<repo name>")
fmt.Println(imageIds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment