2024-09-29 17:20:59 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.sr.ht/~timharek/yr-go/yr"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var nowCmd = &cobra.Command{
|
2024-09-29 18:31:42 +00:00
|
|
|
Use: "now <location>",
|
2024-09-29 17:20:59 +00:00
|
|
|
Aliases: []string{"current", "n"},
|
|
|
|
Short: "Get current forecasted weather",
|
2024-09-29 18:31:42 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
Run: now,
|
2024-09-29 17:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(nowCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
func now(cmd *cobra.Command, args []string) {
|
|
|
|
isJson, err := cmd.Flags().GetBool("json")
|
2024-09-29 17:50:45 +00:00
|
|
|
cobra.CheckErr(err)
|
2024-09-29 17:20:59 +00:00
|
|
|
location := args[0]
|
|
|
|
|
|
|
|
yr, err := yr.New()
|
2024-09-29 17:50:45 +00:00
|
|
|
cobra.CheckErr(err)
|
2024-09-29 17:20:59 +00:00
|
|
|
|
|
|
|
now, err := yr.Now(location)
|
2024-09-29 17:50:45 +00:00
|
|
|
cobra.CheckErr(err)
|
2024-09-29 17:20:59 +00:00
|
|
|
|
|
|
|
if isJson {
|
|
|
|
j, err := json.MarshalIndent(now, "", " ")
|
2024-09-29 17:50:45 +00:00
|
|
|
cobra.CheckErr(err)
|
2024-09-29 17:20:59 +00:00
|
|
|
fmt.Printf("%s", j)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-29 17:50:45 +00:00
|
|
|
fmt.Printf(`Current forecast for %s
|
|
|
|
Temperature: %.1f
|
|
|
|
Rain: %.1f`, now.Location, now.Temperature, now.Percipitation)
|
2024-09-29 17:20:59 +00:00
|
|
|
}
|