Skip to content

Commit

Permalink
Add fuzzy search support (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewhammond authored Feb 9, 2023
1 parent a0c48d6 commit c77f248
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/app/ui/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ func (s *Service) getNodes(c echo.Context) error {
var nodes *chef.NodeList
var err error
if query != "" {
if !strings.Contains(query, ":") {
query = fuzzifySearchStr(query)
}
nodes, err = s.chef.SearchNodes(c.Request().Context(), query)
} else {
nodes, err = s.chef.GetNodes(c.Request().Context())
Expand All @@ -230,6 +233,27 @@ func (s *Service) getNodes(c echo.Context) error {
})
}

// fuzzifySearchStr mimics the fuzzy search functionality
// provided by chef https://github.com/chef/chef/blob/main/lib/chef/search/query.rb#L109
func fuzzifySearchStr(s string) string {
format := []string{
"tags:*%v*",
"roles:*%v*",
"fqdn:*%v*",
"addresses:*%v*",
"policy_name:*%v*",
"policy_group:*%v*",
}
var b strings.Builder
for i, f := range format {
if i > 0 {
b.WriteString(" OR ")
}
b.WriteString(fmt.Sprintf(f, s))
}
return b.String()
}

func (s *Service) getRoles(c echo.Context) error {
roles, err := s.chef.GetRoles(c.Request().Context())
if err != nil {
Expand Down

0 comments on commit c77f248

Please sign in to comment.