Skip to content

Commit

Permalink
Can evaluate commands in interactive mode. Fixed quote and nested quo…
Browse files Browse the repository at this point in the history
…te handling for commands
  • Loading branch information
DerrickGold committed Nov 14, 2021
1 parent 99e8f99 commit 6018b9a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
22 changes: 14 additions & 8 deletions cmd/arp/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bufio"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -187,7 +188,7 @@ func interactivePrompt(showOpts bool, canRetry bool, websocketMode bool) {
"d) Dump all values in data store",
"x) Step through tests until next failure",
"q) Hot reload test file",
"*) Expand typed variable. e.g. @{host}",
"*) Evaluate varaiable or inline command. e.g. @{host}, $(date -u -R)",
}

if showOpts {
Expand Down Expand Up @@ -221,15 +222,14 @@ func interactiveInput(tests []*TestCase, curTest int, subTest bool, result *Test
}
interactivePrompt(true, canRetry, websocketPrompt)

reader := bufio.NewReader(os.Stdin)
for {
input := ""
fmt.Scanln(&input)

input, _ := reader.ReadString('\n')
if input == "" {
return StepInput{}
}

switch strings.ReplaceAll(input, "\n", "") {
switch sanitized := strings.ReplaceAll(input, "\n", ""); sanitized {
case "n":
return StepInput{}
case "e":
Expand All @@ -248,16 +248,22 @@ func interactiveInput(tests []*TestCase, curTest int, subTest bool, result *Test
case "q":
return StepInput{HotReload: true}
default:
expanded, err := tests[curTest].GlobalDataStore.ExpandVariable(input)
expanded, err := tests[curTest].GlobalDataStore.ExpandVariable(sanitized)
if err != nil {
fmt.Printf("\nFailed to expand variable: %v\n", err)
} else {
if _, ok := expanded.(string); !ok {
if s, ok := expanded.(string); !ok {
data, _ := json.MarshalIndent(expanded, "", IndentStr(1))
expanded = string(data)
} else {
executed, err := ExecuteCommand(s)
if err != nil {
fmt.Printf("\nFailed to execute command: %v\n", err)
}
expanded = executed
}

fmt.Printf("%v -> %v\n", input, expanded)
fmt.Printf("%v -> %v\n", sanitized, expanded)
}
}

Expand Down
15 changes: 12 additions & 3 deletions subcommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func executeCommandStr(input string) (string, error) {

// regular arguments can be separated by spaces if not quoted
if !inQuote && char == ' ' && argStartPos != i {
// no +1 on token end to exclude the space delimiter
tokens = append(tokens, realCmd[argStartPos:i])
argStartPos = i + 1
} else if inQuote && char == ' ' {
Expand All @@ -94,11 +95,10 @@ func executeCommandStr(input string) (string, error) {
} else if !inQuote && char == '"' {
//if we aren't in a quoted string and we hit a quote, then we can continue
inQuote = true
// set our start position for the next argument to exclude the starting quote
argStartPos = i + 1
} else if inQuote && char == '"' {
// if we are in a quote and we hit another quote, we'll treat that as the closing one
tokens = append(tokens, realCmd[argStartPos:i])
// +1 to include the end quote for our token
tokens = append(tokens, realCmd[argStartPos:i+1])
inQuote = false
// make our next argument starting position skip this closing quote
argStartPos = i + 1
Expand All @@ -113,6 +113,15 @@ func executeCommandStr(input string) (string, error) {
var args []string
for _, s := range tokens {
newToken := ""
// if the whole token is quoted, then we can remove them and promote the nested quotes
if s[0] == '"' && s[len(s)-1] == '"' {
s = s[1 : len(s)-1]
} else {
// otherwise, leave the string alone.
args = append(args, s)
continue
}

for i := 0; i < len(s); i++ {
if s[i] == '\\' {
escapeEnd := i + 1
Expand Down

0 comments on commit 6018b9a

Please sign in to comment.