Skip to content

Commit

Permalink
Merge pull request #110 from bilalcaliskan/devel #minor
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
bilalcaliskan authored Jul 17, 2023
2 parents dafd1c9 + 99c3c59 commit 53ce8bf
Show file tree
Hide file tree
Showing 16 changed files with 673 additions and 243 deletions.
20 changes: 1 addition & 19 deletions cmd/bucketpolicy/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/bilalcaliskan/s3-manager/internal/constants"

rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options"

Expand Down Expand Up @@ -72,24 +69,9 @@ s3-manager bucketpolicy add my_custom_policy.json

logger.Info().Msg("will attempt to add below bucket policy")
fmt.Println(bucketPolicyOpts.BucketPolicyContent)
if bucketPolicyOpts.DryRun {
logger.Info().Msg("skipping operation since '--dry-run' flag is passed")
return nil
}

if !bucketPolicyOpts.AutoApprove {
var res string
if res, err = confirmRunner.Run(); err != nil {
if strings.ToLower(res) == "n" {
return constants.ErrUserTerminated
}

return constants.ErrInvalidInput
}
}

logger.Info().Msg("trying to add bucket policy")
_, err = aws.SetBucketPolicy(svc, bucketPolicyOpts)
_, err = aws.SetBucketPolicy(svc, bucketPolicyOpts, confirmRunner, logger)
if err != nil {
logger.Error().
Str("error", err.Error()).
Expand Down
43 changes: 1 addition & 42 deletions cmd/bucketpolicy/add/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (

"github.com/bilalcaliskan/s3-manager/internal/prompt"

internalaws "github.com/bilalcaliskan/s3-manager/internal/aws"
"github.com/bilalcaliskan/s3-manager/internal/constants"

"github.com/aws/aws-sdk-go/service/s3"
"github.com/bilalcaliskan/s3-manager/cmd/root/options"
internalaws "github.com/bilalcaliskan/s3-manager/internal/aws"
"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -54,19 +52,6 @@ func TestExecuteAddCmd(t *testing.T) {
false,
false,
},
{
"Success with dry-run",
[]string{"../../../testdata/bucketpolicy.json"},
true,
nil,
&s3.PutBucketPolicyOutput{},
prompt.PromptMock{
Msg: "y",
Err: nil,
},
true,
false,
},
{
"Failure",
[]string{"../../../testdata/bucketpolicy.json"},
Expand All @@ -80,32 +65,6 @@ func TestExecuteAddCmd(t *testing.T) {
false,
false,
},
{
"Failure caused by user terminated process",
[]string{"../../../testdata/bucketpolicy.json"},
false,
nil,
&s3.PutBucketPolicyOutput{},
prompt.PromptMock{
Msg: "n",
Err: constants.ErrInjected,
},
false,
false,
},
{
"Failure caused by prompt error",
[]string{"../../../testdata/bucketpolicy.json"},
false,
nil,
&s3.PutBucketPolicyOutput{},
prompt.PromptMock{
Msg: "nasdasd",
Err: constants.ErrInjected,
},
false,
false,
},
{
"Failure caused by target file not found",
[]string{"../../../testdata/bucketpolicy.jsonnnn"},
Expand Down
20 changes: 1 addition & 19 deletions cmd/bucketpolicy/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package remove

import (
"fmt"
"strings"

rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options"

"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/bilalcaliskan/s3-manager/cmd/bucketpolicy/options"
"github.com/bilalcaliskan/s3-manager/internal/utils"

"github.com/bilalcaliskan/s3-manager/internal/constants"

"github.com/bilalcaliskan/s3-manager/internal/aws"
"github.com/bilalcaliskan/s3-manager/internal/prompt"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -59,24 +56,9 @@ s3-manager bucketpolicy remove

logger.Info().Msg("will attempt to delete below bucket policy")
fmt.Println(bucketPolicyOpts.BucketPolicyContent)
if bucketPolicyOpts.DryRun {
logger.Info().Msg("skipping operation since '--dry-run' flag is passed")
return nil
}

if !bucketPolicyOpts.AutoApprove {
var res string
if res, err = confirmRunner.Run(); err != nil {
if strings.ToLower(res) == "n" {
return constants.ErrUserTerminated
}

return constants.ErrInvalidInput
}
}

logger.Info().Msg("trying to remove current bucket policy if exists")
_, err = aws.DeleteBucketPolicy(svc, bucketPolicyOpts)
_, err = aws.DeleteBucketPolicy(svc, bucketPolicyOpts, confirmRunner, logger)
if err != nil {
logger.Error().
Str("error", err.Error()).
Expand Down
20 changes: 1 addition & 19 deletions cmd/tags/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"strings"

"github.com/bilalcaliskan/s3-manager/internal/constants"

rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options"
"github.com/bilalcaliskan/s3-manager/internal/utils"

Expand Down Expand Up @@ -84,23 +82,7 @@ s3-manager tags add foo1=bar1,foo2=bar2
fmt.Printf("%s=%s\n", i, v)
}

if tagOpts.DryRun {
logger.Info().Msg("skipping operation since '--dry-run' flag is passed")
return nil
}

if !tagOpts.AutoApprove {
var res string
if res, err = confirmRunner.Run(); err != nil {
if strings.ToLower(res) == "n" {
return constants.ErrUserTerminated
}

return constants.ErrInvalidInput
}
}

if _, err := aws.SetBucketTags(svc, tagOpts); err != nil {
if err := aws.SetBucketTags(svc, tagOpts, confirmRunner, logger); err != nil {
logger.Error().
Str("error", err.Error()).
Msg("an error occurred while setting tags")
Expand Down
22 changes: 2 additions & 20 deletions cmd/tags/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"strings"

"github.com/bilalcaliskan/s3-manager/internal/constants"

rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options"

"github.com/bilalcaliskan/s3-manager/internal/prompt"
Expand Down Expand Up @@ -99,33 +97,17 @@ s3-manager tags remove foo1=bar1,foo2=bar2
fmt.Printf(outputStr, i, v)
}

if tagOpts.DryRun {
logger.Info().Msg("skipping operation since '--dry-run' flag is passed")
return nil
}

if !tagOpts.AutoApprove {
var res string
if res, err = confirmRunner.Run(); err != nil {
if strings.ToLower(res) == "n" {
return constants.ErrUserTerminated
}

return constants.ErrInvalidInput
}
}

utils.RemoveMapElements(tagOpts.ActualTags, tagOpts.TagsToRemove)

if _, err := aws.DeleteAllBucketTags(svc, tagOpts); err != nil {
if _, err := aws.DeleteAllBucketTags(svc, tagOpts, confirmRunner, logger); err != nil {
logger.Error().
Str("error", err.Error()).
Msg("an error occurred while deleting all the tags")
return err
}

tagOpts.TagsToAdd = tagOpts.ActualTags
if _, err := aws.SetBucketTags(svc, tagOpts); err != nil {
if err := aws.SetBucketTags(svc, tagOpts, confirmRunner, logger); err != nil {
logger.Error().
Str("error", err.Error()).
Msg("an error occurred while setting desired tags")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/bilalcaliskan/s3-manager
go 1.20

require (
github.com/aws/aws-sdk-go v1.44.298
github.com/aws/aws-sdk-go v1.44.300
github.com/dimiro1/banner v1.1.0
github.com/manifoldco/promptui v0.9.0
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/aws/aws-sdk-go v1.44.298 h1:5qTxdubgV7PptZJmp/2qDwD2JL187ePL7VOxsSh1i3g=
github.com/aws/aws-sdk-go v1.44.298/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/aws-sdk-go v1.44.300 h1:Zn+3lqgYahIf9yfrwZ+g+hq/c3KzUBaQ8wqY/ZXiAbY=
github.com/aws/aws-sdk-go v1.44.300/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM=
Expand Down
Loading

0 comments on commit 53ce8bf

Please sign in to comment.