-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (63 loc) · 1.51 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/BenchR267/goalfred"
)
func main() {
queryTerms, _ := goalfred.NormalizedArguments()
defer goalfred.Print()
pods := getPods(strings.Join(queryTerms, "%20"))
for _, pod := range pods {
goalfred.Add(pod)
}
}
type pod struct {
Link string
Name string `json:"id"`
Summary string
Version string
Platforms []string
}
func (p pod) Documentation() string {
return fmt.Sprintf("http://cocoadocs.org/docsets/%s/%s", p.Name, p.Version)
}
func (p pod) Item() goalfred.Item {
title := fmt.Sprintf("%s (%s)", p.Name, p.Version)
instruction := fmt.Sprintf("pod '%s', '%s'", p.Name, p.Version)
i := goalfred.Item{
Title: title,
Subtitle: p.Summary,
Arg: p.Link,
}
i.Mod.Cmd = &goalfred.ModContent{
Arg: p.Documentation(),
Subtitle: "Open documentation!",
}
i.Mod.Alt = &goalfred.ModContent{
Arg: instruction,
Subtitle: "Copy pod install instructions",
}
return i
}
type response struct {
Allocations [][]json.RawMessage `json:"allocations"`
}
func getPods(searchTerm string) []pod {
url := fmt.Sprintf("https://search.cocoapods.org/api/v1/pods.picky.hash.json?query=%v&ids=20&offset=0&sort=quality", searchTerm)
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var res response
json.Unmarshal(body, &res)
rawJSON := res.Allocations[0][5]
var pods []pod
json.Unmarshal(rawJSON, &pods)
return pods
}