~kota/metweather

ee8107674a68f11ebed69fa4fb9455474d422642 — Dakota Walsh 3 months ago f465ab8
add daily forcast printing
3 files changed, 40 insertions(+), 9 deletions(-)

M cmd/forecast.go
M go.mod
M go.sum
M cmd/forecast.go => cmd/forecast.go +37 -4
@@ 20,7 20,11 @@ var forecastCmd = &cobra.Command{
func init() {
	rootCmd.AddCommand(forecastCmd)
	forecastCmd.PersistentFlags().StringP("location", "l", "", "location used for the weather forecast")
	forecastCmd.PersistentFlags().BoolP("week", "w", false, "print the forecast for the next week")
	forecastCmd.PersistentFlags().BoolP("day", "d", false, "print the forecast for the next 48 hours")
	viper.BindPFlag("location", forecastCmd.PersistentFlags().Lookup("location"))
	viper.BindPFlag("now", forecastCmd.PersistentFlags().Lookup("now"))
	viper.BindPFlag("day", forecastCmd.PersistentFlags().Lookup("day"))
}

// forecast fetches and prints a forecast based on options provided


@@ 31,10 35,39 @@ func forecast(cmd *cobra.Command, args []string) {
	if location == "" {
		log.Fatal("location is required either using the flag or config")
	}
	err := forecastWeekly(client, ctx, location)
	if viper.GetBool("day") {
		err := forecastDaily(client, ctx, location)
		if err != nil {
			log.Fatal(err)
		}
	} else {
		err := forecastWeekly(client, ctx, location)
		if err != nil {
			log.Fatal(err)
		}
	}
}

// forecastDaily fetches and prints a forecast for the next 48 hours with an
// hour on each line
func forecastDaily(client *metservice.Client, ctx context.Context, location string) error {
	f, _, err := client.GetObservationForecastHours(ctx, location)
	if err != nil {
		log.Fatal(err)
		return fmt.Errorf("getting forecast: %v", err)
	}
	for _, hour := range f.Forecasts {
		fmt.Fprintf(out, "%d-%d-%d %.2d:%.2d ",
			hour.Date.Local().Year(),
			hour.Date.Local().Month(),
			hour.Date.Local().Day(),
			hour.Date.Local().Hour(),
			hour.Date.Local().Minute())
		fmt.Fprintf(out, "%.2d°C ", *hour.Temp)
		fmt.Fprintf(out, "%.2dkm/h ", *hour.WindSpeed)
		fmt.Fprintf(out, "%-2s - ", *hour.WindDirection)
		fmt.Fprintf(out, "%d%%\n", *hour.Humidity)
	}
	return nil
}

// forecastWeekly fetches and prints a forecast on one line for each day using


@@ 49,9 82,9 @@ func forecastWeekly(client *metservice.Client, ctx context.Context, location str
			day.Date.Local().Year(),
			day.Date.Local().Month(),
			day.Date.Local().Day())
		fmt.Fprintf(out, "%s ", *day.ForecastWord)
		fmt.Fprintf(out, "%d-", *day.Min)
		fmt.Fprintf(out, "%d°C\n", *day.Max)
		fmt.Fprintf(out, "%d°C ", *day.Max)
		fmt.Fprintf(out, "%s\n", *day.ForecastWord)
	}
	return nil
}

M go.mod => go.mod +1 -1
@@ 3,7 3,7 @@ module git.sr.ht/~kota/metweather
go 1.16

require (
	git.sr.ht/~kota/metservice-go v1.1.0
	git.sr.ht/~kota/metservice-go v1.1.2
	github.com/adrg/xdg v0.3.3
	github.com/mitchellh/go-homedir v1.1.0
	github.com/spf13/cast v1.4.0 // indirect

M go.sum => go.sum +2 -4
@@ 37,10 37,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
git.sr.ht/~kota/metservice-go v1.0.1 h1:XjQjIgBywJ3la2RQnpeU82WGU9oEnsJ+8Sch0vb8yGE=
git.sr.ht/~kota/metservice-go v1.0.1/go.mod h1:ruGNU5HLsIa4TPYAiB2FHPUTk3Ya4/uRT06/OLCZB4c=
git.sr.ht/~kota/metservice-go v1.1.0 h1:unTahlDCgk5fbq9e2eWvzYS9Dk9qyYDE8THgdySjIDM=
git.sr.ht/~kota/metservice-go v1.1.0/go.mod h1:ruGNU5HLsIa4TPYAiB2FHPUTk3Ya4/uRT06/OLCZB4c=
git.sr.ht/~kota/metservice-go v1.1.2 h1:C04kWJn35xZDSK+216wPkgh7hiFuG6cdatsIWQGp4AA=
git.sr.ht/~kota/metservice-go v1.1.2/go.mod h1:ruGNU5HLsIa4TPYAiB2FHPUTk3Ya4/uRT06/OLCZB4c=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/adrg/xdg v0.3.3 h1:s/tV7MdqQnzB1nKY8aqHvAMD+uCiuEDzVB5HLRY849U=