Skip to content

Commit

Permalink
refactor: get rid of cmd/utils packages phase
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalcaliskan committed Jul 9, 2023
1 parent d4c6ef9 commit 8e0b4ac
Show file tree
Hide file tree
Showing 26 changed files with 86 additions and 380 deletions.
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,
}
)
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
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
9 changes: 4 additions & 5 deletions cmd/search/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import (

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

"github.com/bilalcaliskan/s3-manager/cmd/search/utils"
internalutils "github.com/bilalcaliskan/s3-manager/internal/utils"
"github.com/bilalcaliskan/s3-manager/internal/utils"

"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/bilalcaliskan/s3-manager/cmd/search/options"
Expand All @@ -26,17 +25,17 @@ var (
FileCmd = &cobra.Command{
Use: "file",
Short: "searches the files which has desired file name pattern in it (supports regex)",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# search a file on target bucket by specifying regex for files
s3-manager search file ".*.json"
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
var rootOpts *rootopts.RootOptions
svc, rootOpts, logger = internalutils.PrepareConstants(cmd)
svc, rootOpts, logger = utils.PrepareConstants(cmd)
searchOpts.RootOptions = rootOpts

if err := utils.CheckFlags(args); err != nil {
if err := utils.CheckArgs(args, 1); err != nil {
logger.Error().Msg(err.Error())
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
Use: "search",
Short: "searches the files which has desired substrings in it",
// we should not define PreRunE since it will override the PreRunE which is inherited from RootCmd
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: false,
}
)
11 changes: 5 additions & 6 deletions cmd/search/text/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package text
import (
"fmt"

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

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

"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/bilalcaliskan/s3-manager/cmd/search/options"
Expand All @@ -27,17 +26,17 @@ var (
TextCmd = &cobra.Command{
Use: "text",
Short: "searches the texts in files which has desired file name pattern and string pattern in it (supports regex)",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# search a text on target bucket by specifying regex for files
s3-manager search text "catch me if you can" --file-name=".*.txt"
`,
PreRunE: func(cmd *cobra.Command, args []string) error {
var rootOpts *rootopts.RootOptions
svc, rootOpts, logger = internalutils.PrepareConstants(cmd)
svc, rootOpts, logger = utils.PrepareConstants(cmd)
searchOpts.RootOptions = rootOpts

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

This file was deleted.

73 changes: 0 additions & 73 deletions cmd/search/utils/utils_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/tags/add/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
AddCmd = &cobra.Command{
Use: "add",
Short: "adds the tagging configuration for the target bucket",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# add comma separated tagging configuration into bucket
s3-manager tags add foo1=bar1,foo2=bar2
Expand Down
2 changes: 1 addition & 1 deletion cmd/tags/remove/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (
RemoveCmd = &cobra.Command{
Use: "remove",
Short: "removes the tagging configuration for the target bucket",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# remove comma separated tagging configuration from bucket
s3-manager tags remove foo1=bar1,foo2=bar2
Expand Down
9 changes: 3 additions & 6 deletions cmd/tags/show/show.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package show

import (
"errors"
"fmt"

"github.com/bilalcaliskan/s3-manager/internal/utils"
Expand All @@ -26,7 +25,7 @@ var (
ShowCmd = &cobra.Command{
Use: "show",
Short: "shows the tagging configuration for the target bucket",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# show the current tagging configuration for bucket
s3-manager tags show
Expand All @@ -36,10 +35,8 @@ s3-manager tags show
svc, rootOpts, logger = utils.PrepareConstants(cmd)
tagOpts.RootOptions = rootOpts

if len(args) > 0 {
err = errors.New("too many arguments provided")
logger.Error().
Msg(err.Error())
if err := utils.CheckArgs(args, 0); err != nil {
logger.Error().Msg(err.Error())
return err
}

Expand Down
5 changes: 2 additions & 3 deletions cmd/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ var (
TagsCmd = &cobra.Command{
Use: "tags",
Short: "shows/sets the tagging configuration of the target bucket",
SilenceUsage: true,
SilenceErrors: true,
// we should not define PreRunE since it will override the PreRunE which is inherited from RootCmd
SilenceUsage: false,
SilenceErrors: false,
}
)
14 changes: 8 additions & 6 deletions cmd/transferacceleration/set/disabled/disabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package disabled

import (
"github.com/aws/aws-sdk-go/service/s3/s3iface"
rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options"
options2 "github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/options"
"github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/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 @@ -22,17 +23,18 @@ var (
DisabledCmd = &cobra.Command{
Use: "disabled",
Short: "disables the transfer acceleration configuration for the target bucket",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# set the transfer acceleration configuration for bucket as disabled
s3-manager transferacceleration set disabled
`,
RunE: func(cmd *cobra.Command, args []string) error {
svc, transferAccelerationOpts, logger = utils.PrepareConstants(cmd, transferAccelerationOpts)
var rootOpts *rootopts.RootOptions
svc, rootOpts, logger = utils.PrepareConstants(cmd)
transferAccelerationOpts.RootOptions = rootOpts

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

Expand Down
11 changes: 7 additions & 4 deletions cmd/transferacceleration/set/enabled/enabled.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package enabled

import (
"github.com/aws/aws-sdk-go/service/s3/s3iface"
rootopts "github.com/bilalcaliskan/s3-manager/cmd/root/options"
"github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/options"
"github.com/bilalcaliskan/s3-manager/cmd/transferacceleration/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 @@ -22,15 +23,17 @@ var (
EnabledCmd = &cobra.Command{
Use: "enabled",
Short: "enables the transfer acceleration configuration for the target bucket",
SilenceUsage: true,
SilenceUsage: false,
SilenceErrors: true,
Example: `# set the transfer acceleration configuration for bucket as enabled
s3-manager transferacceleration set enabled
`,
RunE: func(cmd *cobra.Command, args []string) error {
svc, transferAccelerationOpts, logger = utils.PrepareConstants(cmd, transferAccelerationOpts)
var rootOpts *rootopts.RootOptions
svc, rootOpts, logger = utils.PrepareConstants(cmd)
transferAccelerationOpts.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
4 changes: 2 additions & 2 deletions cmd/transferacceleration/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
SetCmd = &cobra.Command{
Use: "set",
Short: "sets the transfer acceleration configuration for the target bucket (enabled/disabled)",
SilenceUsage: true,
SilenceErrors: true,
SilenceUsage: false,
SilenceErrors: false,
}
)
Loading

0 comments on commit 8e0b4ac

Please sign in to comment.