Skip to content

Commit

Permalink
Merge pull request #10 from someengineering/lloesche/curl
Browse files Browse the repository at this point in the history
Add equivalent curl as debug
  • Loading branch information
lloesche authored Jul 25, 2024
2 parents d9eaa5a + 379103e commit 8f6837d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
9 changes: 8 additions & 1 deletion search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"fmt"
"io"
"net/http"

"github.com/sirupsen/logrus"
"github.com/someengineering/fixctl/utils"
)

type SearchRequest struct {
Expand Down Expand Up @@ -47,6 +50,10 @@ func SearchGraph(apiEndpoint, fixJWT, workspaceID, searchStr string, withEdges b
Secure: true,
})

escapedRequestBody := utils.EscapeSingleQuotes(string(requestBody))
curlCommand := fmt.Sprintf("curl -X POST -H 'Content-Type: application/json' -H 'Accept: application/ndjson' -H 'Cookie: session_token=%s' -d '%s' %s", fixJWT, escapedRequestBody, url)
logrus.Debugln("Equivalent curl command:", curlCommand)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
Expand Down Expand Up @@ -76,7 +83,7 @@ func SearchGraph(apiEndpoint, fixJWT, workspaceID, searchStr string, withEdges b

var result interface{}
if err := decoder.Decode(&result); err != nil {
errs <- fmt.Errorf("error unmarshaling JSON: %w", err)
errs <- fmt.Errorf("error unmarshalling JSON: %w", err)
return
}
results <- result
Expand Down
4 changes: 4 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,7 @@ func SanitizeOutputFormat(format string) (string, error) {
return "", fmt.Errorf("unsupported output format")
}
}

func EscapeSingleQuotes(s string) string {
return strings.ReplaceAll(s, "'", "'\\''")
}
22 changes: 22 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,25 @@ func TestSanitizeCSVHeaders(t *testing.T) {
}
}
}

func TestEscapeSingleQuotes(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"", ""},
{"no single quotes", "no single quotes"},
{"single ' quote", "single '\\'' quote"},
{"multiple ' single ' quotes", "multiple '\\'' single '\\'' quotes"},
{"escaped \\ backslash ' and single quote", "escaped \\ backslash '\\'' and single quote"},
{"'leading", "'\\''leading"},
{"trailing'", "trailing'\\''"},
}

for _, test := range tests {
result := EscapeSingleQuotes(test.input)
if result != test.expected {
t.Errorf("EscapeSingleQuotes(%q) = %q; want %q", test.input, result, test.expected)
}
}
}

0 comments on commit 8f6837d

Please sign in to comment.