Tuesday, August 31, 2021

Searching My Blog

Created a very basic Go script this morning that allows me to search this blog. It looks something like this:

package main

import (
	"fmt"
	"os"
	"regexp"
	"strings"

	"github.com/mmcdole/gofeed"
)

func main() {
	file, _ := os.Open("path to file")
	defer file.Close()

	fp := gofeed.NewParser()
	feed, _ := fp.Parse(file)

	args := os.Args[1:]

	search := strings.Join(args[:], " ")

	for _, item := range feed.Items {
		itemContentStr := &item.Content

		r, _ := regexp.Compile("(?i)" + search)
		result := r.FindAllString(*itemContentStr, -1)
		
		if len(result) > 0 {
			fmt.Println(result, item.Link)
		}
	}
}

It basically loops through a local RSS file and runs some basic regex over the content of each item. It then prints out the results of what it finds with the url. The url is clickable from the command line with command-click.

The script is invocable via search <query>. This is done by adding a search alias to my .zshrc set as a path to a build.

It's a super simple but effective way to find content especially when I build up more posts!