Skip to content

Commit

Permalink
fix: broken test and bad region
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Dec 29, 2023
1 parent ff184aa commit e5a0cb3
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app/domain/model/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,9 @@ func TestBucketSets_Contains(t *testing.T) {
}

func TestBucket_TrimKey(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skip this test on Windows")
}
t.Parallel()

tests := []struct {
Expand Down
15 changes: 13 additions & 2 deletions app/interactor/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (s *S3BucketObjectsLister) ListS3BucketObjects(ctx context.Context, input *
// S3BucketObjectsDeleter implements the S3BucketObjectsDeleter interface.
type S3BucketObjectsDeleter struct {
service.S3BucketObjectsDeleter
service.S3BucketLocationGetter
}

// S3BucketObjectsDeleterSet is a provider set for S3BucketObjectsDeleter.
Expand All @@ -152,9 +153,10 @@ var S3BucketObjectsDeleterSet = wire.NewSet(
var _ usecase.S3BucketObjectsDeleter = (*S3BucketObjectsDeleter)(nil)

// NewS3BucketObjectsDeleter creates a new S3BucketObjectsDeleter.
func NewS3BucketObjectsDeleter(d service.S3BucketObjectsDeleter) *S3BucketObjectsDeleter {
func NewS3BucketObjectsDeleter(d service.S3BucketObjectsDeleter, l service.S3BucketLocationGetter) *S3BucketObjectsDeleter {
return &S3BucketObjectsDeleter{
S3BucketObjectsDeleter: d,
S3BucketLocationGetter: l,
}
}

Expand All @@ -163,8 +165,17 @@ func (s *S3BucketObjectsDeleter) DeleteS3BucketObjects(ctx context.Context, inpu
if err := input.Bucket.Validate(); err != nil {
return nil, err
}
_, err := s.S3BucketObjectsDeleter.DeleteS3BucketObjects(ctx, &service.S3BucketObjectsDeleterInput{

location, err := s.S3BucketLocationGetter.GetS3BucketLocation(ctx, &service.S3BucketLocationGetterInput{
Bucket: input.Bucket,
})
if err != nil {
return nil, err
}

_, err = s.S3BucketObjectsDeleter.DeleteS3BucketObjects(ctx, &service.S3BucketObjectsDeleterInput{
Bucket: input.Bucket,
Region: location.Region,
S3ObjectSets: input.S3ObjectSets,
})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/subcmd/s3hub/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func newLsCmd() *cobra.Command {
}
cmd.Flags().StringP("profile", "p", "", "AWS profile name. if this is empty, use $AWS_PROFILE")
// not used. however, this is common flag.
cmd.Flags().StringP("region", "r", model.RegionUSEast1.String(), "AWS region name")
cmd.Flags().StringP("region", "r", "", "AWS region name, default is us-east-1")
return cmd
}

Expand Down
13 changes: 4 additions & 9 deletions cmd/subcmd/s3hub/mb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func newMbCmd() *cobra.Command {
},
}
cmd.Flags().StringP("profile", "p", "", "AWS profile name. if this is empty, use $AWS_PROFILE")
cmd.Flags().StringP("region", "r", model.RegionUSEast1.String(), "AWS region name")
cmd.Flags().StringP("region", "r", "", "AWS region name, default is us-east-1")
return cmd
}

Expand All @@ -32,8 +32,6 @@ type mbCmd struct {
*s3hub
// bucket is the name of the bucket to create.
bucket model.Bucket
// region is the AWS region name.
region model.Region
}

// Parse parses command line arguments.
Expand All @@ -44,25 +42,22 @@ func (m *mbCmd) Parse(cmd *cobra.Command, args []string) error {
m.bucket = model.Bucket(args[0])

m.s3hub = newS3hub()
if err := m.s3hub.parse(cmd); err != nil {
return err
}
return nil
return m.s3hub.parse(cmd)
}

// Do executes mb command.
func (m *mbCmd) Do() error {
_, err := m.S3BucketCreator.CreateS3Bucket(m.ctx, &usecase.S3BucketCreatorInput{
Bucket: m.bucket,
Region: m.region,
Region: m.s3hub.region,
})
if err != nil {
return errfmt.Wrap(err, "can not create bucket")
}

m.printf("[Success]\n")
m.printf(" profile: %s\n", m.profile.String())
m.printf(" region : %s\n", m.region)
m.printf(" region : %s\n", m.s3hub.region)
m.printf(" bucket : %s\n", color.YellowString("%s", m.bucket))
return nil
}
23 changes: 13 additions & 10 deletions cmd/subcmd/s3hub/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func newRmCmd() *cobra.Command {
}
cmd.Flags().StringP("profile", "p", "", "AWS profile name. if this is empty, use $AWS_PROFILE")
// not used. however, this is common flag.
cmd.Flags().StringP("region", "r", model.RegionUSEast1.String(), "AWS region name")
cmd.Flags().StringP("region", "r", "", "AWS region name, default is us-east-1")
cmd.Flags().BoolP("force", "f", false, "Force delete")
return cmd
}
Expand Down Expand Up @@ -90,7 +90,7 @@ func (r *rmCmd) remove(bucket model.Bucket, key model.S3Key) error {
// delete bucket and all objects
if key.Empty() {
if !r.force {
if !subcmd.Question(r.command.OutOrStdout(), fmt.Sprintf("delete %s with objects?", bucket)) {
if !subcmd.Question(r.command.OutOrStdout(), fmt.Sprintf("delete %s with objects?", color.YellowString("%s", bucket))) {
return nil
}
}
Expand All @@ -100,31 +100,34 @@ func (r *rmCmd) remove(bucket model.Bucket, key model.S3Key) error {
if err := r.removeBucket(bucket); err != nil {
return err
}
r.printf("deleted %s\n", color.YellowString("%s", bucket))
return nil
}

// delete all objects in bucket
if key.IsAll() {
if !r.force {
if !subcmd.Question(r.command.OutOrStdout(), fmt.Sprintf("delete all objects in %s? (retains bucket)", bucket)) {
if !subcmd.Question(r.command.OutOrStdout(), fmt.Sprintf("delete all objects in %s? (retains bucket)", color.YellowString("%s", bucket))) {
return nil
}
}
if err := r.removeObjects(bucket); err != nil {
return err
}
r.printf("deleted %s with objects\n", color.YellowString("%s", bucket))
return nil
}

// delete a object in bucket
if !r.force {
if !subcmd.Question(r.command.OutOrStdout(), fmt.Sprintf("delete %s", filepath.Join(bucket.String(), key.String()))) {
if !subcmd.Question(r.command.OutOrStdout(), fmt.Sprintf("delete %s", color.YellowString(filepath.Join(bucket.String(), key.String())))) {
return nil
}
}
if err := r.removeObject(bucket, key); err != nil {
return err
}
r.printf("deleted %s\n", color.YellowString(filepath.Join(bucket.String(), key.String())))
return nil
}

Expand Down Expand Up @@ -160,7 +163,6 @@ func (r *rmCmd) removeObjects(bucket model.Bucket) error {
sem := semaphore.NewWeighted(model.MaxS3DeleteObjectsParallelsCount)
chunks := r.divideIntoChunks(output.Objects, model.S3DeleteObjectChunksSize)

r.command.Println(color.YellowString("delete %d objects in %s", output.Objects.Len(), bucket))
bar := progressbar.Default(int64(output.Objects.Len()))
for _, chunk := range chunks {
chunk := chunk // Create a new variable to avoid concurrency issues
Expand All @@ -177,13 +179,13 @@ func (r *rmCmd) removeObjects(bucket model.Bucket) error {
}); err != nil {
return err
}
bar.Add(len(chunk))
return nil
return bar.Add(len(chunk))
})
}
if err := eg.Wait(); err != nil {
return err
}
r.printf("delete %s objects in %s", color.YellowString("%d", output.Objects.Len()), color.YellowString("%s", bucket))
return nil
}

Expand Down Expand Up @@ -222,14 +224,15 @@ func (r *rmCmd) existBuckets() error {
}

notExistBuckets := make([]string, 0, len(r.buckets))
for _, b := range r.buckets {
if output.Buckets.Contains(b.TrimKey()) {
for _, bucket := range r.buckets {
if output.Buckets.Contains(bucket.TrimKey()) {
continue
}
b, _ := bucket.Split()
notExistBuckets = append(notExistBuckets, b.String())
}
if len(notExistBuckets) == 0 {
return nil
}
return fmt.Errorf("bucket does not exist: %s", color.YellowString(strings.Join(notExistBuckets, ", ")))
return fmt.Errorf("s3 bucket does not exist: %s", color.YellowString(strings.Join(notExistBuckets, ", ")))
}
4 changes: 2 additions & 2 deletions utils/errfmt/errfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

// Wrap return wrapping error with message.
// If e is nil, return new error with msg. If msg is empty string, return e.
// For example: Wrap(errors.New("original error"), "add message") returns "original error: add message".
// For example: Wrap(errors.New("original error"), "add message") returns "add message: original error".
func Wrap(e error, msg string) error {
if e == nil {
return errors.New(msg)
}
if msg == "" {
return e
}
return fmt.Errorf("%w: %s", e, msg)
return fmt.Errorf("%s: %w", msg, e)
}
2 changes: 1 addition & 1 deletion utils/errfmt/errfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestWrap(t *testing.T) {
e: errors.New("original error"),
msg: "add message",
},
wantErrMsg: "original error: add message",
wantErrMsg: "add message: original error",
},
{
name: "make new error because user not specify nil for error",
Expand Down

0 comments on commit e5a0cb3

Please sign in to comment.