Add -i flag

This commit is contained in:
Elara 2023-08-25 20:22:16 -07:00
parent b23007a81a
commit e3d9ff941f
2 changed files with 33 additions and 5 deletions

View File

@ -48,3 +48,11 @@ tafparser -s m/s -d m
```
This tells `tafparser` to convert all speed units to meters per second and distance units to meters.
`tafparser` can also fetch TAF reports for you using the [aviationweather.gov](https://aviationweather.gov) site. Use the `-i <identifier>` flag to tell it to do that, like so:
```bash
tafparser -i EGLL
```
That should automatically fetch the report for London Heathrow and parse it.

View File

@ -2,7 +2,10 @@ package main
import (
"encoding/json"
"io"
"net/http"
"os"
"strings"
"github.com/alecthomas/repr"
"github.com/spf13/pflag"
@ -21,6 +24,7 @@ func main() {
printGo := pflag.BoolP("print-go", "G", false, "Print Go code instead of JSON")
convertDist := pflag.StringP("convert-distance", "d", "", "Convert all the distances to the given unit. (valid units: mi, m, km)")
convertSpd := pflag.StringP("convert-speed", "s", "", "Convert all the speeds to the given unit. (valid units: m/s, kph, kts, mph)")
identifier := pflag.StringP("identifier", "i", "", "Automatically fetch the TAF report for the specified ICAO identifier")
pflag.Parse()
var opts taf.Options
@ -41,18 +45,34 @@ func main() {
opts.SpeedUnit = s
}
var fl *os.File
var r io.Reader
var err error
if pflag.NArg() > 0 {
fl, err = os.Open(pflag.Arg(0))
fl, err := os.Open(pflag.Arg(0))
if err != nil {
log.Fatal("Error opening file").Err(err).Send()
}
defer fl.Close()
r = fl
} else if *identifier != "" {
// Identifiers must be uppercase
*identifier = strings.ToUpper(*identifier)
// Get the TAF report from aviationweather.gov's beta endpoint
res, err := http.Get("https://beta.aviationweather.gov/cgi-bin/data/taf.php?ids=" + *identifier)
if err != nil {
log.Fatal("Error getting TAF report").Err(err).Send()
}
// The backend doesn't return an error for non-existent reports, so check the content length instead
if res.ContentLength == 0 {
log.Fatal("Could not find a TAF report for the specified airport").Str("id", *identifier).Send()
}
defer res.Body.Close()
r = res.Body
} else {
fl = os.Stdin
r = os.Stdin
}
fc, err := taf.DecodeWithOptions(fl, opts)
fc, err := taf.DecodeWithOptions(r, opts)
if err != nil {
log.Fatal("Error parsing TAF data").Err(err).Send()
}