gostations/stations.go

89 lines
1.9 KiB
Go
Raw Normal View History

2021-03-16 12:32:59 +00:00
package main
2021-03-16 13:09:27 +00:00
import (
"context"
2021-03-16 13:09:27 +00:00
"flag"
"fmt"
"os"
"github.com/gmgauthier/gostations/internal/config"
playerpkg "github.com/gmgauthier/gostations/internal/player"
"github.com/gmgauthier/gostations/internal/radio"
2021-03-16 13:09:27 +00:00
)
2021-03-16 12:32:59 +00:00
2021-03-18 22:24:46 +00:00
var version string
2024-07-09 20:29:11 +00:00
func showVersion() {
fmt.Println(version)
}
2024-07-09 20:29:11 +00:00
func precheck() {
p := config.MustGet("player.command") // or fall back inside
if p == "" {
p = "mpv"
}
if !playerpkg.IsInstalled(p) {
fmt.Printf("%s is either not installed, or not on your $PATH. Cannot continue.\n", p)
os.Exit(1)
}
}
2024-07-09 20:29:11 +00:00
func main() {
2021-03-16 13:09:27 +00:00
argCount := len(os.Args[1:])
var (
2024-07-09 20:29:11 +00:00
name string
2021-03-16 13:09:27 +00:00
country string
2024-07-09 20:29:11 +00:00
state string
tags string
notok bool
2024-07-09 20:29:11 +00:00
version bool
2021-03-16 13:09:27 +00:00
)
flag.Usage = func() {
fmt.Printf("Usage: \n")
fmt.Printf(" gostations ")
fmt.Printf(" [-n \"name\"] [-c \"home country\"] [-s \"home state\"] [-t \"ordered,tag,list\"] [-x] [-v]\n")
flag.PrintDefaults()
fmt.Printf(" -h (or none)\n")
fmt.Printf("\tThis help message\n")
}
2021-03-16 13:09:27 +00:00
flag.StringVar(&name, "n", "", "Station name (or identifier).")
flag.StringVar(&country, "c", "", "Home country.")
flag.StringVar(&state, "s", "", "Home state (if in the United States).")
flag.StringVar(&tags, "t", "", "Tag (or comma-separated tag list)")
2024-07-09 20:29:11 +00:00
flag.BoolVar(&notok, "x", false, "If toggled, will show stations that are down")
flag.BoolVar(&version, "v", false, "Show version.")
2021-03-16 13:09:27 +00:00
flag.Parse()
if argCount == 0 {
flag.Usage()
os.Exit(0)
2021-03-16 13:09:27 +00:00
}
if version {
showVersion()
os.Exit(0)
}
precheck()
2021-03-16 12:32:59 +00:00
if err := config.Init(); err != nil {
fmt.Printf("config init: %v\n", err)
os.Exit(1)
}
stations, err := radio.Search(context.Background(), name, country, state, tags, notok)
if err != nil {
// graceful: empty list + menu (old code dropped errs; we at least log)
fmt.Printf("warning: station search: %v\n", err)
stations = nil
}
2021-03-16 23:06:20 +00:00
menu := RadioMenu(stations)
err = menu.Run()
2021-03-16 23:06:20 +00:00
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
2024-07-09 20:29:11 +00:00
}