generated from bilalcaliskan/golang-cli-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from bilalcaliskan/devel
Devel
- Loading branch information
Showing
12 changed files
with
746 additions
and
409 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
- [x] versioning/show | ||
- [x] versioning/set | ||
- [ ] search/file | ||
- [x] versioning | ||
- [x] search/file | ||
- [x] tags | ||
- [NO] storageclass | ||
- [NO] encryption | ||
- [ ] search/text (all files, single file etc.) | ||
- [ ] tags | ||
- [ ] encryption | ||
- [ ] bucketpolicy | ||
- [ ] objectlock | ||
- [ ] re-enable prompt/select logic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package disabled | ||
|
||
import ( | ||
"github.com/bilalcaliskan/s3-manager/cmd/versioning/set/utils" | ||
|
||
"github.com/aws/aws-sdk-go/service/s3/s3iface" | ||
rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options" | ||
"github.com/bilalcaliskan/s3-manager/cmd/versioning/options" | ||
"github.com/bilalcaliskan/s3-manager/internal/aws" | ||
"github.com/bilalcaliskan/s3-manager/internal/logging" | ||
"github.com/rs/zerolog" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
versioningOpts = options.GetVersioningOptions() | ||
} | ||
|
||
var ( | ||
svc s3iface.S3API | ||
logger zerolog.Logger | ||
versioningOpts *options.VersioningOptions | ||
DisabledCmd = &cobra.Command{ | ||
Use: "disabled", | ||
Short: "", | ||
SilenceUsage: true, | ||
SilenceErrors: true, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
rootOpts := cmd.Context().Value(rootopts.OptsKey{}).(*rootopts.RootOptions) | ||
svc = cmd.Context().Value(rootopts.S3SvcKey{}).(s3iface.S3API) | ||
|
||
versioningOpts.RootOptions = rootOpts | ||
logger = logging.GetLogger(rootOpts) | ||
|
||
if err := utils.CheckArgs(args); err != nil { | ||
logger.Error(). | ||
Msg(err.Error()) | ||
return err | ||
} | ||
|
||
versioningOpts.DesiredState = "disabled" | ||
versioning, err := aws.GetBucketVersioning(svc, versioningOpts.RootOptions) | ||
if err != nil { | ||
logger.Error().Msg(err.Error()) | ||
return err | ||
} | ||
|
||
if err := utils.DecideActualState(versioning, versioningOpts); err != nil { | ||
logger.Error().Msg(err.Error()) | ||
return err | ||
} | ||
|
||
logger.Info().Msgf(utils.InfCurrentState, versioningOpts.ActualState) | ||
if versioningOpts.ActualState == "disabled" { | ||
logger.Warn(). | ||
Str("state", versioningOpts.ActualState). | ||
Msg(utils.WarnDesiredState) | ||
return nil | ||
} | ||
|
||
logger.Info().Msgf(utils.InfSettingVersioning, versioningOpts.DesiredState) | ||
_, err = aws.SetBucketVersioning(svc, versioningOpts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Info().Msgf(utils.InfSuccess, versioningOpts.DesiredState) | ||
|
||
return nil | ||
}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
package disabled | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/bilalcaliskan/s3-manager/cmd/versioning/set/utils" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/s3" | ||
"github.com/aws/aws-sdk-go/service/s3/s3iface" | ||
"github.com/bilalcaliskan/s3-manager/cmd/root/options" | ||
internalaws "github.com/bilalcaliskan/s3-manager/internal/aws" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
defaultGetBucketVersioningOutput = &s3.GetBucketVersioningOutput{ | ||
Status: aws.String("Enabled"), | ||
} | ||
defaultGetBucketVersioningErr error | ||
defaultPutBucketVersioningOutput = &s3.PutBucketVersioningOutput{} | ||
defaultPutBucketVersioningErr error | ||
) | ||
|
||
func createSvc(rootOpts *options.RootOptions) (*s3.S3, error) { | ||
return internalaws.CreateAwsService(rootOpts) | ||
} | ||
|
||
// Define a mock struct to be used in your unit tests | ||
type mockS3Client struct { | ||
s3iface.S3API | ||
} | ||
|
||
func (m *mockS3Client) GetBucketVersioning(input *s3.GetBucketVersioningInput) (*s3.GetBucketVersioningOutput, error) { | ||
return defaultGetBucketVersioningOutput, defaultGetBucketVersioningErr | ||
} | ||
|
||
func (m *mockS3Client) PutBucketVersioning(input *s3.PutBucketVersioningInput) (*s3.PutBucketVersioningOutput, error) { | ||
return defaultPutBucketVersioningOutput, defaultPutBucketVersioningErr | ||
} | ||
|
||
func TestExecuteTooManyArguments(t *testing.T) { | ||
rootOpts := options.GetRootOptions() | ||
rootOpts.AccessKey = "thisisaccesskey" | ||
rootOpts.SecretKey = "thisissecretkey" | ||
rootOpts.Region = "thisisregion" | ||
rootOpts.BucketName = "thisisbucketname" | ||
|
||
ctx := context.Background() | ||
DisabledCmd.SetContext(ctx) | ||
svc, err := createSvc(rootOpts) | ||
assert.NotNil(t, svc) | ||
assert.Nil(t, err) | ||
|
||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) | ||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) | ||
|
||
args := []string{"enabled"} | ||
DisabledCmd.SetArgs(args) | ||
|
||
err = DisabledCmd.Execute() | ||
assert.NotNil(t, err) | ||
assert.Equal(t, utils.ErrTooManyArguments, err.Error()) | ||
|
||
rootOpts.SetZeroValues() | ||
versioningOpts.SetZeroValues() | ||
} | ||
|
||
/*func TestExecuteWrongArguments(t *testing.T) { | ||
rootOpts := options.GetRootOptions() | ||
rootOpts.AccessKey = "thisisaccesskey" | ||
rootOpts.SecretKey = "thisissecretkey" | ||
rootOpts.Region = "thisisregion" | ||
rootOpts.BucketName = "thisisbucketname" | ||
ctx := context.Background() | ||
DisabledCmd.SetContext(ctx) | ||
svc, err := createSvc(rootOpts) | ||
assert.NotNil(t, svc) | ||
assert.Nil(t, err) | ||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) | ||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) | ||
args := []string{"eeenabled"} | ||
DisabledCmd.SetArgs(args) | ||
err = DisabledCmd.Execute() | ||
assert.NotNil(t, err) | ||
assert.Equal(t, ErrWrongArgumentProvided, err.Error()) | ||
rootOpts.SetZeroValues() | ||
versioningOpts.SetZeroValues() | ||
}*/ | ||
|
||
/* | ||
func TestExecuteNoArgument(t *testing.T) { | ||
rootOpts := options.GetRootOptions() | ||
rootOpts.AccessKey = "thisisaccesskey" | ||
rootOpts.SecretKey = "thisissecretkey" | ||
rootOpts.Region = "thisisregion" | ||
rootOpts.BucketName = "thisisbucketname" | ||
ctx := context.Background() | ||
DisabledCmd.SetContext(ctx) | ||
svc, err := createSvc(rootOpts) | ||
assert.NotNil(t, svc) | ||
assert.Nil(t, err) | ||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) | ||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) | ||
DisabledCmd.SetArgs([]string{}) | ||
err = DisabledCmd.Execute() | ||
assert.NotNil(t, err) | ||
assert.Equal(t, ErrNoArgument, err.Error()) | ||
rootOpts.SetZeroValues() | ||
versioningOpts.SetZeroValues() | ||
} | ||
*/ | ||
func TestExecuteSuccessAlreadyDisabled(t *testing.T) { | ||
rootOpts := options.GetRootOptions() | ||
rootOpts.AccessKey = "thisisaccesskey" | ||
rootOpts.SecretKey = "thisissecretkey" | ||
rootOpts.Region = "thisisregion" | ||
rootOpts.BucketName = "thisisbucketname" | ||
|
||
ctx := context.Background() | ||
DisabledCmd.SetContext(ctx) | ||
|
||
mockSvc := &mockS3Client{} | ||
svc = mockSvc | ||
|
||
defaultGetBucketVersioningErr = nil | ||
defaultGetBucketVersioningOutput.Status = aws.String("Suspended") | ||
defaultPutBucketVersioningErr = nil | ||
|
||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) | ||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) | ||
|
||
DisabledCmd.SetArgs([]string{}) | ||
err := DisabledCmd.Execute() | ||
assert.Nil(t, err) | ||
|
||
rootOpts.SetZeroValues() | ||
versioningOpts.SetZeroValues() | ||
} | ||
|
||
func TestExecuteSuccess(t *testing.T) { | ||
rootOpts := options.GetRootOptions() | ||
rootOpts.AccessKey = "thisisaccesskey" | ||
rootOpts.SecretKey = "thisissecretkey" | ||
rootOpts.Region = "thisisregion" | ||
rootOpts.BucketName = "thisisbucketname" | ||
|
||
ctx := context.Background() | ||
DisabledCmd.SetContext(ctx) | ||
|
||
mockSvc := &mockS3Client{} | ||
svc = mockSvc | ||
|
||
defaultGetBucketVersioningErr = nil | ||
defaultGetBucketVersioningOutput.Status = aws.String("Enabled") | ||
defaultPutBucketVersioningErr = nil | ||
|
||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) | ||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) | ||
|
||
DisabledCmd.SetArgs([]string{}) | ||
err := DisabledCmd.Execute() | ||
assert.Nil(t, err) | ||
|
||
rootOpts.SetZeroValues() | ||
versioningOpts.SetZeroValues() | ||
} | ||
|
||
func TestExecuteGetBucketVersioningErr(t *testing.T) { | ||
rootOpts := options.GetRootOptions() | ||
rootOpts.AccessKey = "thisisaccesskey" | ||
rootOpts.SecretKey = "thisissecretkey" | ||
rootOpts.Region = "thisisregion" | ||
rootOpts.BucketName = "thisisbucketname" | ||
|
||
ctx := context.Background() | ||
DisabledCmd.SetContext(ctx) | ||
|
||
mockSvc := &mockS3Client{} | ||
svc = mockSvc | ||
|
||
defaultGetBucketVersioningErr = errors.New("dummy error") | ||
|
||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) | ||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) | ||
|
||
DisabledCmd.SetArgs([]string{}) | ||
err := DisabledCmd.Execute() | ||
assert.NotNil(t, err) | ||
|
||
rootOpts.SetZeroValues() | ||
versioningOpts.SetZeroValues() | ||
} | ||
|
||
func TestExecuteSetBucketVersioningErr(t *testing.T) { | ||
rootOpts := options.GetRootOptions() | ||
rootOpts.AccessKey = "thisisaccesskey" | ||
rootOpts.SecretKey = "thisissecretkey" | ||
rootOpts.Region = "thisisregion" | ||
rootOpts.BucketName = "thisisbucketname" | ||
|
||
ctx := context.Background() | ||
DisabledCmd.SetContext(ctx) | ||
|
||
mockSvc := &mockS3Client{} | ||
svc = mockSvc | ||
|
||
defaultGetBucketVersioningErr = nil | ||
defaultPutBucketVersioningErr = errors.New("new dummy error") | ||
|
||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) | ||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) | ||
|
||
DisabledCmd.SetArgs([]string{}) | ||
err := DisabledCmd.Execute() | ||
assert.NotNil(t, err) | ||
|
||
rootOpts.SetZeroValues() | ||
versioningOpts.SetZeroValues() | ||
} | ||
|
||
func TestExecuteSuccessEnabledWrongVersioning(t *testing.T) { | ||
rootOpts := options.GetRootOptions() | ||
rootOpts.AccessKey = "thisisaccesskey" | ||
rootOpts.SecretKey = "thisissecretkey" | ||
rootOpts.Region = "thisisregion" | ||
rootOpts.BucketName = "thisisbucketname" | ||
|
||
ctx := context.Background() | ||
DisabledCmd.SetContext(ctx) | ||
|
||
mockSvc := &mockS3Client{} | ||
svc = mockSvc | ||
|
||
defaultGetBucketVersioningErr = nil | ||
defaultGetBucketVersioningOutput.Status = aws.String("Suspendeddd") | ||
defaultPutBucketVersioningErr = nil | ||
|
||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.S3SvcKey{}, svc)) | ||
DisabledCmd.SetContext(context.WithValue(DisabledCmd.Context(), options.OptsKey{}, rootOpts)) | ||
|
||
DisabledCmd.SetArgs([]string{}) | ||
err := DisabledCmd.Execute() | ||
assert.NotNil(t, err) | ||
|
||
rootOpts.SetZeroValues() | ||
versioningOpts.SetZeroValues() | ||
} |
Oops, something went wrong.