Skip to content

Commit

Permalink
feat: DynamoMQ CLI rewritten using spf13/cobra
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatanabe committed Oct 31, 2023
1 parent 730a8e5 commit 2402785
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 192 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*.dll
*.so
*.dylib
dynamomq

# Test binary, built with `go test -c`
*.test
Expand Down
107 changes: 2 additions & 105 deletions cmd/dynamomq/main.go
Original file line number Diff line number Diff line change
@@ -1,112 +1,9 @@
package main

import (
"bufio"
"context"
"flag"
"fmt"
"os"
"strings"

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

func main() {
defer fmt.Printf("... CLI is ending\n\n\n")

fmt.Println("===========================================================")
fmt.Println(">> Welcome to Priority Queueing CLI Tool!")
fmt.Println("===========================================================")
fmt.Println("for help, enter one of the following: ? or h or help")
fmt.Println("all commands in CLIs need to be typed in lowercase")
fmt.Println("")

executionPath, _ := os.Getwd()
tableName := flag.String("table", dynamomq.DefaultTableName, "AWS DynamoDB table name")
endpoint := flag.String("endpoint-url", "", "AWS DynamoDB base endpoint url")

flag.Parse()

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](cfg,
dynamomq.WithTableName(*tableName),
dynamomq.WithAWSBaseEndpoint(*endpoint))
if err != nil {
fmt.Printf("... AWS session could not be established!: %v\n", err)
} else {
fmt.Println("... AWS session is properly established!")
}

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

// 1. Create a Scanner using the InputStream available.
scanner := bufio.NewScanner(os.Stdin)

for {
// 2. Don't forget to prompt the user
if c.Message != nil {
fmt.Printf("\nID <%s> >> Enter command: ", c.Message.ID)
} else {
fmt.Print("\n>> Enter command: ")
}

// 3. Use the Scanner to read a line of text from the user.
scanned := scanner.Scan()
if !scanned {
break
}

input := scanner.Text()
if input == "" {
continue
}

command, params := parseInput(input)
switch command {
case "":
continue
case "quit", "q":
return
default:
// 4. Now, you can do anything with the input string that you need to.
// Like, output it to the user.
c.Run(context.Background(), command, params)
}
}
}

func parseInput(input string) (command string, params []string) {
input = strings.TrimSpace(input)
arr := strings.Fields(input)

if len(arr) == 0 {
return "", nil
}

command = strings.ToLower(arr[0])

if len(arr) > 1 {
params = make([]string, len(arr)-1)
for i := 1; i < len(arr); i++ {
params[i-1] = strings.TrimSpace(arr[i])
}
}
return command, params
cmd.Execute()
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ go 1.21.0
require (
github.com/aws/aws-sdk-go-v2 v1.21.0
github.com/aws/aws-sdk-go-v2/config v1.18.42
github.com/aws/aws-sdk-go-v2/credentials v1.13.40
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.10.39
github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression v1.4.66
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.21.5
github.com/spf13/cobra v1.7.0
github.com/upsidr/dynamotest v0.1.1
)

require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.13.40 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.41 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.35 // indirect
Expand All @@ -37,6 +38,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
Expand All @@ -46,6 +48,7 @@ require (
github.com/ory/dockertest/v3 v3.10.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqy
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/containerd/continuity v0.4.2 h1:v3y/4Yz5jwnvqPKJJ+7Wf93fyWoCB3F5EclWG023MDM=
github.com/containerd/continuity v0.4.2/go.mod h1:F6PTNCKepoxEaXLQp3wDAjygEnImnZ/7o4JzpodfroQ=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -67,6 +68,8 @@ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaU
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand All @@ -91,8 +94,13 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
1 change: 0 additions & 1 deletion internal/cli/cli_test.go

This file was deleted.

Loading

0 comments on commit 2402785

Please sign in to comment.