83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// config holds the parsed command-line options.
|
|
type config struct {
|
|
cmd string
|
|
host string
|
|
interval time.Duration
|
|
timeout time.Duration
|
|
|
|
// aircraft filters
|
|
withPos bool
|
|
emergency bool
|
|
minAlt int
|
|
maxAlt int
|
|
withinNM float64
|
|
squawk string
|
|
callsign string
|
|
}
|
|
|
|
// parseFlags parses global flags, the command, and then command-specific flags.
|
|
// Aircraft filters are only accepted for the aircraft command.
|
|
func parseFlags() config {
|
|
var cfg config
|
|
fs := flag.NewFlagSet("wingbits", flag.ExitOnError)
|
|
fs.Usage = usage(fs)
|
|
fs.StringVar(&cfg.host, "host", "", "station host or IP (required)")
|
|
fs.DurationVar(&cfg.interval, "interval", 0, "if >0, stream on this interval")
|
|
fs.DurationVar(&cfg.timeout, "timeout", 10*time.Second, "per-request timeout")
|
|
_ = fs.Parse(os.Args[1:])
|
|
|
|
cfg.cmd = fs.Arg(0)
|
|
requireHostAndCmd(fs, cfg)
|
|
parseAircraftFlags(&cfg, fs.Args()[1:])
|
|
return cfg
|
|
}
|
|
|
|
func requireHostAndCmd(fs *flag.FlagSet, cfg config) {
|
|
if cfg.host == "" || cfg.cmd == "" {
|
|
fs.Usage()
|
|
os.Exit(2)
|
|
}
|
|
}
|
|
|
|
// parseAircraftFlags consumes filter flags that follow the command word.
|
|
func parseAircraftFlags(cfg *config, args []string) {
|
|
fs := flag.NewFlagSet(cfg.cmd, flag.ExitOnError)
|
|
fs.BoolVar(&cfg.withPos, "with-pos", false, "[aircraft] only aircraft with a position")
|
|
fs.BoolVar(&cfg.emergency, "emergency", false, "[aircraft] only aircraft squawking an emergency")
|
|
fs.IntVar(&cfg.minAlt, "min-alt", 0, "[aircraft] minimum barometric altitude (ft)")
|
|
fs.IntVar(&cfg.maxAlt, "max-alt", 0, "[aircraft] maximum barometric altitude (ft)")
|
|
fs.Float64Var(&cfg.withinNM, "within-nm", 0, "[aircraft] within this range of the receiver (nm)")
|
|
fs.StringVar(&cfg.squawk, "squawk", "", "[aircraft] match this Mode A squawk code")
|
|
fs.StringVar(&cfg.callsign, "callsign", "", "[aircraft] match this callsign")
|
|
_ = fs.Parse(args)
|
|
}
|
|
|
|
func usage(fs *flag.FlagSet) func() {
|
|
return func() {
|
|
out := fs.Output()
|
|
fmt.Fprintf(out, "wingbits — query a Wingbits station\n\n")
|
|
fmt.Fprintf(out, "Usage:\n wingbits -host HOST [global flags] COMMAND [command flags]\n\n")
|
|
fmt.Fprintf(out, "Commands:\n")
|
|
for _, c := range commandDescs {
|
|
fmt.Fprintf(out, " %-12s %s\n", c.name, c.desc)
|
|
}
|
|
fmt.Fprintf(out, "\nGlobal flags:\n")
|
|
fs.PrintDefaults()
|
|
fmt.Fprintf(out, "\naircraft flags: -with-pos -emergency -min-alt -max-alt -within-nm -squawk -callsign\n")
|
|
fmt.Fprintf(out, "\nExamples:\n")
|
|
fmt.Fprintf(out, " wingbits -host 192.168.0.127 check\n")
|
|
fmt.Fprintf(out, " wingbits -host 192.168.0.127 aircraft -with-pos -min-alt 30000\n")
|
|
fmt.Fprintf(out, " wingbits -host 192.168.0.127 -interval 2s stats\n")
|
|
fmt.Fprintf(out, " wingbits -host 192.168.0.127 metrics | jq .NATS\n")
|
|
}
|
|
}
|