Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:
- cron: '0 10 * * 0'

env:
GO_VERSION: '1.24'
GO_VERSION: '1.26'

jobs:
analyze:
Expand Down Expand Up @@ -67,7 +67,7 @@ jobs:
uses: github/codeql-action/analyze@v4

- name: Upload CodeQL results
uses: actions/upload-artifact@v5.0.0
uses: actions/upload-artifact@v6.0.0
with:
name: codeql-results
path: /home/runner/work/go-lambda-cleanup/results/go.sarif
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ jobs:
run: |
aws ec2 describe-regions --all-regions --region us-east-1 --query "Regions[].RegionName" --output text >> cmd/aws-regions.txt

- name: Install Task
uses: go-task/setup-task@v1

- name: Update Dependencies
run: go get -u ./...

- name: Execute Go tests
run: make tests
run: task tests

- name: Set up QEMU
uses: docker/setup-qemu-action@v3.7.0
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ permissions:
packages: write

env:
GO_VERSION: '1.24'
GO_VERSION: '1.26'

jobs:

Expand Down Expand Up @@ -41,7 +41,8 @@ jobs:

- name: Check out code into the Go module directory
uses: actions/checkout@v6

- name: Install Task
uses: go-task/setup-task@v1

- name: Generate AWS region file
env:
Expand All @@ -56,10 +57,10 @@ jobs:
# args: -v

- name: Execute Go tests
run: make tests-coverage
run: task tests-coverage

- name: Codecov Upload
uses: codecov/codecov-action@v5.5.1
uses: codecov/codecov-action@v5.5.2
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
GO_VERSION: '1.24'
UPX_VERSION: "4.2.4"
SVU_VERSION: "2.0.1"
GO_VERSION: '1.26'
UPX_VERSION: "5.1.0"
SVU_VERSION: "3.3.0"

jobs:
tag:
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:


env:
GO_VERSION: '1.24'
GO_VERSION: '1.26'

jobs:
test:
Expand All @@ -24,6 +24,9 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v6

- name: Install Task
uses: go-task/setup-task@v1

- name: Generate AWS region file
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
Expand All @@ -37,7 +40,7 @@ jobs:
# args: -v

- name: Execute Go tests
run: make tests-coverage
run: task tests-coverage

- name: Test CLI
env:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) karl-cardenas-coding
# SPDX-License-Identifier: MIT

FROM golang:1.24.2-alpine3.21 AS builder
FROM golang:1.26.0-alpine3.22 AS builder

LABEL org.opencontainers.image.source="http://github.com/karl-cardenas-coding/go-lambda-cleanup"
LABEL org.opencontainers.image.description="A solution for removing previous versions of AWS Lambdas"
Expand Down
48 changes: 48 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# https://taskfile.dev
version: '3'

tasks:
aws-regions:
desc: Generate AWS regions file
cmds:
- aws ec2 describe-regions --region us-east-1 --all-regions --query "Regions[].RegionName" --output text > cmd/aws-regions.txt

license:
desc: Apply license headers
cmds:
- copywrite headers

opensource:
desc: Check open source licenses
cmds:
- ~/go/bin/go-licenses report github.com/karl-cardenas-coding/go-lambda-cleanup/v2 --template=documentation/open-source.tpl > documentation/open-source.md

lint:
desc: Run Go linter
cmds:
- golangci-lint run ./...

build:
desc: Build the binary
cmds:
- go build -ldflags="-X 'github.com/karl-cardenas-coding/go-lambda-cleanup/v2/cmd.VersionString=1.0.0'" -o=glc -v

tests:
desc: Run tests
cmds:
- go test -shuffle on -race ./...

tests-coverage:
desc: Run tests with code coverage
cmds:
- go test -race -shuffle on -cover -coverprofile=coverage.out -covermode=atomic ./...

view-coverage:
desc: View code coverage report
cmds:
- go tool cover -html=coverage.out

