add flag to use aws config cred

This commit is contained in:
Jay 2018-12-24 11:25:56 +08:00
parent ef25ccc07b
commit b113007969

29
main.go
View File

@ -20,10 +20,11 @@ var svc *ecr.ECR
// RunFlag - // RunFlag -
type RunFlag struct { type RunFlag struct {
Repo string Repo string
KeepNum int KeepNum int
ExcludeTag arrayFlag ExcludeTag arrayFlag
ListRepo bool ListRepo bool
CertProfile string
} }
type ImageArr []*ecr.ImageDetail type ImageArr []*ecr.ImageDetail
@ -56,22 +57,30 @@ func init() {
flag.IntVar(&runFlag.KeepNum, "keep", 5, "keep number images") flag.IntVar(&runFlag.KeepNum, "keep", 5, "keep number images")
flag.Var(&runFlag.ExcludeTag, "e", "exlude delete tag") flag.Var(&runFlag.ExcludeTag, "e", "exlude delete tag")
flag.BoolVar(&runFlag.ListRepo, "list-repo", false, "list all repository") flag.BoolVar(&runFlag.ListRepo, "list-repo", false, "list all repository")
flag.StringVar(&runFlag.CertProfile, "profile", "", "aws config profile")
flag.Parse() flag.Parse()
} }
func main() { func main() {
accessKey := os.Getenv("AWS_ACCESS_KEY") var cred *credentials.Credentials
secretKey := os.Getenv("AWS_SECRET_KEY") _ = cred
region := os.Getenv("AWS_REGION") region := os.Getenv("AWS_REGION")
if len(accessKey) == 0 || len(secretKey) == 0 {
log.Fatal("aws access key or secret key env not found")
}
if len(region) == 0 { if len(region) == 0 {
region = "us-east-1" region = "us-east-1"
} }
if len(runFlag.CertProfile) == 0 {
accessKey := os.Getenv("AWS_ACCESS_KEY")
secretKey := os.Getenv("AWS_SECRET_KEY")
if len(accessKey) == 0 || len(secretKey) == 0 {
log.Fatal("AWS_ACCESS_KEY or AWS_SECRET_KEY env not found")
}
cred = credentials.NewStaticCredentials(accessKey, secretKey, "")
} else {
cred = credentials.NewSharedCredentials("", runFlag.CertProfile)
}
conf := &aws.Config{ conf := &aws.Config{
Region: aws.String(region), Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(accessKey, secretKey, ""), Credentials: cred,
} }
sess := session.New(conf) sess := session.New(conf)
svc = ecr.New(sess) svc = ecr.New(sess)