Skip to content

Commit

Permalink
implement s3 mb subcommnad
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Dec 26, 2023
1 parent 891c82e commit 4a8c34b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 15 deletions.
3 changes: 3 additions & 0 deletions app/interactor/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (s *S3BucketCreator) CreateBucket(ctx context.Context, input *usecase.S3Buc
if err := input.Bucket.Validate(); err != nil {
return nil, err
}
if err := input.Region.Validate(); err != nil {
return nil, err
}

in := service.S3BucketCreatorInput{
Bucket: input.Bucket,
Expand Down
109 changes: 102 additions & 7 deletions cmd/subcmd/s3hub/mb.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,113 @@
package s3hub

import "github.com/spf13/cobra"
import (
"context"
"errors"

"github.com/fatih/color"
"github.com/nao1215/rainbow/app/di"
"github.com/nao1215/rainbow/app/domain/model"
"github.com/nao1215/rainbow/app/usecase"
"github.com/nao1215/rainbow/utils/errfmt"
"github.com/spf13/cobra"
)

// newMbCmd return mb command. mb means make bucket.
func newMbCmd() *cobra.Command {
return &cobra.Command{
Use: "mb",
Short: "Make S3 bucket",
RunE: mb,
cmd := &cobra.Command{
Use: "mb [flags] BUCKET_NAME",
Short: "Make S3 bucket",
Example: " s3hub mb -p myprofile -r us-east-1 BUCKET_NAME",
RunE: mb,
}
cmd.Flags().StringP("profile", "p", "", "AWS profile name. if this is empty, use $AWS_PROFILE")
cmd.Flags().StringP("region", "r", "", "AWS region name. if this is empty, use us-east-1")
return cmd
}

// mbCmd is the command for mb.
type mbCmd struct {
*cobra.Command
// ctx is the context of mb command.
ctx context.Context
// app is the application service for S3.
app *di.S3App
// profile is the AWS profile name.
profile model.AWSProfile
// region is the AWS region name.
region model.Region
// bucket is the name of the bucket to create.
bucket model.Bucket
}

// mb is the entrypoint of mb command.
func mb(cmd *cobra.Command, _ []string) error {
cmd.Println("mb is not implemented yet")
func mb(cmd *cobra.Command, args []string) error {
mb, err := parse(cmd, args)
if err != nil {
return err
}
return mb.do()
}

// parse parses command line arguments.
func parse(cmd *cobra.Command, args []string) (*mbCmd, error) {
if len(args) != 1 {
return nil, errors.New("you must specify a bucket name")
}

ctx := context.Background()
p, err := cmd.Flags().GetString("profile")
if err != nil {
return nil, errfmt.Wrap(err, "can not parse command line argument (--profile)")
}
profile := model.NewAWSProfile(p)

r, err := cmd.Flags().GetString("region")
if err != nil {
return nil, errfmt.Wrap(err, "can not parse command line argument (--region)")
}
region := model.Region(r)

cfg, err := model.NewAWSConfig(ctx, profile, region)
if err != nil {
return nil, errfmt.Wrap(err, "can not get aws config")
}
if region == "" {
if cfg.Config.Region == "" {
region = model.RegionUSEast1
} else {
region = model.Region(cfg.Config.Region)
}
}

app, err := di.NewS3App(ctx, profile, region)
if err != nil {
return nil, errfmt.Wrap(err, "can not create s3 application service")
}

return &mbCmd{
Command: cmd,
ctx: ctx,
app: app,
profile: profile,
region: region,
bucket: model.Bucket(args[0]),
}, nil
}

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

mb.Printf("[Success]\n")
mb.Printf(" profile: %s\n", mb.profile.String())
mb.Printf(" region : %s\n", mb.region)
mb.Printf(" bucket : %s\n", color.YellowString("%s", mb.bucket))
return nil
}
5 changes: 3 additions & 2 deletions cmd/subcmd/s3hub/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ func Execute() error {
// newRootCmd returns a root command for s3hub.
func newRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "s3hub",
Short: "user-friendly S3 buckets management tool",
Use: "s3hub",
Long: `s3hub is user-friendly S3 buckets management tool.
If you want to use interactive mode, run s3hub without any arguments.`,
}
cmd.CompletionOptions.DisableDefaultCmd = true
cmd.SilenceUsage = true
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.47.7
github.com/charmbracelet/bubbles v0.17.1
github.com/charmbracelet/bubbletea v0.25.0
github.com/fatih/color v1.16.0
github.com/google/go-cmp v0.6.0
github.com/google/wire v0.5.0
github.com/muesli/reflow v0.3.0
Expand Down Expand Up @@ -38,7 +39,8 @@ require (
github.com/google/subcommands v1.0.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
Expand All @@ -48,7 +50,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.6.0 // indirect
Expand Down
14 changes: 10 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ github.com/charmbracelet/lipgloss v0.9.1/go.mod h1:1mPmG4cxScwUQALAAnacHaigiiHB9
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY=
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand All @@ -58,8 +60,11 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
Expand Down Expand Up @@ -90,10 +95,11 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit 4a8c34b

Please sign in to comment.