Skip to content

Commit

Permalink
Merge pull request #101 from bilalcaliskan/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
bilalcaliskan authored Jul 9, 2023
2 parents 06c1c80 + 8e0b4ac commit 5267106
Show file tree
Hide file tree
Showing 47 changed files with 822 additions and 1,287 deletions.
22 changes: 8 additions & 14 deletions cmd/bucketpolicy/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"os"
"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/cmd/bucketpolicy/utils"
"github.com/bilalcaliskan/s3-manager/internal/aws"
"github.com/bilalcaliskan/s3-manager/internal/prompt"
"github.com/bilalcaliskan/s3-manager/internal/utils"
"github.com/rs/zerolog"
"github.com/spf13/cobra"
)
Expand All @@ -28,29 +30,21 @@ var (
AddCmd = &cobra.Command{
Use: "add",
Short: "adds a bucket policy configuration for the target bucket by specifying a valid policy file",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# add a bucket policy configuration onto target bucket
s3-manager bucketpolicy add my_custom_policy.json
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
svc, bucketPolicyOpts, logger = utils.PrepareConstants(cmd, options.GetBucketPolicyOptions())
var rootOpts *rootopts.RootOptions
svc, rootOpts, logger = utils.PrepareConstants(cmd)
bucketPolicyOpts.RootOptions = rootOpts

if len(args) < 1 {
err = errors.New("no argument provided, you should provide bucket policy path on your filesystem")
logger.Error().Msg(err.Error())
return err
} else if len(args) > 1 {
err = errors.New("too many argument provided, just provide bucket policy path on your filesystem")
if err := utils.CheckArgs(args, 1); err != nil {
logger.Error().Msg(err.Error())
return err
}

/*if err := utils.CheckArgs(cmd, args); err != nil {
logger.Error().Msg(err.Error())
return err
}*/

logger = logger.With().Str("policyFilePath", args[0]).Logger()

logger.Info().Msg("trying to read target policy file")
Expand Down
4 changes: 2 additions & 2 deletions cmd/bucketpolicy/bucketpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
BucketPolicyCmd = &cobra.Command{
Use: "bucketpolicy",
Short: "shows/sets the bucket policy configuration of the target bucket",
SilenceUsage: true,
SilenceErrors: true,
SilenceUsage: false,
SilenceErrors: false,
}
)
26 changes: 16 additions & 10 deletions cmd/bucketpolicy/remove/remove.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package remove

import (
"errors"
"fmt"
"strings"

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

"github.com/aws/aws-sdk-go/service/s3/s3iface"
options "github.com/bilalcaliskan/s3-manager/cmd/bucketpolicy/options"
"github.com/bilalcaliskan/s3-manager/cmd/bucketpolicy/utils"
"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 All @@ -27,15 +31,17 @@ var (
RemoveCmd = &cobra.Command{
Use: "remove",
Short: "removes the current bucket policy configuration of the target bucket",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# remove the current bucket policy configuration onto target bucket
s3-manager bucketpolicy remove
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
svc, bucketPolicyOpts, logger = utils.PrepareConstants(cmd, options.GetBucketPolicyOptions())
var rootOpts *rootopts.RootOptions
svc, rootOpts, logger = utils.PrepareConstants(cmd)
bucketPolicyOpts.RootOptions = rootOpts

if err := utils.CheckArgs(args); err != nil {
if err := utils.CheckArgs(args, 0); err != nil {
logger.Error().
Msg(err.Error())
return err
Expand All @@ -62,11 +68,11 @@ s3-manager bucketpolicy remove
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
6 changes: 4 additions & 2 deletions cmd/bucketpolicy/remove/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"errors"
"testing"

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

"github.com/aws/aws-sdk-go/aws"

"github.com/aws/aws-sdk-go/service/s3/s3iface"
Expand Down Expand Up @@ -145,7 +147,7 @@ func TestExecuteRemoveCmd(t *testing.T) {
nil,
&s3.DeleteBucketPolicyOutput{}, &promptMock{
msg: "n",
err: nil,
err: constants.ErrInjected,
}, false, false,
},
{"Failure caused by prompt error", []string{}, false, true,
Expand All @@ -155,7 +157,7 @@ func TestExecuteRemoveCmd(t *testing.T) {
nil,
&s3.DeleteBucketPolicyOutput{}, &promptMock{
msg: "nasdfadf",
err: errors.New("injected error"),
err: constants.ErrInjected,
}, false, false,
},
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/bucketpolicy/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package show
import (
"fmt"

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

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

options2 "github.com/bilalcaliskan/s3-manager/cmd/bucketpolicy/options"
"github.com/bilalcaliskan/s3-manager/internal/aws"
Expand All @@ -24,15 +26,17 @@ var (
ShowCmd = &cobra.Command{
Use: "show",
Short: "shows the bucket policy configuration of the target bucket",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# show the current bucket policy configuration for target bucket
s3-manager bucketpolicy show
`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
svc, bucketPolicyOpts, logger = utils.PrepareConstants(cmd, options2.GetBucketPolicyOptions())
var rootOpts *rootopts.RootOptions
svc, rootOpts, logger = utils.PrepareConstants(cmd)
bucketPolicyOpts.RootOptions = rootOpts

if err := utils.CheckArgs(args); err != nil {
if err := utils.CheckArgs(args, 0); err != nil {
logger.Error().
Msg(err.Error())
return err
Expand Down
32 changes: 0 additions & 32 deletions cmd/bucketpolicy/utils/utils.go

This file was deleted.

72 changes: 0 additions & 72 deletions cmd/bucketpolicy/utils/utils_test.go

This file was deleted.

18 changes: 8 additions & 10 deletions cmd/clean/clean.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package clean

import (
"errors"
"fmt"

"github.com/aws/aws-sdk-go/service/s3/s3iface"
Expand All @@ -27,18 +26,12 @@ var (
ValidSortByOpts = []string{"size", "lastModificationDate"}
cleanOpts *options.CleanOptions
svc s3iface.S3API
promptRunner prompt.PromptRunner = prompt.GetPromptRunner("Delete Files? (y/N)", true, func(s string) error {
if len(s) == 1 {
return nil
}

return errors.New("invalid input")
})
confirmRunner prompt.PromptRunner = prompt.GetConfirmRunner()
// CleanCmd represents the clean command
CleanCmd = &cobra.Command{
Use: "clean",
Short: "finds and clears desired files by a pre-configured rule set",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# clean the desired files on target bucket
s3-manager clean --min-size-mb=1 --max-size-mb=1000 --keep-last-n-files=2 --sort-by=lastModificationDate
Expand All @@ -50,6 +43,11 @@ s3-manager clean --min-size-mb=1 --max-size-mb=1000 --keep-last-n-files=2 --sort
cleanOpts.RootOptions = rootOpts
logger = logging.GetLogger(rootOpts)

if err := utils.CheckArgs(args, 0); err != nil {
logger.Error().Msg(err.Error())
return err
}

if cleanOpts.MinFileSizeInMb > cleanOpts.MaxFileSizeInMb && (cleanOpts.MinFileSizeInMb != 0 && cleanOpts.MaxFileSizeInMb != 0) {
err := fmt.Errorf("flag '--min-size-mb' must be equal or lower than '--max-size-mb'")
logger.Error().Str("error", err.Error()).Msg("an error occured while validating flags")
Expand All @@ -65,7 +63,7 @@ s3-manager clean --min-size-mb=1 --max-size-mb=1000 --keep-last-n-files=2 --sort

logger.Info().Msg("trying to search files on target bucket")

if err := cleaner.StartCleaning(svc, promptRunner, cleanOpts, logger); err != nil {
if err := cleaner.StartCleaning(svc, confirmRunner, cleanOpts, logger); err != nil {
logger.Error().Str("error", err.Error()).Msg("an error occurred while cleaning")
return err
}
Expand Down
7 changes: 0 additions & 7 deletions cmd/clean/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ type CleanOptions struct {
FileExtensions string
FileNamePrefix string
KeepLastNFiles int
DryRun bool
AutoApprove bool
SortBy string
*options.RootOptions
}
Expand All @@ -33,9 +31,6 @@ func (opts *CleanOptions) InitFlags(cmd *cobra.Command) {
"defines how many of the files to skip deletion in specified criteria, 0 means clean them all")
cmd.Flags().StringVarP(&opts.SortBy, "sort-by", "", "lastModificationDate",
"defines the ascending order in the specified criteria, valid options are \"lastModificationDate\" and \"size\"")
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 what to delete or completely delete them all (default false)")
}

// GetCleanOptions returns the pointer of CleanOptions
Expand All @@ -48,8 +43,6 @@ func (opts *CleanOptions) SetZeroValues() {
opts.MaxFileSizeInMb = 0
opts.FileExtensions = ""
opts.KeepLastNFiles = 2
opts.DryRun = false
opts.AutoApprove = false
opts.SortBy = "lastModificationDate"
opts.RootOptions.SetZeroValues()
}
10 changes: 6 additions & 4 deletions cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ var (
ver = version.Get()
logger zerolog.Logger
rootCmd = &cobra.Command{
Use: "s3-manager",
Short: "configure subcommand configures the bucket level settings",
Long: ``,
Version: ver.GitVersion,
Use: "s3-manager",
Short: "configure subcommand configures the bucket level settings",
Long: ``,
Version: ver.GitVersion,
SilenceUsage: false,
SilenceErrors: false,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// TODO: uncomment when interactivity enabled again
/*if !opts.Interactive {
Expand Down
Loading

0 comments on commit 5267106

Please sign in to comment.