Skip to content

Commit

Permalink
refactor: refactor AWS authentication process and remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatanabe committed Oct 30, 2023
1 parent ff774c8 commit 730a8e5
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 239 deletions.
30 changes: 14 additions & 16 deletions cmd/dynamomq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"os"
"strings"

"github.com/vvatanabe/dynamomq/internal/cli"

"github.com/aws/aws-sdk-go-v2/config"
"github.com/vvatanabe/dynamomq"
"github.com/vvatanabe/dynamomq/internal/cli"
)

func main() {
Expand All @@ -24,24 +24,24 @@ func main() {
fmt.Println("")

executionPath, _ := os.Getwd()
fmt.Printf("current directory is: [%s]\n", executionPath)

region := flag.String("region", dynamomq.AwsRegionDefault, "AWS region")
credentialsProfile := flag.String("profile", dynamomq.AwsProfileDefault, "AWS credentials profile")
tableName := flag.String("table", dynamomq.DefaultTableName, "AWS DynamoDB table name")
endpoint := flag.String("endpoint-url", "", "AWS DynamoDB base endpoint url")

flag.Parse()

fmt.Printf("profile is: [%s]\n", *credentialsProfile)
fmt.Printf("region is: [%s]\n", *region)
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
fmt.Printf("failed to load aws config: %s\n", err)
os.Exit(1)
}

fmt.Printf("current dir is: [%s]\n", executionPath)
fmt.Printf("region is: [%s]\n", cfg.Region)
fmt.Printf("table is: [%s]\n", *tableName)
fmt.Printf("endpoint is: [%s]\n", *endpoint)
fmt.Println("")

client, err := dynamomq.NewFromConfig[any](context.Background(),
dynamomq.WithAWSRegion(*region),
dynamomq.WithAWSCredentialsProfileName(*credentialsProfile),
client, err := dynamomq.NewFromConfig[any](cfg,
dynamomq.WithTableName(*tableName),
dynamomq.WithAWSBaseEndpoint(*endpoint))
if err != nil {
Expand All @@ -51,11 +51,9 @@ func main() {
}

c := cli.CLI{
Region: *region,
CredentialsProfile: *credentialsProfile,
TableName: *tableName,
Client: client,
Message: nil,
TableName: *tableName,
Client: client,
Message: nil,
}

// 1. Create a Scanner using the InputStream available.
Expand Down
61 changes: 3 additions & 58 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,15 @@ const (
)

type CLI struct {
Region string
BaseEndpoint string
CredentialsProfile string
TableName string

Client dynamomq.Client[any]
Message *dynamomq.Message[any]
TableName string
Client dynamomq.Client[any]
Message *dynamomq.Message[any]
}

func (c *CLI) Run(ctx context.Context, command string, params []string) {
switch command {
case "h", "?", "help":
c.help(ctx, params)
case "aws":
c.aws(ctx, params)
case "qstat", "qstats":
c.qstat(ctx, params)
case "dlq":
Expand Down Expand Up @@ -70,7 +64,6 @@ func (c *CLI) Run(ctx context.Context, command string, params []string) {

func (c *CLI) help(_ context.Context, _ []string) {
fmt.Println(`... this is CLI HELP!
> aws --profile --region --table --endpoint-url [Establish connection with AWS; Default profile name: 'default' and region: 'us-east-1']
> qstat | qstats [Retrieves the Queue statistics (no need to be in App mode)]
> dlq [Retrieves the Dead Letter Queue (DLQ) statistics]
> enqueue-test | et [SendMessage test Message records in DynamoDB: A-101, A-202, A-303 and A-404; if already exists, it will overwrite it]
Expand All @@ -89,54 +82,6 @@ func (c *CLI) help(_ context.Context, _ []string) {
> id`)
}

func (c *CLI) aws(ctx context.Context, params []string) {
if len(params) == 0 {
printError("aws --profile --region --table --endpoint-url [Establish connection with AWS; Default profile: 'default' and region: 'us-east-1']")
return
}
profile, region, table, endpoint := parseParams(params)
if region != "" {
c.Region = region
}
if table != "" {
c.TableName = table
}
if profile != "" {
c.CredentialsProfile = profile
}
if endpoint != "" {
c.BaseEndpoint = endpoint
}
client, err := dynamomq.NewFromConfig[any](ctx,
dynamomq.WithAWSRegion(c.Region),
dynamomq.WithAWSCredentialsProfileName(profile),
dynamomq.WithTableName(c.TableName),
dynamomq.WithAWSBaseEndpoint(c.BaseEndpoint))
if err != nil {
fmt.Printf(" ... AWS session could not be established!: %v\n", err)
} else {
c.Client = client
fmt.Println(" ... AWS session is properly established!")
}
}

func parseParams(params []string) (profile, region, table, endpoint string) {
// Map to store parsed values
for i := 0; i < len(params)-1; i++ {
switch params[i] {
case "--profile", "-profile":
profile = params[i+1]
case "--region", "-region":
region = params[i+1]
case "--table", "-table":
table = params[i+1]
case "--endpoint-url", "-endpoint-url":
endpoint = params[i+1]
}
}
return
}

func (c *CLI) ls(ctx context.Context, _ []string) {
if c.Client == nil {
fmt.Println(needAWSMessage)
Expand Down
47 changes: 0 additions & 47 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1 @@
package cli

import "testing"

func TestParseParams(t *testing.T) {
tests := []struct {
input []string
profile, region, table, endpoint string
}{
{
input: []string{"--profile", "myProfile", "--region", "us-west-1", "--table", "myTable", "--endpoint-url", "http://localhost:8000"},
profile: "myProfile",
region: "us-west-1",
table: "myTable",
endpoint: "http://localhost:8000",
},
{
input: []string{"--profile", "myProfile", "--region", "us-west-1"},
profile: "myProfile",
region: "us-west-1",
table: "",
endpoint: "",
},
{
input: []string{"--table", "myTable", "--endpoint-url", "http://localhost:8000"},
profile: "",
region: "",
table: "myTable",
endpoint: "http://localhost:8000",
},
}

for _, tt := range tests {
profile, region, table, endpoint := parseParams(tt.input)
if profile != tt.profile {
t.Errorf("Expected profile %s, got %s", tt.profile, profile)
}
if region != tt.region {
t.Errorf("Expected region %s, got %s", tt.region, region)
}
if table != tt.table {
t.Errorf("Expected table %s, got %s", tt.table, table)
}
if endpoint != tt.endpoint {
t.Errorf("Expected endpoint %s, got %s", tt.endpoint, endpoint)
}
}
}
Loading

0 comments on commit 730a8e5

Please sign in to comment.