Created
May 14, 2017 04:29
-
-
Save prasincs/ec27a3711726e43fdb63da9d545bc39d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"sort" | |
"github.com/aws/aws-sdk-go/aws" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/ecr" | |
"github.com/nsd20463/numeralsort" | |
) | |
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. | |
log.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.Slice(imageIds, func(i, j int) bool { | |
//fmt.Println(*c[i].ImageTag, *c[j].ImageTag) | |
if imageIds[i].ImageTag == nil { | |
return true | |
} | |
if imageIds[j].ImageTag == nil { | |
return false | |
} | |
//return strings.Compare(*imageIds[i].ImageTag, *imageIds[j].ImageTag) == -1 | |
return numeralsort.Less(*imageIds[i].ImageTag, *imageIds[j].ImageTag) | |
}) | |
return imageIds | |
} | |
func main() { | |
region := flag.String("region", "us-east-1", "AWS Region") | |
accountID := flag.String("account", "", "Your AWS AccountID") | |
repo := flag.String("repo", "", "The ECR Repo") | |
flag.Parse() | |
imageIds := GetSortedImageIds(*region, *accountID, *repo) | |
for _, imageId := range imageIds { | |
if imageId.ImageTag != nil { | |
fmt.Println(*imageId.ImageTag, *imageId.ImageDigest) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment