Skip to content

Commit

Permalink
Support and correctly colorize labels in quoted strings with dots and…
Browse files Browse the repository at this point in the history
… slashes (#99)
  • Loading branch information
AvitalTamir authored Oct 6, 2024
1 parent 9018d8a commit 0753a68
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
13 changes: 6 additions & 7 deletions cmd/cyphernetes/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var (
bracketsRegex = regexp.MustCompile(`[\(\)\[\]\{\}\<\>]`)
variableRegex = regexp.MustCompile(`"(.*?)"`)
identifierRegex = regexp.MustCompile(`0m(\w+):(\w+)`)
propertiesRegex = regexp.MustCompile(`\{([^{}]*(\{[^{}]*\}[^{}]*)*)\}`)
propertiesRegex = regexp.MustCompile(`\{((?:[^{}]|\{[^{}]*\})*)\}`)
returnRegex = regexp.MustCompile(`(?i)(return)(\s+.*)`)
returnJsonPathRegex = regexp.MustCompile(`(\.|\*)`)
)
Expand Down Expand Up @@ -176,12 +176,11 @@ func colorizeProperties(obj string) string {
stripped = strings.TrimSuffix(stripped, "}")

// Colorize properties
colored := regexp.MustCompile(`("?\w+"?)(\s*:\s*)("[^"]*"|[^,{}]+|(\{[^{}]*\}))`).ReplaceAllStringFunc(stripped, func(prop string) string {
parts := regexp.MustCompile(`("?\w+"?)(\s*:\s*)(.+)`).FindStringSubmatch(prop)
if len(parts) == 4 {
colored := regexp.MustCompile(`((?:"(?:[^"\\]|\\.)*"|[^:,{}]+)\s*:\s*)("[^"]*"|[^,{}]+|(\{[^{}]*\}))`).ReplaceAllStringFunc(stripped, func(prop string) string {
parts := regexp.MustCompile(`((?:"(?:[^"\\]|\\.)*"|[^:,{}]+)\s*:\s*)(.+)`).FindStringSubmatch(prop)
if len(parts) == 3 {
key := parts[1]
spacing := parts[2]
value := parts[3]
value := parts[2]

// Check if value is a nested object
if strings.HasPrefix(value, "{") && strings.HasSuffix(value, "}") {
Expand All @@ -190,7 +189,7 @@ func colorizeProperties(obj string) string {
value = "\033[36m" + value + "\033[0m" // Cyan for non-object values
}

return fmt.Sprintf("\033[33m%s\033[0m%s%s", key, spacing, value)
return fmt.Sprintf("\033[33m%s\033[0m%s", key, value)
}
return prop
})
Expand Down
1 change: 1 addition & 0 deletions pkg/parser/k8s_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (q *QueryExecutor) getK8sResources(kind string, fieldSelector string, label
}

func (q *QueryExecutor) fetchResources(kind string, fieldSelector string, labelSelector string) (unstructured.UnstructuredList, error) {
labelSelector = strings.ReplaceAll(labelSelector, "\"", "")
// Use discovery client to find the GVR for the given kind
gvr, err := FindGVR(q.Clientset, kind)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/parser/k8s_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ func getNodeResources(n *NodePattern, q *QueryExecutor, extraFilters []*KeyValue

if n.ResourceProperties.Properties != nil {
for _, prop := range n.ResourceProperties.Properties.PropertyList {
if prop.Key == "name" || prop.Key == "metadata.name" {
if prop.Key == "name" || prop.Key == "metadata.name" || prop.Key == `"name"` || prop.Key == `"metadata.name"` {
fieldSelector += fmt.Sprintf("metadata.name=%s,", prop.Value)
hasNameSelector = true
} else {
Expand Down
6 changes: 5 additions & 1 deletion pkg/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// TestParseQueryWithReturn tests the parsing of a query with a MATCH and RETURN clause.
func TestParseQueryWithReturn(t *testing.T) {
// Define the query to parse.
query := `MATCH (d:deploy { service: "foo", app: "bar"}), (s:Service {service: "foo", app: "bar"}) RETURN s.spec.ports, d.metadata.name`
query := `MATCH (d:deploy { service: "foo", app: "bar"}), (s:Service {service: "foo", app: "bar", "test.io/test": "foo"}) RETURN s.spec.ports, d.metadata.name`

// Define the expected AST structure after parsing.
expected := &Expression{
Expand Down Expand Up @@ -50,6 +50,10 @@ func TestParseQueryWithReturn(t *testing.T) {
Key: "app",
Value: "bar",
},
{
Key: `"test.io/test"`,
Value: "foo",
},
},
},
},
Expand Down

0 comments on commit 0753a68

Please sign in to comment.