A Tutorial on Building a Fortune Clone CLI Application
A Tutorial on Building a Fortune Clone CLI Application
In this tutorial, we will be building a fortune clone with Go as a part of the pipe trilogy that includes gololcat and gocowsay. Fortune is a simple program that displays a pseudorandom message from a database of quotations. It acts as a random quote generator and has a long history dating back to Unix Version 7. Many Linux distributions come preinstalled with it, and on OSX, it can be installed using brew install fortune.
To start building our fortune clone, here is a breakdown of what our program will do:
Retrieve the path of the fortunes folder
Index the files in the fortunes folder, excluding binary files and offensive fortunes
Pick a random fortune from the indexed files
Print the random fortune
First, let’s use the os/exec package to run the fortune command with the -f flag, which outputs the path of the fortunes folder. You can use the following code snippet:
Next, we will extract the first line of the output, which contains the path of the fortunes folder. To achieve this, we will use the bufio package to scan the output line by line and extract the first line. Here’s the updated code snippet:
Now that we have the path of the fortunes folder, we need to index the files in that folder. We will use the path/filepath package’s Walk method to iterate through the file tree, excluding binary files and offensive fortunes. The indexed file paths will be stored in a slice called files. Here’s the updated code snippet:
Now that we have indexed the files, we can pick a random fortune from the files slice. We will use the math/rand package’s random number generator functionality to achieve this. Here’s the updated code snippet:
err = filepath.Walk(root, func(path string, f os.FileInfo, err error)error { if err != nil { log.Fatal(err) } if strings.Contains(path, "/off/") { returnnil } if filepath.Ext(path) == ".dat" { returnnil } if f.IsDir() { returnnil } files = append(files, path) returnnil }) if err != nil { panic(err) }
rand.Seed(time.Now().UnixNano()) i := randomInt(1, len(files)) randomFile := files[i]
fmt.Println(randomFile) }
Finally, we need to read the selected fortune file, scan the quotes in the file, and print a random quote. Each quote in the file is separated by a “%” character on a line of its own. Here’s the updated code snippet:
To finalize our program, we can remove the separate visit function and move it as an inline function argument of filepath.Walk. We will also move the files variable as a local variable inside the main function. Here’s the final version of our fortune clone:
With this, our fortune clone is complete. Please note that this is a basic implementation and doesn’t include all the features of the original fortune command. However, it serves as a starting point for further enhancements.
Tags: Go, CLI, Tutorial, Fortune Clone, Random Quote Generator