| 53 | } |
| 54 | |
| 55 | func (s *searchCmd) doSearch(pattern string) error { |
| 56 | // Initialize celer configuration. |
| 57 | if err := s.celer.Init(); err != nil { |
| 58 | return color.PrintError(err, "Failed to initialize celer.") |
| 59 | } |
| 60 | |
| 61 | // Perform search. |
| 62 | libraries, err := s.search(pattern) |
| 63 | if err != nil { |
| 64 | return color.PrintError(err, "Failed to search available ports.") |
| 65 | } |
| 66 | |
| 67 | // Display results. |
| 68 | title := fmt.Sprintf("search results that match pattern '%s':", pattern) |
| 69 | color.Println(color.Title, title) |
| 70 | color.Println(color.Line, strings.Repeat("-", len(title))) |
| 71 | if len(libraries) > 0 { |
| 72 | for _, lib := range libraries { |
| 73 | color.Println(color.Hint, lib) |
| 74 | } |
| 75 | color.Println(color.Line, strings.Repeat("-", len(title))) |
| 76 | color.Printf(color.Summary, "total: %d port(s)\n", len(libraries)) |
| 77 | } else { |
| 78 | color.Println(color.Error, "no matched port found.") |
| 79 | } |
| 80 | |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | func (s *searchCmd) search(pattern string) ([]string, error) { |
| 85 | var results []string |