diff --git a/dialog/params.go b/dialog/params.go
index bd0f77d..c046d96 100644
--- a/dialog/params.go
+++ b/dialog/params.go
@@ -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 {
diff --git a/dialog/params_test.go b/dialog/params_test.go
index b90440c..129ec1b 100644
--- a/dialog/params_test.go
+++ b/dialog/params_test.go
@@ -195,3 +195,48 @@ func TestSearchForParams_MultipleParamsSameKeyDifferentValues_InvalidFormat_Mult
t.Fatal(diff)
}
}
+
+func TestInsertParams(t *testing.T) {
+ command := " 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 \"/\" -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 //_delete_by_query/"
+
+ 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)
+ }
+}