Run performs the search logic.
(searchTerm string)
| 10 | |
| 11 | // Run performs the search logic. |
| 12 | func Run(searchTerm string) { |
| 13 | // Retrieve the list of feeds to search through. |
| 14 | feeds, err := RetrieveFeeds() |
| 15 | if err != nil { |
| 16 | log.Fatal(err) |
| 17 | } |
| 18 | |
| 19 | // Create an unbuffered channel to receive match results to display. |
| 20 | results := make(chan *Result) |
| 21 | |
| 22 | // Setup a wait group so we can process all the feeds. |
| 23 | var waitGroup sync.WaitGroup |
| 24 | |
| 25 | // Set the number of goroutines we need to wait for while |
| 26 | // they process the individual feeds. |
| 27 | waitGroup.Add(len(feeds)) |
| 28 | |
| 29 | // Launch a goroutine for each feed to find the results. |
| 30 | for _, feed := range feeds { |
| 31 | // Retrieve a matcher for the search. |
| 32 | matcher, exists := matchers[feed.Type] |
| 33 | if !exists { |
| 34 | matcher = matchers["default"] |
| 35 | } |
| 36 | |
| 37 | // Launch the goroutine to perform the search. |
| 38 | go func(matcher Matcher, feed *Feed) { |
| 39 | Match(matcher, feed, searchTerm, results) |
| 40 | waitGroup.Done() |
| 41 | }(matcher, feed) |
| 42 | } |
| 43 | |
| 44 | // Launch a goroutine to monitor when all the work is done. |
| 45 | go func() { |
| 46 | // Wait for everything to be processed. |
| 47 | waitGroup.Wait() |
| 48 | |
| 49 | // Close the channel to signal to the Display |
| 50 | // function that we can exit the program. |
| 51 | close(results) |
| 52 | }() |
| 53 | |
| 54 | // Start displaying results as they are available and |
| 55 | // return after the final result is displayed. |
| 56 | Display(results) |
| 57 | } |
| 58 | |
| 59 | // Register is called to register a matcher for use by the program. |
| 60 | func Register(feedType string, matcher Matcher) { |
nothing calls this directly
no test coverage detected