gostations/stations.go
Greg Gauthier b378fec3b2
Some checks failed
gobuild / build (push) Failing after 4s
refactor: reorganize into internal packages, fix critical panics and error handling
Move browser, config, player, and radio logic into internal/{config,radio,player,ui,version}.
Add cached API host resolution, context-aware HTTP client, and nil/length guards to eliminate
RandomIP, nslookup, reverseLookup, and GetStations panics. Replace subExecute with clean
legacyPlayer using Run-only execution. Fix inverted -short test guards, unformatted config
error messages, and "Erorr" typo. Remove obsolete vendor/GOPATH logic from build scripts.
Update callers in stations.go and radiomenu.go to new paths; retain shims for transition.
2026-06-05 21:10:54 +01:00

89 lines
1.9 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/gmgauthier/gostations/internal/config"
playerpkg "github.com/gmgauthier/gostations/internal/player"
"github.com/gmgauthier/gostations/internal/radio"
)
var version string
func showVersion() {
fmt.Println(version)
}
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)
}
}
func main() {
argCount := len(os.Args[1:])
var (
name string
country string
state string
tags string
notok bool
version bool
)
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")
}
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)")
flag.BoolVar(&notok, "x", false, "If toggled, will show stations that are down")
flag.BoolVar(&version, "v", false, "Show version.")
flag.Parse()
if argCount == 0 {
flag.Usage()
os.Exit(0)
}
if version {
showVersion()
os.Exit(0)
}
precheck()
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
}
menu := RadioMenu(stations)
err = menu.Run()
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}