-
Notifications
You must be signed in to change notification settings - Fork 3
/
smList.go
56 lines (46 loc) · 1.05 KB
/
smList.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
package smlist
import (
"encoding/json"
"fmt"
"io"
"os"
"sync"
"github.com/frioux/leatherman/pkg/sweetmarias"
)
/*
Run lists all of the available [Sweet Maria's](https://www.sweetmarias.com/) coffees
as json documents per line. Here's how you might see the top ten coffees by
rating:
```bash
$ sm-list | jq -r '[.Score, .Title, .URL ] | @tsv' | sort -n | tail -10
```
Command: sm-list */
func Run(_ []string, _ io.Reader) error {
wg := sync.WaitGroup{}
tokens := make(chan struct{}, 10)
coffees, err := sweetmarias.AllCoffees()
if err != nil {
return fmt.Errorf("sweetmarias.AllCoffees: %w", err)
}
e := json.NewEncoder(os.Stdout)
for _, url := range coffees {
wg.Add(1)
tokens <- struct{}{}
url := url
go func() {
defer func() { wg.Done(); <-tokens }()
c, err := sweetmarias.LoadCoffee(url)
if err != nil {
fmt.Fprintf(os.Stderr, "sweetmarias.LoadCoffee: %s\n", err)
return
}
err = e.Encode(c)
if err != nil {
fmt.Fprintf(os.Stderr, "json.Encode: %s\n", err)
return
}
}()
}
wg.Wait()
return nil
}