summaryrefslogtreecommitdiff
path: root/weather.go
diff options
context:
space:
mode:
authorNick White <git@njw.name>2021-02-05 13:31:33 +0000
committerNick White <git@njw.name>2021-02-05 13:31:33 +0000
commit3413df247aee70fc9ba217dfaa24bb217fff4248 (patch)
tree52d3a2d0b31762b5e2de0062c4c026ffa9254513 /weather.go
parentd7dc8bb54b289500ed4a2a915bff4459c85ccac3 (diff)
downloadweather-3413df247aee70fc9ba217dfaa24bb217fff4248.tar.bz2
weather-3413df247aee70fc9ba217dfaa24bb217fff4248.zip
Fix argument parsing so choosing a source is clear and works properly
Diffstat (limited to 'weather.go')
-rw-r--r--weather.go29
1 files changed, 16 insertions, 13 deletions
diff --git a/weather.go b/weather.go
index 4fd1c36..b0ca2da 100644
--- a/weather.go
+++ b/weather.go
@@ -133,7 +133,7 @@ type Weather struct {
}
var (
- bbc = flag.Bool("b", true, "use bbc as data source")
+ src = flag.String("s", "bbc", "data source provider (valid options: 'bbc', 'metoffice')")
numdays = flag.Int("n", 2, "number of days to show")
verbose = flag.Bool("v", false, "verbose: show all weather details")
)
@@ -203,25 +203,28 @@ func processMet(b []byte) []Weather {
func main() {
var err error
var id string
+ var url string
var resp *http.Response
var weather []Weather
flag.Parse()
+
+ switch *src {
+ case "bbc":
+ id = bbcdefid
+ url = bbcurl
+ case "metoffice":
+ id = metdefid
+ url = meturl
+ default:
+ log.Fatalf("data source %s not supported; use either 'bbc' or 'metoffice'\n", *src)
+ }
+
if flag.NArg() > 0 {
id = flag.Arg(0)
- } else {
- if *bbc {
- id = bbcdefid
- } else {
- id = metdefid
- }
}
- if *bbc {
- resp, err = http.Get(fmt.Sprintf(bbcurl, id))
- } else {
- resp, err = http.Get(fmt.Sprintf(meturl, id))
- }
+ resp, err = http.Get(fmt.Sprintf(url, id))
if err != nil {
log.Fatal(err)
}
@@ -231,7 +234,7 @@ func main() {
}
b, err := ioutil.ReadAll(resp.Body)
- if *bbc {
+ if *src == "bbc" {
weather = processBBC(b)
} else {
weather = processMet(b)