2024-09-29 17:20:59 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.sr.ht/~timharek/yr-go/yr"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var nowCmd = &cobra.Command{
|
|
|
|
Use: "now",
|
|
|
|
Aliases: []string{"current", "n"},
|
|
|
|
Short: "Get current forecasted weather",
|
|
|
|
Args: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) < 1 || len(args) > 1 {
|
|
|
|
return errors.New("Need a maximum of one argument")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Run: now,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|