-
Notifications
You must be signed in to change notification settings - Fork 0
/
place.go
89 lines (74 loc) · 1.96 KB
/
place.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
88
89
package main
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"github.com/aabizri/transit/maps"
"github.com/aabizri/navitia"
"github.com/aabizri/navitia/pretty"
"github.com/fatih/color"
"github.com/fogleman/gg"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var placeFlags = []cli.Flag{
cli.UintFlag{
Name: "count, c",
Value: 6,
},
}
var placeCommand = cli.Command{
Name: "place",
Aliases: []string{"places,p"},
Usage: "Search for places",
Action: placeAction,
Flags: placeFlags,
}
func placeAction(c *cli.Context) error {
gctx := context.Background()
for i, query := range c.Args() {
ctx, cancel := context.WithTimeout(gctx, requestTimeout)
defer cancel()
// Create a request
req := navitia.PlacesRequest{Query: query, Count: c.Uint("count")}
// Execute it
pr, err := session.Places(ctx, req)
if err != nil {
return errors.Wrap(err, "Error while requesting places")
}
// Now let's print
msg := fmt.Sprintf("\n[%d/%d] Query \"%s\" ", i+1, len(c.Args()), color.New(color.Underline, color.FgHiCyan).Sprint(query))
buf := bytes.NewBuffer([]byte(msg))
err = pretty.DefaultPlacesResultsConf.PrettyWrite(pr, buf)
if err != nil {
return errors.Wrapf(err, "Error while preparing result output for query #%d", i)
}
// And copy
_, err = io.Copy(os.Stdout, buf)
if err != nil {
return errors.Wrapf(err, "error while copying buffer to stdout")
}
// If enabled, draw !
if path := c.GlobalString("map"); path != "" {
if !filepath.IsAbs(path) {
wd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "couldn't retrieve working directory, consider giving an absolute path")
}
path = filepath.Join(wd, path)
}
img, err := maps.DrawPlaces(pr.Places)
if err != nil {
return errors.Wrap(err, "error while drawing places")
}
err = gg.SavePNG(path, img)
if err != nil {
return errors.Wrapf(err, "error while saving image to %s", path)
}
}
}
return nil
}