-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathecr.go
45 lines (38 loc) · 1020 Bytes
/
ecr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"sort"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
)
func retrieveFromECR(image string) ([]string, error) {
svc := ecr.New(session.New())
input := &ecr.DescribeImagesInput{
RepositoryName: aws.String(image),
Filter: &ecr.DescribeImagesFilter{
TagStatus: aws.String("TAGGED"), // extract tagged images only
},
}
result, err := svc.DescribeImages(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
return nil, aerr
}
return nil, err
}
tags := extractEcrTagNames(result.ImageDetails)
return tags, nil
}
func extractEcrTagNames(images []*ecr.ImageDetail) []string {
tags := []string{}
sort.Slice(images, func(i, j int) bool {
return images[i].ImagePushedAt.After(*images[j].ImagePushedAt)
}) // sort Newest -> Oldest
for _, image := range images {
for _, tag := range image.ImageTags {
tags = append(tags, *tag)
}
}
return tags
}