Skip to content

Commit

Permalink
init cli (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringsaturn authored Mar 14, 2022
1 parent 1711e29 commit 809b1b5
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cmd/placekey/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 809b1b5

Please sign in to comment.