Browse Source

Initial commit

master
Joop Kiefte 9 months ago
commit
eb7108d91d
2 changed files with 43 additions and 0 deletions
  1. +3
    -0
      README.md
  2. +40
    -0
      main.go

+ 3
- 0
README.md View File

@ -0,0 +1,3 @@
# Bahá'í message of the day tool
Early stage. The database needed can be found at prayers.kiefte.eu

+ 40
- 0
main.go View File

@ -0,0 +1,40 @@
package main
import (
"database/sql"
"fmt"
"time"
"math/rand"
"os"
_ "modernc.org/sqlite"
)
func main() {
rand.Seed(time.Now().UnixNano())
db, err := sql.Open("sqlite", "prayers.db")
if err != nil {
fmt.Println("put prayers.db alongside the binary: "+ err.Error())
os.Exit(1)
}
langcode := "en"
if len(os.Args) > 1 {
langcode = os.Args[1]
}
rows, err := db.Query("select text, author from prayers where language = ?", langcode)
if err != nil {
fmt.Println("database query failed: " +err.Error())
os.Exit(2)
}
var prayers []string
for rows.Next() {
var prayer, author string
rows.Scan(&prayer, &author)
prayers = append(prayers, prayer+"\n\n-- "+author)
}
if len(prayers) < 1 {
fmt.Println("query returns no prayers")
os.Exit(3)
}
fmt.Println(prayers[rand.Intn(len(prayers))])
}

Loading…
Cancel
Save