-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,019 additions
and
37 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
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,39 @@ | ||
//go:build wireinject | ||
// +build wireinject | ||
|
||
// Package di Inject dependence by wire command. | ||
package di | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/wire" | ||
"github.com/nao1215/rainbow/app/domain/model" | ||
"github.com/nao1215/rainbow/app/external" | ||
"github.com/nao1215/rainbow/app/interactor" | ||
"github.com/nao1215/rainbow/app/usecase" | ||
) | ||
|
||
// S3App is the application service for S3. | ||
type S3App struct { | ||
// S3BucketCreator is the usecase for creating a new S3 bucket. | ||
s3bucketCreator usecase.S3BucketCreator | ||
} | ||
|
||
// NewS3App creates a new S3App. | ||
func NewS3App(ctx context.Context, profile model.AWSProfile, region model.Region) (*S3App, error) { | ||
wire.Build( | ||
model.NewAWSConfig, | ||
external.NewS3Client, | ||
external.S3BucketCreatorSet, | ||
interactor.S3bucketCreatorSet, | ||
newS3App, | ||
) | ||
return nil, nil | ||
} | ||
|
||
func newS3App(s3bucketCreator usecase.S3BucketCreator) *S3App { | ||
return &S3App{ | ||
s3bucketCreator: s3bucketCreator, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,62 @@ | ||
package model | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
) | ||
|
||
// AWSProfile is the name of the AWS profile. | ||
type AWSProfile string | ||
|
||
// NewAWSProfile returns a new AWSProfile. | ||
// If p is empty, read $AWS_PROFILE and return it. | ||
func NewAWSProfile(p string) AWSProfile { | ||
if p == "" { | ||
profile := os.Getenv("AWS_PROFILE") | ||
if profile == "" { | ||
return AWSProfile("default") | ||
} | ||
return AWSProfile(profile) | ||
} | ||
return AWSProfile(p) | ||
} | ||
|
||
// String returns the string representation of the AWSProfile. | ||
func (p AWSProfile) String() string { | ||
return string(p) | ||
} | ||
|
||
// AWSConfig is the AWS config. | ||
type AWSConfig struct { | ||
*aws.Config | ||
} | ||
|
||
// NewAWSConfig creates a new AWS config. | ||
func NewAWSConfig(ctx context.Context, profile AWSProfile, region Region) (*AWSConfig, error) { | ||
opts := []func(*config.LoadOptions) error{} | ||
if profile.String() != "" { | ||
opts = append(opts, config.WithSharedConfigProfile(profile.String())) | ||
} | ||
if region.String() != "" { | ||
opts = append(opts, config.WithRegion(string(region))) | ||
} | ||
|
||
cfg, err := config.LoadDefaultConfig(ctx, opts...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &AWSConfig{ | ||
Config: &cfg, | ||
}, nil | ||
} | ||
|
||
// Region returns the AWS region. | ||
func (c *AWSConfig) Region() Region { | ||
if Region(c.Config.Region) == "" { | ||
return RegionUSEast1 | ||
} | ||
return Region(c.Config.Region) | ||
} |
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,76 @@ | ||
package model | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNewAWSProfile(t *testing.T) { //nolint | ||
type args struct { | ||
p string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want AWSProfile | ||
}{ | ||
{ | ||
name: "success", | ||
args: args{ | ||
p: "test", | ||
}, | ||
want: AWSProfile("test"), | ||
}, | ||
{ | ||
name: "success. p is empty", | ||
args: args{ | ||
p: "", | ||
}, | ||
want: AWSProfile("from env"), | ||
}, | ||
{ | ||
name: "success. p is empty and $AWS_PROFILE is empty", | ||
args: args{ | ||
p: "", | ||
}, | ||
want: AWSProfile("default"), | ||
}, | ||
} | ||
for _, tt := range tests { //nolint | ||
if tt.name == "success. p is empty" { | ||
t.Setenv("AWS_PROFILE", "from env") | ||
} else if tt.name == "success. p is empty and $AWS_PROFILE is empty" { | ||
t.Setenv("AWS_PROFILE", "") | ||
} | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
if got := NewAWSProfile(tt.args.p); got != tt.want { | ||
t.Errorf("NewAWSProfile() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAWSProfileString(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
p AWSProfile | ||
want string | ||
}{ | ||
{ | ||
name: "success", | ||
p: AWSProfile("test"), | ||
want: "test", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
if got := tt.p.String(); got != tt.want { | ||
t.Errorf("AWSProfile.String() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,12 @@ | ||
package model | ||
|
||
import "errors" | ||
|
||
var ( | ||
// ErrInvalidRegion is an error that occurs when the region is invalid. | ||
ErrInvalidRegion = errors.New("invalid region") | ||
// ErrEmptyRegion is an error that occurs when the region is empty. | ||
ErrEmptyRegion = errors.New("region is empty") | ||
// ErrInvalidBucketName is an error that occurs when the bucket name is invalid. | ||
ErrInvalidBucketName = errors.New("bucket name is invalid") | ||
) |
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,10 @@ | ||
package model | ||
|
||
// Validator is an interface that represents a validator. | ||
type Validator interface { | ||
// Validate validates the value. | ||
Validate() error | ||
} | ||
|
||
// ValidationFunc is a type that represents a validation function. | ||
type ValidationFunc func() error |
Oops, something went wrong.