Skip to content

Commit

Permalink
Merge pull request #106 from bilalcaliskan/devel
Browse files Browse the repository at this point in the history
continuous refactoring
  • Loading branch information
bilalcaliskan authored Jul 12, 2023
2 parents 6150799 + 1b3ddfd commit dd95063
Show file tree
Hide file tree
Showing 38 changed files with 1,362 additions and 2,223 deletions.
12 changes: 6 additions & 6 deletions cmd/bucketpolicy/add/add.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package add

import (
"errors"
"fmt"
"io"
"os"
"strings"

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

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

"github.com/aws/aws-sdk-go/service/s3/s3iface"
Expand Down Expand Up @@ -60,7 +61,6 @@ s3-manager bucketpolicy add my_custom_policy.json
}
}()

// Read the file's content
content, err := io.ReadAll(file)
if err != nil {
logger.Error().Msg(err.Error())
Expand All @@ -80,11 +80,11 @@ s3-manager bucketpolicy add my_custom_policy.json
if !bucketPolicyOpts.AutoApprove {
var res string
if res, err = confirmRunner.Run(); err != nil {
return err
}
if strings.ToLower(res) == "n" {
return constants.ErrUserTerminated
}

if strings.ToLower(res) == "n" {
return errors.New("user terminated the process")
return constants.ErrInvalidInput
}
}

Expand Down
139 changes: 89 additions & 50 deletions cmd/bucketpolicy/add/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"errors"
"testing"

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

"github.com/aws/aws-sdk-go/service/s3/s3iface"

"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"
)

Expand All @@ -20,21 +22,15 @@ var (
defaultPutBucketPolicyErr error
)

func createSvc(rootOpts *options.RootOptions) (*s3.S3, error) {
return internalaws.CreateAwsService(rootOpts)
}

type promptMock struct {
msg string
err error
}

func (p promptMock) Run() (string, error) {
// return expected result
return p.msg, p.err
}