nil:
desc: Check for nil errors
cmds:
- ~/go/bin/nilaway ./...
21 changes: 13 additions & 8 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ var cleanCmd = &cobra.Command{
config = GlobalCliConfig
awsEnvRegion = os.Getenv("AWS_DEFAULT_REGION")
awsEnvProfile = os.Getenv("AWS_PROFILE")

if *config.RegionFlag == "" {
if awsEnvRegion != "" {
*config.RegionFlag, err = validateRegion(f, awsEnvRegion)
if err != nil {
return err
}

} else {
return errors.New("missing region flag and AWS_DEFAULT_REGION env variable. Please use -r and provide a valid AWS region")
}
Expand All @@ -94,6 +94,7 @@ var cleanCmd = &cobra.Command{
} else {
log.Infof("The AWS Profile flag \"%s\" was passed in", *config.ProfileFlag)
}

awsConfigOptions = append(awsConfigOptions, awsConfig.WithSharedConfigProfile(*config.ProfileFlag))

if *config.Verbose {
Expand All @@ -112,11 +113,13 @@ var cleanCmd = &cobra.Command{

if *config.LambdaListFile != "" {
log.Info("******** CUSTOM LAMBDA LIST PROVIDED ********")

list, err := internal.GenerateLambdaDeleteList(*config.LambdaListFile)
if err != nil {
log.Infof("an issue occurred while processing %s", *config.LambdaListFile)
log.Info(err.Error())
}

customeDeleteList = list
}

Expand All @@ -129,6 +132,7 @@ var cleanCmd = &cobra.Command{
if err != nil {
return errors.New("ERROR RETRIEVING AWS CREDENTIALS")
}

if creds.Expired() {
return errors.New("AWS CREDENTIALS EXPIRED")
}
Expand All @@ -138,6 +142,7 @@ var cleanCmd = &cobra.Command{
// Set the User-Agent for all AWS with the Lambda client
o.APIOptions = append(o.APIOptions, middleware.AddUserAgentKeyValue("go-lambda-cleanup", VersionString))
})

err = executeClean(ctx, &config, initSvc, customeDeleteList)
if err != nil {
return err
Expand All @@ -150,7 +155,7 @@ var cleanCmd = &cobra.Command{
/*
executeClean is the main function that executes the clean-up process
It takes a context, a pointer to a cliConfig struct, a pointer to a lambda client, and a list of custom lambdas to delete
An error is returned if the function fails to execute
An error is returned if the function fails to execute.
*/
func executeClean(ctx context.Context, config *cliConfig, svc *lambda.Client, customList []string) error {
startTime := time.Now()
Expand Down Expand Up @@ -311,7 +316,7 @@ func displayDuration(startTime time.Time) {
}

// generateDeleteInputStructs takes a list of lambda.DeleteFunctionInput and a boolean value to determine if the user wants more details. The function returns a list of lambda.DeleteFunctionInput
// An error is returned if the function fails to execute
// An error is returned if the function fails to execute.
func generateDeleteInputStructs(versionsList [][]types.FunctionConfiguration, details bool) ([][]lambda.DeleteFunctionInput, error) {
var (
returnError error
Expand Down Expand Up @@ -349,7 +354,7 @@ func generateDeleteInputStructs(versionsList [][]types.FunctionConfiguration, de
}

// calculateSpaceRemoval returns the total size of all the versions to be deleted.
// The function takes a list of lambda.DeleteFunctionInput and returns an int
// The function takes a list of lambda.DeleteFunctionInput and returns an int.
func calculateSpaceRemoval(deleteList [][]types.FunctionConfiguration) int {
var (
size int
Expand All @@ -367,7 +372,7 @@ func calculateSpaceRemoval(deleteList [][]types.FunctionConfiguration) int {
}

// countDeleteVersions returns the total number of versions to be deleted.
// The function takes a list of lambda.DeleteFunctionInput and returns an int
// The function takes a list of lambda.DeleteFunctionInput and returns an int.
func countDeleteVersions(deleteList [][]lambda.DeleteFunctionInput) int {
var (
versionsCount int
Expand Down Expand Up @@ -594,16 +599,16 @@ func getLambdaStorage(list []types.FunctionConfiguration) (int64, error) {

// validateRegion validates the user input to ensure it is a valid AWS region. The function takes a embed.FS and a string. The function returns a string and an error
// An embedded file is used to validate the user input. The embedded file contains a list of all the AWS regions
// Example of the embedded file: ap-south-2 ap-south-1 eu-south-1 eu-south-2 me-central-1 ca-central-1 eu-central-1 eu-central-2
// Example of the embedded file: ap-south-2 ap-south-1 eu-south-1 eu-south-2 me-central-1 ca-central-1 eu-central-1 eu-central-2.
func validateRegion(f embed.FS, input string) (string, error) {
var output string

var err error

rawData, _ := f.ReadFile(regionFile)
regionsList := strings.Split(string(rawData), " ")
regionsList := strings.SplitSeq(string(rawData), " ")

for _, region := range regionsList {
for region := range regionsList {
if strings.ToLower(input) == strings.TrimSpace(region) {
output = strings.TrimSpace(region)
}
Expand Down
Loading
Loading