-
Notifications
You must be signed in to change notification settings - Fork 6
/
AareGuru.go
87 lines (75 loc) · 2.79 KB
/
AareGuru.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"github.com/gexclaude/aaregurucli/api"
"github.com/gexclaude/aaregurucli/console"
"github.com/gexclaude/aaregurucli/outcities"
"github.com/gexclaude/aaregurucli/outstd"
"github.com/gexclaude/aaregurucli/outtypewrt"
"github.com/gexclaude/aaregurucli/texts"
"gopkg.in/alecthomas/kingpin.v2"
"os"
"sync"
)
var (
app = kingpin.New("aareguru", texts.CliDescription)
standard = app.Command("standard", texts.CliCommandStandardDescription).Default()
cityStandard = standard.Arg("city", texts.CliCityDescription).String()
typewriter = app.Command("schribmaschine", texts.CliCommandTypewriterDescription)
cityTypewriter = typewriter.Arg("city", texts.CliCityDescription).String()
cities = app.Command("cities", texts.CliCommandCitiesDescription)
proxy = app.Flag("proxy", texts.CliProxyDescription).Short('p').String()
colorless = app.Flag("ohni-farb", texts.CliColorlessDescription).Short('f').Bool()
noprogressbar = app.Flag("ohni-ladebauke", texts.CliNoprogressbarDescription).Short('l').Bool()
debug = app.Flag("debug", texts.CliProxyDescription).Short('d').Hidden().Bool()
)
func main() {
kingpin.MustParse(app.Parse(os.Args[1:]))
console.InitConsole(!*colorless)
console.ClearConsole()
fmt.Println()
errChannel := make(chan string)
var wg sync.WaitGroup
defer func() {
if r := recover(); r != nil {
fmt.Println()
fmt.Println(console.CRed(texts.ErrorDetailMsg))
fmt.Println(r)
fmt.Println()
fmt.Print(texts.ErrorHintsMsg)
fmt.Print()
}
console.BeforeExitConsole()
}()
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case standard.FullCommand():
outstd.Init(!*noprogressbar)
aareGuruResponseChannel := askAareGuru(errChannel, &wg, cityStandard)
outstd.RenderAareGuruResponse(aareGuruResponseChannel, errChannel, &wg)
case typewriter.FullCommand():
outtypewrt.Init()
aareGuruResponseChannel := askAareGuru(errChannel, &wg, cityTypewriter)
outtypewrt.RenderAareGuruResponse(aareGuruResponseChannel, errChannel, &wg)
case cities.FullCommand():
citiesResponseChannel := askAareGuruForCities(errChannel, &wg)
outcities.RenderCities(citiesResponseChannel, errChannel, &wg)
}
}
func askAareGuru(errChannel chan<- string, wg *sync.WaitGroup, city *string) chan api.AareGuruResponse {
aareGuruResponseChannel := make(chan api.AareGuruResponse)
go func() {
defer wg.Done()
wg.Add(1)
api.AskAareGuru(proxy, city, aareGuruResponseChannel, errChannel, *debug)
}()
return aareGuruResponseChannel
}
func askAareGuruForCities(errChannel chan<- string, wg *sync.WaitGroup) chan api.CitiesResponse {
citiesResponseChannel := make(chan api.CitiesResponse)
go func() {
defer wg.Done()
wg.Add(1)
api.AskAareGuruForCities(proxy, citiesResponseChannel, errChannel, *debug)
}()
return citiesResponseChannel
}