-
Notifications
You must be signed in to change notification settings - Fork 1
/
aur.go
336 lines (306 loc) · 9.13 KB
/
aur.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright (c) 2015, Ben Morgan. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// Package aur lets you query the Arch Linux User Repository (AUR).
package aur
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/goulash/pacman"
"github.com/goulash/pacman/alpm"
)
// Packages is a slice of Package with several methods facilitating
// sorting, iterating, and converting to pacman.Packages.
type Packages []*Package
func (pkgs Packages) Len() int { return len(pkgs) }
func (pkgs Packages) Swap(i, j int) { pkgs[i], pkgs[j] = pkgs[j], pkgs[i] }
func (pkgs Packages) Less(i, j int) bool {
if pkgs[i].Name != pkgs[j].Name {
return pkgs[i].Name < pkgs[j].Name
}
return alpm.VerCmp(pkgs[i].Version, pkgs[j].Version) == -1
}
// Pkgs returns the entire slice as pacman.Packages.
func (pkgs Packages) Pkgs() pacman.Packages {
results := make(pacman.Packages, len(pkgs))
for i, p := range pkgs {
results[i] = p.Pkg()
}
return results
}
// Iterate calls f for each package in the list of packages.
func (pkgs Packages) Iterate(f func(pacman.AnyPackage)) {
for _, p := range pkgs {
f(p)
}
}
// NotFoundError is returned when a package could not be found on AUR.
//
// The error message returned is different dependent on the number of packages
// that could not be found.
type NotFoundError struct {
Names []string
}
func (e NotFoundError) Error() string {
n := len(e.Names)
if n == 1 {
return fmt.Sprintf("package %q could not be found on AUR", e.Names[0])
} else if n == 2 {
return fmt.Sprintf("packages %q and %q could not be found on AUR", e.Names[0], e.Names[1])
}
// We have three or more packages that we could not find.
var buf bytes.Buffer
buf.WriteString("packages ")
for _, nam := range e.Names[:n-1] {
buf.WriteRune('"')
buf.WriteString(nam)
buf.WriteString(`", `)
}
buf.WriteString(`and "`)
buf.WriteString(e.Names[n-1])
buf.WriteString(`" could not be found on AUR`)
return buf.String()
}
// IsNotFound returns true if the error is an instance of NotFoundError.
func IsNotFound(err error) bool {
_, ok := err.(*NotFoundError)
return ok
}
// response is what the AUR api returns:
//
// {
// "version":5,
// "type":"multiinfo",
// "resultcount":1,
// "results": [
// {
// "ID":279276,
// "Name":"telegram-desktop",
// "PackageBaseID":95783,
// "PackageBase":"telegram-desktop",
// "Version":"0.9.28-1",
// "Description":"Official desktop version of Telegram messaging app.",
// "URL":"https:\/\/desktop.telegram.org\/",
// "NumVotes":43,
// "Popularity":8.94582,
// "OutOfDate":null,
// "Maintainer":"eduardosm",
// "FirstSubmitted":1436478182,
// "LastModified":1456515415,
// "URLPath":"\/cgit\/aur.git\/snapshot\/telegram-desktop.tar.gz",
// "Depends":["ffmpeg","icu","jasper","libexif","libmng","libwebp",
// "libxkbcommon-x11","libinput","libproxy","mtdev",
// "openal","libva","desktop-file-utils","gtk-update-icon-cache"],
// "MakeDepends":["git","patch","libunity","libappindicator-gtk2"]
// "License":["GPL3"]
// "Keywords":[]
// }
// ]
// }
type response struct {
Version int `json:"version"`
Type string `json:"type"`
ResultCount int `json:"resultcount"`
Results []*Package `json:"results"`
}
// Package is the information that we can retrieve about a package that is
// hosted on the Arch Linux User Repository (AUR), version 4.
//
// JSON Example:
//
// {
// "ID": 213309,
// "Name": "repoctl",
// "PackageBaseID": 96153,
// "PackageBase": "repoctl",
// "Version": "0.13-2",
// "Description": "A supplement to repo-add and repo-remove which simplifies managing local repositories",
// "URL": "https:\/\/github.com\/cassava\/repoctl",
// "NumVotes": 1,
// "OutOfDate": 0,
// "Maintainer": "cassava",
// "FirstSubmitted": 1437296687,
// "LastModified": 1437298275,
// "License": "MIT",
// "URLPath": "\/cgit\/aur.git\/snapshot\/repoctl.tar.gz",
// "CategoryID": 1,
// "Popularity": 0
// }
type Package struct {
ID uint64
Name string
PackageBaseID uint64
PackageBase string
Version string
Description string
URL string
NumVotes int
Popularity float64
OutOfDate int
Maintainer string
FirstSubmitted uint64
LastModified uint64
URLPath string
Depends []string
MakeDepends []string
License []string
Keywords []string
}
// Pkg converts an aur.Package into a pacman.Package.
//
// Note that only a few fields in the resulting Package are actually filled in,
// namely Origin, Name, Version, Description, URL, and License. This is all the
// information that we are able to retrieve.
func (p *Package) Pkg() *pacman.Package {
return &pacman.Package{
Origin: pacman.AUROrigin,
Name: p.Name,
Base: p.PackageBase,
Version: p.Version,
Description: p.Description,
URL: p.URL,
// TODO: License is string, but p.License is []string
// Has there been a format change?
//License: p.License,
Depends: p.Depends,
MakeDepends: p.MakeDepends,
}
}
// PkgName returns the unique name of the package.
func (p *Package) PkgName() string { return p.Name }
// PkgVersion returns the version string of the package.
func (p *Package) PkgVersion() string { return p.Version }
// PkgDepends returns the dependencies of the package.
func (p *Package) PkgDepends() []string { return p.Depends }
// PkgMakeDepends returns the make dependenciess of the package.
func (p *Package) PkgMakeDepends() []string { return p.MakeDepends }
// DownloadURL returns the URL for downloading the PKGBUILD tarball.
func (p *Package) DownloadURL() string {
return fmt.Sprintf("https://aur.archlinux.org%s", p.URLPath)
}
const (
searchURL = "https://aur.archlinux.org/rpc.php?v=5&type=search&by=name&arg=%s"
multiInfoURL = "https://aur.archlinux.org/rpc.php?v=5&type=multiinfo&arg[]=%s"
multiInfoArg = "&arg[]="
)
// generateMultiInfoURL creates a URL that gets the package information from AUR.
func generateMultiInfoURL(args []string) string {
na := make([]string, len(args))
for i, s := range args {
na[i] = url.QueryEscape(s)
}
return fmt.Sprintf(multiInfoURL, strings.Join(na, multiInfoArg))
}
func SearchByName(query string) (Packages, error) {
q := fmt.Sprintf(searchURL, url.QueryEscape(query))
resp, err := http.Get(q)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var msg response
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&msg)
return msg.Results, err
}
// Read reads package information from the Arch Linux User Repository (AUR)
// online.
//
// If a package cannot be found, (nil, *NotFoundError) is returned.
func Read(pkgname string) (*Package, error) {
q := generateMultiInfoURL([]string{pkgname})
resp, err := http.Get(q)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var msg response
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&msg)
if err != nil {
return nil, err
}
if msg.ResultCount == 0 {
return nil, &NotFoundError{Names: []string{pkgname}}
}
return msg.Results[0], nil
}
// ReadAll reads multiple packages from the Arch Linux User Repository (AUR)
// at once.
//
// If any packages cannot be found, (Packages, *NotFoundError) is returned.
// That is, all successfully read packages are returned.
func ReadAll(pkgnames []string) (Packages, error) {
// We only query at most 200 packages at a time, the limit currently
// appears to be 250, but we'll stay well beneath that for now.
const limit = 200
if len(pkgnames) <= limit {
return readAll(pkgnames)
}
var pkgs Packages
err := &NotFoundError{
Names: make([]string, 0, 0),
}
for len(pkgnames) > 0 {
// Select next slice of messages
var slice []string
if len(pkgnames) > limit {
slice = pkgnames[:limit]
pkgnames = pkgnames[limit:]
} else {
slice = pkgnames
pkgnames = []string{}
}
// Query selected slice of messages
p, e := readAll(slice)
if e != nil {
nfe, ok := e.(*NotFoundError)
if !ok {
// e is not NotFoundError, and we don't know how to handle it,
// so return directly as-is.
return nil, e
}
err.Names = append(err.Names, nfe.Names...)
}
pkgs = append(pkgs, p...)
}
if len(err.Names) == 0 {
return pkgs, nil
} else {
return pkgs, err
}
}
func readAll(pkgnames []string) (Packages, error) {
q := generateMultiInfoURL(pkgnames)
resp, err := http.Get(q)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var msg response
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&msg)
if err != nil {
return nil, err
}
if msg.ResultCount != len(pkgnames) {
m := make(map[string]bool)
nfe := &NotFoundError{
Names: make([]string, 0, len(pkgnames)-msg.ResultCount),
}
for _, i := range msg.Results {
m[i.Name] = true
}
for _, s := range pkgnames {
if !m[s] {
nfe.Names = append(nfe.Names, s)
}
}
return msg.Results, nfe
}
return msg.Results, nil
}