// Define a testdata struct to be used in your unit tests
type mockS3Client struct {
s3iface.S3API
}
Expand All @@ -48,65 +44,120 @@ func TestExecuteAddCmd(t *testing.T) {
AddCmd.SetContext(ctx)

rootOpts := options.GetMockedRootOptions()
svc, err := internalaws.CreateAwsService(rootOpts)
assert.NotNil(t, svc)
assert.Nil(t, err)

cases := []struct {
caseName string
args []string
shouldPass bool
shouldMock bool
svc s3iface.S3API
putBucketPolicyErr error
putBucketPolicyOutput *s3.PutBucketPolicyOutput
promptMock *promptMock
dryRun bool
autoApprove bool
}{
{"Success", []string{"../../../testdata/bucketpolicy.json"},
true, true,
nil, &s3.PutBucketPolicyOutput{},
{
"Success",
[]string{"../../../testdata/bucketpolicy.json"},
true,
&mockS3Client{},
nil,
&s3.PutBucketPolicyOutput{},
&promptMock{
msg: "y",
err: nil,
}, false, false,
},
false,
false,
},
{"Success with dry-run",
{
"Success with dry-run",
[]string{"../../../testdata/bucketpolicy.json"},
true, true,
nil, &s3.PutBucketPolicyOutput{},
true,
&mockS3Client{},
nil,
&s3.PutBucketPolicyOutput{},
&promptMock{
msg: "y",
err: nil,
}, true, false,
},
true,
false,
},
{"Failure", []string{"../../../testdata/bucketpolicy.json"},
false, true,
{
"Failure",
[]string{"../../../testdata/bucketpolicy.json"},
false,
&mockS3Client{},
errors.New("dummy error"),
&s3.PutBucketPolicyOutput{}, nil, false, false,
&s3.PutBucketPolicyOutput{},
nil,
false,
false,
},
{"Failure caused by user terminated process", []string{"../../../testdata/bucketpolicy.json"},
false, true,
nil, &s3.PutBucketPolicyOutput{},
{
"Failure caused by user terminated process",
[]string{"../../../testdata/bucketpolicy.json"},
false,
&mockS3Client{},
nil,
&s3.PutBucketPolicyOutput{},
&promptMock{
msg: "n",
err: nil,
}, false, false,
err: constants.ErrInjected,
},
false,
false,
},
{"Failure caused by prompt error", []string{"../../../testdata/bucketpolicy.json"},
false, true,
nil, &s3.PutBucketPolicyOutput{},
{
"Failure caused by prompt error",
[]string{"../../../testdata/bucketpolicy.json"},
false,
&mockS3Client{},
nil,
&s3.PutBucketPolicyOutput{},
&promptMock{
msg: "nasdasd",
err: errors.New("injected error"),
}, false, false,
err: constants.ErrInjected,
},
false,
false,
},
{"Failure caused by target file not found", []string{"../../../testdata/bucketpolicy.jsonnnn"},
false, true, nil,
&s3.PutBucketPolicyOutput{}, nil, false, false,
{
"Failure caused by target file not found",
[]string{"../../../testdata/bucketpolicy.jsonnnn"},
false,
&mockS3Client{},
nil,
&s3.PutBucketPolicyOutput{},
nil,
false,
false,
},
{"Failure caused by too many arguments error", []string{"enabled", "foo"},
false, false, nil, &s3.PutBucketPolicyOutput{},
nil, false, false,
{
"Failure caused by too many arguments error",
[]string{"enabled", "foo"},
false,
svc,
nil,
&s3.PutBucketPolicyOutput{},
nil,
false,
false,
},
{"Failure caused by no arguments provided error", []string{}, false, false,
nil, &s3.PutBucketPolicyOutput{}, nil, false, false,
{
"Failure caused by no arguments provided error",
[]string{},
false,
svc,
nil,
&s3.PutBucketPolicyOutput{},
nil,
false,
false,
},
}

Expand All @@ -119,23 +170,11 @@ func TestExecuteAddCmd(t *testing.T) {
defaultPutBucketPolicyErr = tc.putBucketPolicyErr
defaultPutBucketPolicyOutput = tc.putBucketPolicyOutput

var err error

if tc.shouldMock {
mockSvc := &mockS3Client{}
svc = mockSvc
assert.NotNil(t, mockSvc)
} else {
svc, err = createSvc(rootOpts)
assert.NotNil(t, svc)
assert.Nil(t, err)
}

if tc.promptMock != nil {
confirmRunner = tc.promptMock
}

AddCmd.SetContext(context.WithValue(AddCmd.Context(), options.S3SvcKey{}, svc))
AddCmd.SetContext(context.WithValue(AddCmd.Context(), options.S3SvcKey{}, tc.svc))
AddCmd.SetContext(context.WithValue(AddCmd.Context(), options.OptsKey{}, rootOpts))
AddCmd.SetArgs(tc.args)

Expand Down
30 changes: 1 addition & 29 deletions cmd/bucketpolicy/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ import (

type BucketPolicyOptsKey struct{}

var (
//substringRunner prompt.PromptRunner = prompt.GetPromptRunner("Provide text to search", nil)
//extensionRunner prompt.PromptRunner = prompt.GetPromptRunner("Provide target file extensions (comma seperated)", nil)
bucketPolicyOpts = &BucketPolicyOptions{}
)
var bucketPolicyOpts = &BucketPolicyOptions{}

// BucketPolicyOptions contains frequent command line and application options.
type BucketPolicyOptions struct {
BucketPolicyContent string
*options.RootOptions
Expand All @@ -23,30 +18,7 @@ func GetBucketPolicyOptions() *BucketPolicyOptions {
return bucketPolicyOpts
}

/*func (opts *BucketPolicyOptions) InitFlags(cmd *cobra.Command) {
cmd.Flags().BoolVarP(&opts.AutoApprove, "auto-approve", "", false, "Skip interactive approval (default false)")
cmd.Flags().BoolVarP(&opts.DryRun, "dry-run", "", false, "specifies that if you "+
"just want to see on what content to take action (default false)")
}*/

func (opts *BucketPolicyOptions) SetZeroValues() {
opts.BucketPolicyContent = ""
opts.RootOptions.SetZeroValues()
}

/*func (opts *ConfigureOptions) PromptInteractiveValues() error {
res, err := substringRunner.Run()
if err != nil {
return err
}
opts.Foo = res
res, err = extensionRunner.Run()
if err != nil {
return err
}
opts.FileExtensions = res
return nil
}
*/
1 change: 0 additions & 1 deletion cmd/bucketpolicy/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

func init() {
bucketPolicyOpts = options.GetBucketPolicyOptions()
//bucketPolicyOpts.InitFlags(RemoveCmd)
}

var (
Expand Down
Loading

0 comments on commit dd95063

Please sign in to comment.