-
Notifications
You must be signed in to change notification settings - Fork 6
/
ecr.go
72 lines (64 loc) · 1.82 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/ecr"
)
func main() {
awsAccessKeyID := flag.String("access-key-id", "", "specify aws access-key-id")
awsSecretAccessKey := flag.String("secret-access-key", "", "specify aws secret-access-key")
region := flag.String("region", "", "specify aws region")
RepositoryName := flag.String("repository-name", "", "specify repository name")
imageTag := flag.String("tag", "", "specify image tag")
flag.Parse()
cfg, err := configure(awsAccessKeyID, awsSecretAccessKey, region)
if err != nil {
return
}
checkImageExists(cfg, RepositoryName, imageTag)
}
func configure(awsAccessKeyID *string, awsSecretAccessKey *string, region *string) (*aws.Config, error) {
// Load the Shared AWS Configuration (~/.aws/config)
cfg, err := config.LoadDefaultConfig(
context.TODO(),
)
if err != nil {
fmt.Println(err)
return nil, err
}
// read AWS Access Keys and overwrite credential
if *awsAccessKeyID != "" && *awsSecretAccessKey != "" {
cfg.Credentials = credentials.NewStaticCredentialsProvider(*awsAccessKeyID, *awsSecretAccessKey, os.Getenv("AWS_SESSION_TOKEN"))
}
cfg.Region = *region
return &cfg, err
}
func checkImageExists(cfg *aws.Config, repositoryName *string, imageTag *string) {
svc := ecr.NewFromConfig(*cfg)
paginator := ecr.NewListImagesPaginator(
svc,
&ecr.ListImagesInput{
RepositoryName: repositoryName,
},
)
for paginator.HasMorePages() {
out, err := paginator.NextPage(context.TODO())
if err != nil {
log.Fatal(err)
}
for _, image := range out.ImageIds {
if image.ImageTag != nil && *image.ImageTag == *imageTag {
fmt.Println("1")
return
}
}
}
fmt.Println("0")
return
}