Skip to content

Commit

Permalink
[Refactor]: samples-go to fix linter errors (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaxhveer authored Feb 5, 2024
1 parent a565b39 commit 5193156
Show file tree
Hide file tree
Showing 44 changed files with 551 additions and 494 deletions.
9 changes: 0 additions & 9 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ linters:
# make sure all errors returned by functions are handled
- errcheck

# find unused code
- deadcode

# show how code can be simplified
- gosimple

Expand All @@ -35,12 +32,6 @@ linters:
# find unused variables, functions, structs, types, etc.
- unused

# find unused struct fields
- structcheck

# find unused global variables
- varcheck

# parse and typecheck code
- typecheck

Expand Down
61 changes: 34 additions & 27 deletions S3-Keploy/bucket/bucket.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package bucket contains bucket struct and its methods
package bucket

import (
Expand All @@ -11,27 +12,27 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)

type BucketBasics struct {
type Basics struct {
S3Client *s3.Client
}

func (basics BucketBasics) ListAllBuckets() (buckets []string) {
func (basics Basics) ListAllBuckets() (buckets []string) {
result, err := basics.S3Client.ListBuckets(context.TODO(), &s3.ListBucketsInput{})
if err != nil {
fmt.Printf("Couldn't list buckets for your account. Here's why: %v\n", err)
return
}
if len(result.Buckets) == 0 {
return append(buckets, "You don't have any buckets!")
} else {
for _, bucket := range result.Buckets {
buckets = append(buckets, *bucket.Name)
}
}

for _, bucket := range result.Buckets {
buckets = append(buckets, *bucket.Name)
}
return buckets
}

func (basics BucketBasics) DeleteOneBucket(bucket string) (message string) {
func (basics Basics) DeleteOneBucket(bucket string) (message string) {
var msg string
_, err := basics.S3Client.DeleteBucket(context.TODO(), &s3.DeleteBucketInput{
Bucket: aws.String(bucket)})
Expand All @@ -43,7 +44,7 @@ func (basics BucketBasics) DeleteOneBucket(bucket string) (message string) {
return msg
}

func (basics BucketBasics) CreateOneBucket(bucket string) (message string) {
func (basics Basics) CreateOneBucket(bucket string) (message string) {

_, err := basics.S3Client.CreateBucket(context.TODO(), &s3.CreateBucketInput{
Bucket: aws.String(bucket),
Expand All @@ -61,29 +62,35 @@ func (basics BucketBasics) CreateOneBucket(bucket string) (message string) {
return msg
}

func (basic BucketBasics) UploadFile(fileName string, bucketName string) (message string) {
func (basics Basics) UploadFile(fileName string, bucketName string) (message string) {
file, err := os.Open(fileName)
if err != nil {
log.Printf("Couldn't open file %v to upload. Here's why: %v\n", fileName, err)
return "File " + fileName + " not uploaded"
} else {
defer file.Close()
_, err = basic.S3Client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(fileName),
Body: file,
})
}

defer func() {
err = file.Close()
if err != nil {
log.Printf("Couldn't upload file %v to %v:%v. Here's why: %v\n",
fileName, bucketName, fileName, err)
return "File " + fileName + " not uploaded"
log.Println(err)
}
return "File " + fileName + " uploaded successfully"
}()

_, err = basics.S3Client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(fileName),
Body: file,
})
if err != nil {
log.Printf("Couldn't upload file %v to %v:%v. Here's why: %v\n",
fileName, bucketName, fileName, err)
return "File " + fileName + " not uploaded"
}
return "File " + fileName + " uploaded successfully"
}

func (basic BucketBasics) DeleteAllObjects(bucketName string) (message string) {
result, err := basic.S3Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
func (basics Basics) DeleteAllObjects(bucketName string) (message string) {
result, err := basics.S3Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(bucketName),
})
if err != nil {
Expand All @@ -97,19 +104,19 @@ func (basic BucketBasics) DeleteAllObjects(bucketName string) (message string) {
for _, key := range objectKeys {
objectIds = append(objectIds, types.ObjectIdentifier{Key: aws.String(key)})
}
_, err = basic.S3Client.DeleteObjects(context.TODO(), &s3.DeleteObjectsInput{
_, err = basics.S3Client.DeleteObjects(context.TODO(), &s3.DeleteObjectsInput{
Bucket: aws.String(bucketName),
Delete: &types.Delete{Objects: objectIds},
})
if err != nil {
return "Couldn't delete objects from bucket " + bucketName + " . Here's why: " + err.Error() + "\n"
} else {
return "All objects deleted successfully"
}

return "All objects deleted successfully"
}

func (basic BucketBasics) GetAllObjects(bucketName string) []types.Object {
result, err := basic.S3Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
func (basics Basics) GetAllObjects(bucketName string) []types.Object {
result, err := basics.S3Client.ListObjectsV2(context.TODO(), &s3.ListObjectsV2Input{
Bucket: aws.String(bucketName),
})
var contents []types.Object
Expand Down
5 changes: 3 additions & 2 deletions S3-Keploy/config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package config implements the configuration function
package config

import (
Expand All @@ -9,14 +10,14 @@ import (
"github.com/aws/aws-sdk-go-v2/service/s3"
)

func Configuration() (awsService bucket.BucketBasics) {
func Configuration() (awsService bucket.Basics) {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("ap-south-1"))
if err != nil {
fmt.Printf("error: %v", err)
return
}

awsService = bucket.BucketBasics{
awsService = bucket.Basics{
S3Client: s3.NewFromConfig(cfg),
}

Expand Down
7 changes: 6 additions & 1 deletion S3-Keploy/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Package main starts the application
package main

import (
"S3-Keploy/config"
"S3-Keploy/routes"
"log"

"github.com/gofiber/fiber/v2"
)
Expand All @@ -14,5 +16,8 @@ func main() {

routes.Register(app, awsService)

app.Listen(":3000")
err := app.Listen(":3000")
if err != nil {
log.Fatal(err)
}
}
3 changes: 2 additions & 1 deletion S3-Keploy/routes/routes.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package routes implements the router function
package routes

import (
Expand All @@ -11,7 +12,7 @@ type Bucket struct {
BucketName string `json:"name"`
}

func Register(app *fiber.App, awsService bucket.BucketBasics) {
func Register(app *fiber.App, awsService bucket.Basics) {
app.Get("/list", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"buckets": awsService.ListAllBuckets(),
Expand Down
Loading

0 comments on commit 5193156

Please sign in to comment.