-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: DynamoMQ CLI rewritten using spf13/cobra
- Loading branch information
Showing
7 changed files
with
170 additions
and
192 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
*.dll | ||
*.so | ||
*.dylib | ||
dynamomq | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
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 |
---|---|---|
@@ -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() | ||
} |
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.