Skip to content

Commit

Permalink
fix: 🐛 Updated insert params to match new search format
Browse files Browse the repository at this point in the history
  • Loading branch information
RamiAwar committed Feb 2, 2024
1 parent 988e248 commit be2b04c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
18 changes: 14 additions & 4 deletions dialog/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,31 @@ var (
CurrentCommand string
//FinalCommand is the command after assigning to variables
FinalCommand string

patternRegex = `<([\S]+?)>`
)

func insertParams(command string, params map[string]string) string {
r, _ := regexp.Compile(patternRegex)

matches := r.FindAllStringSubmatch(command, -1)
if len(matches) == 0 {
return command
}

resultCommand := command
for k, v := range params {
resultCommand = strings.Replace(resultCommand, k, v, -1)
for _, p := range matches {
splitted := strings.Split(p[1], "=")
resultCommand = strings.Replace(resultCommand, p[0], params[splitted[0]], -1)
}

return resultCommand
}

// SearchForParams returns variables from a command
func SearchForParams(lines []string) map[string]string {
re := `<([\S]+?)>`
if len(lines) == 1 {
r, _ := regexp.Compile(re)
r, _ := regexp.Compile(patternRegex)

params := r.FindAllStringSubmatch(lines[0], -1)
if len(params) == 0 {
Expand Down
45 changes: 45 additions & 0 deletions dialog/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,48 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_Mult
t.Fatal(diff)
}
}

func TestInsertParams(t *testing.T) {
command := "<a=1> <a> <b> hello"

params := map[string]string{
"a": "test",
"b": "case",
}

got := insertParams(command, params)
want := "test test case hello"
if want != got {
t.Fatalf("wanted '%s', got '%s'", want, got)
}
}

func TestInsertParams_unique_parameters(t *testing.T) {
command := "curl -X POST \"<host=http://localhost:9200>/<index>\" -H 'Content-Type: application/json'"

params := map[string]string{
"host": "localhost:9200",
"index": "test",
}

got := insertParams(command, params)
want := "curl -X POST \"localhost:9200/test\" -H 'Content-Type: application/json'"
if got != want {
t.Fatalf("got %s, want %s", got, want)
}
}

func TestInsertParams_complex(t *testing.T) {
command := "something <host=http://localhost:9200>/<test>/_delete_by_query/<host>"

params := map[string]string{
"host": "localhost:9200",
"test": "case",
}

got := insertParams(command, params)
want := "something localhost:9200/case/_delete_by_query/localhost:9200"
if got != want {
t.Fatalf("got %s, want %s", got, want)
}
}

0 comments on commit be2b04c

Please sign in to comment.