Skip to content

Commit

Permalink
Merge pull request #45 from pratikkumar-mohite/pm/issue-22
Browse files Browse the repository at this point in the history
Issue 22: Improving logging with logrus
  • Loading branch information
pratikkumar-mohite authored Aug 31, 2024
2 parents c1cc0b9 + 27e9a1b commit ab09272
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 16 deletions.
6 changes: 3 additions & 3 deletions cmd/s3-cleanup/cleanup.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package main

import (
"fmt"
"time"
"sync"

log "github.com/sirupsen/logrus"
"github.com/pratikkumar-mohite/s3-cleanup/pkg/aws"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ func s3Cleanup() {
}(version)
}
versionWG.Wait()
fmt.Printf("Deleted Object: %v\n", object.ObjectName)
log.Infof("Deleted Object: %v\n", object.ObjectName)
}
wg.Done()
}
Expand All @@ -60,5 +60,5 @@ func s3Cleanup() {
wg.Wait()

elapsedTime := time.Since(startTime)
fmt.Println("Total time taken for object deletion: ", elapsedTime)
log.Infof("Time taken for object deletion: %v", elapsedTime)
}
5 changes: 3 additions & 2 deletions cmd/s3-cleanup/utils.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package main

import (
"fmt"
"os"

log "github.com/sirupsen/logrus"
)

func getFromEnv(env string) string {
variable := os.Getenv(env)
if variable == "" {
panic(fmt.Sprintf("Environment variable %s is not set", env))
log.Fatalf("Environment variable %s is not set", env)
}
return variable
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/aws/aws-sdk-go-v2 v1.30.3
github.com/aws/aws-sdk-go-v2/config v1.27.27
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3
github.com/sirupsen/logrus v1.9.3
)

require (
Expand All @@ -24,4 +25,5 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect
github.com/aws/smithy-go v1.20.3 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 h1:ZsDKRLXGWHk8WdtyYMoGNO7bTudr
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3/go.mod h1:zwySh8fpFyXp9yOr/KVzxOl8SRqgf/IDw5aUt9UKFcQ=
github.com/aws/smithy-go v1.20.3 h1:ryHwveWzPV5BIof6fyDvor6V3iUL7nTfiTKXHiW05nE=
github.com/aws/smithy-go v1.20.3/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 2 additions & 1 deletion pkg/aws/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"context"

log "github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
Expand All @@ -11,7 +12,7 @@ import (
func AWSConnection(profile string) aws.Config {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigProfile(profile))
if err != nil {
panic("unable to load SDK config, " + err.Error())
log.Fatalf("Unable to load SDK config, %v", err)
}
return cfg
}
Expand Down
21 changes: 11 additions & 10 deletions pkg/aws/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ package aws

import (
"context"
"fmt"
"os"
"strings"

log "github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
)

func (c *S3Client) getS3Bucket(bucket_name string) string {
output, err := c.Client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
if err != nil {
panic("unable to list buckets, " + err.Error())
log.Fatalf("Unable to list buckets, " + err.Error())
}
for _, bucket := range output.Buckets {
if aws.ToString(bucket.Name) == bucket_name {
return bucket_name
}
}
if bucket_name == "" {
panic("bucket name is empty")
log.Fatalf("Bucket name is empty %s", bucket_name)
}
return ""
}
Expand All @@ -33,7 +33,7 @@ func (c *S3Client) checkVersioningStatus(bucket string) string {

result, err := c.Client.GetBucketVersioning(context.TODO(), input)
if err != nil {
panic("failed to get bucket versioning, " + err.Error())
log.Fatalf("Failed to get bucket versioning, %v" + err.Error())
}

return string(result.Status)
Expand All @@ -51,7 +51,7 @@ func (c *S3Client) listObjectVersions(bucket *string) []S3BucketObject {
for paginator.HasMorePages() {
page, err := paginator.NextPage(context.TODO())
if err != nil {
fmt.Printf("failed to get page, %v", err)
log.Fatalf("Failed to get page, %v", err)
}

for _, version := range page.Versions {
Expand Down Expand Up @@ -83,10 +83,11 @@ func (c *S3Client) GetS3BucketObjects() []S3BucketObject {
Bucket: &bucket,
})
if err != nil {
panic("unable to list objects, " + err.Error())
log.Fatalf("Unable to list objects, %v" + err.Error())
}
objects := make([]S3BucketObject, len(output.Contents))
if c.checkVersioningStatus(bucket) == "Enabled" {
log.Infof("Versioning is enabled for bucket %s\n", bucket)
return c.listObjectVersions(&bucket)
}
for index, object := range output.Contents {
Expand All @@ -107,14 +108,14 @@ func (c *S3Client) DeleteS3BucketObjectVersion(object_name string, version_id st
VersionId: &version_id,
})
if err != nil {
panic("unable to delete object version, " + err.Error())
log.Errorf("Unable to delete object version, %v" + err.Error())
}
}

func (c *S3Client) UploadS3BucketObjects(object_file_path string) string {
file, err := os.Open(object_file_path)
if err != nil {
panic("Error opening file:" + err.Error())
log.Fatalf("Error opening file: %v" + err.Error())
}
defer file.Close()

Expand All @@ -127,8 +128,8 @@ func (c *S3Client) UploadS3BucketObjects(object_file_path string) string {
})

if err != nil {
panic("Error uploading file:" + err.Error())
log.Fatalf("Error uploading file: %v" + err.Error())
}
fmt.Printf("File %s uploaded successfully\n", key)
log.Infof("File %s uploaded successfully\n", key)
return *object.VersionId
}

0 comments on commit ab09272

Please sign in to comment.