package astro import ( "fmt" "github.com/spf13/cobra" "time" ) var isFormat bool func init() { Cmd.PersistentFlags().Float64Var(&lon, "lon", -273, "经度,WGS84坐标系") Cmd.PersistentFlags().Float64Var(&lat, "lat", -273, "纬度,WGS84坐标系") Cmd.PersistentFlags().Float64Var(&height, "height", 0, "海拔高度") CmdSun.Flags().StringVarP(&nowDay, "now", "n", "", "指定现在的时间") CmdSun.Flags().BoolVarP(&isTimestamp, "timestamp", "t", false, "是否为时间戳") CmdSun.Flags().BoolVarP(&isLive, "live", "v", false, "是否为实时") CmdSun.Flags().BoolVarP(&isFormat, "format", "f", false, "格式化输出") CmdSun.Flags().StringVarP(&city, "city", "c", "", "城市名") CmdMoon.Flags().StringVarP(&nowDay, "now", "n", "", "指定现在的时间") CmdMoon.Flags().BoolVarP(&isTimestamp, "timestamp", "t", false, "是否为时间戳") CmdMoon.Flags().BoolVarP(&isLive, "live", "v", false, "是否为实时") CmdMoon.Flags().BoolVarP(&isFormat, "format", "f", false, "格式化输出") CmdMoon.Flags().StringVarP(&city, "city", "c", "", "城市名") CmdStar.Flags().StringVarP(&nowDay, "now", "n", "", "指定现在的时间") CmdStar.Flags().BoolVarP(&isTimestamp, "timestamp", "t", false, "是否为时间戳") CmdStar.Flags().BoolVarP(&isLive, "live", "v", false, "是否为实时") CmdStar.Flags().BoolVarP(&isFormat, "format", "f", false, "格式化输出") CmdStar.Flags().StringVarP(&city, "city", "c", "", "城市名") Cmd.AddCommand(CmdCal, CmdSun, CmdMoon, CmdStar) } var Cmd = &cobra.Command{ Use: "astro", Short: "天文计算", } var CmdSun = &cobra.Command{ Use: "sun", Short: "太阳计算", Run: func(cmd *cobra.Command, args []string) { format := 0 if isFormat { format = 1 } isSet := CliLoadLonLatHeight() var now = time.Now() var err error if nowDay != "" { now, err = parseDate(now, nowDay, isTimestamp) if err != nil { fmt.Println(err) return } } for { if isSet { fmt.Printf("经度: %f 纬度: %f 海拔: %f\n", lon, lat, height) } BasicSun(now, uint8(format)) if isSet { SunDetail(now, lon, lat, height, uint8(format)) } if !isLive { break } time.Sleep(time.Nanosecond* time.Duration(1000000000-time.Now().Nanosecond()) + 1) if nowDay == "" { now = time.Now() now = now.Add(time.Duration(now.Nanosecond()*-1) * time.Nanosecond) } else { now = now.Add(time.Second) } ClearScreen() } }, } var CmdMoon = &cobra.Command{ Use: "moon", Short: "月亮计算", Run: func(cmd *cobra.Command, args []string) { format := 0 if isFormat { format = 1 } isSet := CliLoadLonLatHeight() var now = time.Now() var err error if nowDay != "" { now, err = parseDate(now, nowDay, isTimestamp) if err != nil { fmt.Println(err) return } } for { if isSet { fmt.Printf("经度: %f 纬度: %f 海拔: %f\n", lon, lat, height) } BasicMoon(now, uint8(format)) if isSet { MoonDetail(now, lon, lat, height, uint8(format)) } if !isLive { break } time.Sleep(time.Nanosecond* time.Duration(1000000000-time.Now().Nanosecond()) + 1) if nowDay == "" { now = time.Now() now = now.Add(time.Duration(now.Nanosecond()*-1) * time.Nanosecond) } else { now = now.Add(time.Second) } ClearScreen() } }, } var CmdStar = &cobra.Command{ Use: "star", Short: "星星计算", Run: func(cmd *cobra.Command, args []string) { if len(args) == 0 { fmt.Println("请输入星星名字") return } format := 0 if isFormat { format = 1 } isSet := CliLoadLonLatHeight() var now = time.Now() var err error if nowDay != "" { now, err = parseDate(now, nowDay, isTimestamp) if err != nil { fmt.Println(err) return } } for { if isSet { fmt.Printf("经度: %f 纬度: %f 海拔: %f\n", lon, lat, height) } BasicStar(now, args[0], uint8(format)) if isSet { StarDetail(now, args[0], lon, lat, height, uint8(format)) } if !isLive { break } time.Sleep(time.Nanosecond* time.Duration(1000000000-time.Now().Nanosecond()) + 1) if nowDay == "" { now = time.Now() now = now.Add(time.Duration(now.Nanosecond()*-1) * time.Nanosecond) } else { now = now.Add(time.Second) } ClearScreen() } }, } func CliLoadLonLatHeight() bool { if city != "" { if !GetFromCity(city) { fmt.Println("城市名错误") return false } fmt.Println("城市名: ", city) SetLonLatHeight(lon, lat, height) return true } tlon, tlat, theight := lon, lat, height LoadLonLatHeight() if lon == -273 && lon != tlon { lon = tlon } if lat == -273 && lat != tlat { lat = tlat } if height == 0 && height != theight { height = theight } SetLonLatHeight(lon, lat, height) if lon == -273 || lat == -273 { return false } return true }