-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
61 lines (53 loc) · 1.36 KB
/
main.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
package main
import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/devopsbox-io/aws-ecr-cleaner/internal/pkg/aws"
"github.com/devopsbox-io/aws-ecr-cleaner/internal/pkg/cleaner"
"os"
"strconv"
"time"
)
const DefaultKeepDays = 30
func main() {
awsProvider, err := aws.NewProvider()
if err != nil {
panic(err)
}
cleanerObj := cleaner.New(awsProvider, cleaner.Config{
DryRun: getDryRun(os.LookupEnv),
DefaultKeepDays: getDefaultKeepDays(os.LookupEnv),
})
if isLambda(os.LookupEnv) {
lambda.Start(func() error {
return cleanerObj.Clean(time.Now())
})
} else {
err := cleanerObj.Clean(time.Now())
if err != nil {
panic(err)
}
}
}
func isLambda(lookupEnv func(key string) (string, bool)) bool {
_, result := lookupEnv("AWS_LAMBDA_FUNCTION_NAME")
return result
}
func getDefaultKeepDays(lookupEnv func(key string) (string, bool)) int {
defaultKeepDays := DefaultKeepDays
defaultKeepDaysStr, isDefaultKeepDaysSet := lookupEnv("DEFAULT_KEEP_DAYS")
if isDefaultKeepDaysSet {
parsedDefaultKeepDays, err := strconv.Atoi(defaultKeepDaysStr)
if err == nil {
defaultKeepDays = parsedDefaultKeepDays
}
}
return defaultKeepDays
}
func getDryRun(lookupEnv func(key string) (string, bool)) bool {
dryRun := true
dryRunStr, isDryRunSet := lookupEnv("DRY_RUN")
if isDryRunSet && dryRunStr == "false" {
dryRun = false
}
return dryRun
}