From 809b1b5fae7a4416b0ab41d5b285b8fa207d9f8f Mon Sep 17 00:00:00 2001 From: ringsaturn Date: Tue, 15 Mar 2022 00:35:43 +0800 Subject: [PATCH] init cli (#3) --- cmd/placekey/main.go | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 cmd/placekey/main.go diff --git a/cmd/placekey/main.go b/cmd/placekey/main.go new file mode 100644 index 0000000..26f5548 --- /dev/null +++ b/cmd/placekey/main.go @@ -0,0 +1,52 @@ +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/ringsaturn/pk" +) + +const FromGeoFlag = "FromGeo" +const ToGeoFlag = "ToGeo" + +func showErr() { + fmt.Printf("expected '%v' or '%v' subcommands\n", FromGeoFlag, ToGeoFlag) + os.Exit(1) +} + +func main() { + if len(os.Args) < 2 { + showErr() + return + } + + switch os.Args[1] { + case FromGeoFlag: + FromGeoCmd := flag.NewFlagSet(FromGeoFlag, flag.ExitOnError) + lat := FromGeoCmd.Float64("lat", 0, "latitude") + long := FromGeoCmd.Float64("long", 0, "longitude") + if err := FromGeoCmd.Parse(os.Args[2:]); err != nil { + panic(err) + } + placeKey, err := pk.GeoToPlacekey(*lat, *long) + if err != nil { + panic(err) + } + fmt.Println(placeKey) + case ToGeoFlag: + ToGeoCmd := flag.NewFlagSet(ToGeoFlag, flag.ExitOnError) + placekey := ToGeoCmd.String("pk", "", "the place key need to convert to geo") + if err := ToGeoCmd.Parse(os.Args[2:]); err != nil { + panic(err) + } + lat, long, err := pk.PlacekeyToGeo(*placekey) + if err != nil { + panic(err) + } + fmt.Println(lat, long) + default: + showErr() + } +}