1 | package main
|
2 |
|
3 | import (
|
4 | "fmt"
|
5 | "os"
|
6 |
|
7 | "github.com/jawher/mow.cli"
|
8 | )
|
9 |
|
10 | func main() {
|
11 | app := cli.App("flights", " ✈️ Find The Best Possible Flight ✈️ ")
|
12 |
|
13 | app.Command("airports", "list all possible airports", func(cmd *cli.Cmd) {
|
14 | cmd.Action = func() {
|
15 | airports := Airports()
|
16 | for _, airport := range airports.Ordered() {
|
17 | fmt.Println(airport)
|
18 | }
|
19 | }
|
20 | })
|
21 |
|
22 | app.Command("routes", "list all possible routes", func(cmd *cli.Cmd) {
|
23 | cmd.Action = func() {
|
24 | for _, route := range Routes(Airports()) {
|
25 | fmt.Println(route)
|
26 | }
|
27 | }
|
28 | })
|
29 |
|
30 | app.Command("furthest", "show the furthest distance you can travel", func(cmd *cli.Cmd) {
|
31 | var (
|
32 | departureCode = cmd.StringArg("DEPARTURE", "", "starting airport")
|
33 | )
|
34 | cmd.Action = func() {
|
35 | airports := Airports()
|
36 | start := airports.Airport(*departureCode)
|
37 | end := start
|
38 | for _, other := range airports {
|
39 | if GetDistance(start, other) > GetDistance(start, end) {
|
40 | end = other
|
41 | }
|
42 | }
|
43 | fmt.Println(Itinerary{weight: GetDistance(start, end), stops: []Airport{start, end}})
|
44 | }
|
45 | })
|
46 |
|
47 | app.Command("nearby", "show airports nearby", func(cmd *cli.Cmd) {
|
48 | var (
|
49 | threshold = cmd.IntOpt("t threshold", 500, "distance from starting airport")
|
50 | departureCode = cmd.StringArg("DEPARTURE", "", "starting airport")
|
51 | )
|
52 | cmd.Action = func() {
|
53 | airports := Airports()
|
54 | start := airports.Airport(*departureCode)
|
55 | for _, end := range NearBy(float64(*threshold), start, airports) {
|
56 | fmt.Println(Itinerary{weight: GetDistance(start, end), stops: []Airport{start, end}})
|
57 | }
|
58 | }
|
59 | })
|
60 |
|
61 | app.Command("route", "find the best possible routes", func(cmd *cli.Cmd) {
|
62 | cmd.Spec = "[OPTIONS] DEPARTURE ARRIVAL"
|
63 | var (
|
64 | //threshold = cmd.IntOpt("threshold", 100, "airport distance threshold")
|
65 | departureCode = cmd.StringArg("DEPARTURE", "", "starting airport")
|
66 | arrivalCode = cmd.StringArg("ARRIVAL", "", "ending airport")
|
67 | )
|
68 | cmd.Action = func() {
|
69 | airports := Airports()
|
70 | //shortest, weight := Load(airports.Airport(*departureCode), airports.Airport(*arrivalCode), opts)
|
71 | fmt.Println(Find(
|
72 | airports.Airport(*departureCode),
|
73 | airports.Airport(*arrivalCode),
|
74 | Load(airports, Routes(airports), ByDistance()),
|
75 | ))
|
76 | //fmt.Println(shortest, weight)
|
77 | //maybe(NewEncoder(Json, Load(airports, Routes(airports))).Encode(os.Stdout))
|
78 | /*
|
79 | departures, arrivals := FindRoutes(float64(*threshold), airports, airports[*arrivalCode], airports[*departureCode])
|
80 | if *asJson {
|
81 | data := [][]Airport{departures, arrivals}
|
82 | maybe(json.NewEncoder(os.Stdout).Encode(data))
|
83 | } else {
|
84 | fmt.Println("Departures: ")
|
85 | for _, departure := range departures {
|
86 | fmt.Println(departure)
|
87 | }
|
88 | fmt.Println("Arrivals: ")
|
89 | for _, arrival := range arrivals {
|
90 | fmt.Println(arrival)
|
91 | }
|
92 | }
|
93 | */
|
94 | }
|
95 | })
|
96 | maybe(app.Run(os.Args))
|
97 | }
|