Example #1
0
// Scourge executes the reaction to a story hit.
func scourge(context *cli.Context) {
	// Read the story
	story, err := common.ReadStory(common.StandardInput())
	if err != nil {
		log.Println(err)
		return
	}

	// Check if there is anything to execute
	if len(story.Triggers) == 0 {
		return
	}

	// Loop through the story triggers
	for _, t := range story.Triggers {
		cmd := exec.Command(t.Command, t.Arguments...)
		err := cmd.Start()

		// If there was a problem starting the command and it is set
		// to wait then we fail hard
		if err != nil && t.Wait {
			log.Printf("Failed to execute [%s]", t.ToString())
			return
		}

		// If we are to wait then we need to check its status and
		// fail hard if there was an issue
		if t.Wait {
			if err = cmd.Wait(); err != nil {
				log.Printf("Error executing [%s]", t.ToString())
				return
			}
		}
	}
}
Example #2
0
// Scour a collection of feeds looking for a match to the given story.
func scour(context *cli.Context) {
	// Get the path to the file containing the feeds
	path := context.String("feeds")

	// Read our current story file
	story, err := common.ReadStory(common.StandardInput())
	if err != nil {
		log.Println(err)
		return
	}

	// Open the feed
	f, err := os.Open(path)
	if err != nil {
		log.Println(err)
		return
	}
	defer f.Close()

	// Digest the feeds
	urls := []string{}
	bf := bufio.NewScanner(f)
	for bf.Scan() {
		u := bf.Text()
		comment := strings.Index(u, "//")

		if u != "" && comment != 0 {
			urls = append(urls, u)
		}
	}
	if err := bf.Err(); err != nil {
		log.Println(err)
		return
	}

	// Loop through each feed searching for a matching story
	c := make(chan common.Story)
	e := make(chan error)
	for _, url := range urls {
		go func(u string) {
			s, err := story.Find(u)
			if err != nil {
				e <- err
				return
			}
			c <- s
		}(url)
	}

	// Make sure everything has completed prior before quitting
	for i := 0; i < len(urls); i++ {
		select {
		case incoming := <-c:
			// Ignore JSON marshalling errors and move on to the next
			// potential match
			json, err := incoming.ToJSON()
			if err == nil {
				fmt.Fprintf(os.Stdout, "%s", json)
				return
			}

		case <-e:
			// TODO: Record the error
		}
	}

	// If nothing hit then report a failure
	log.Println("Could not find a story match")
}