/
pypi.go
50 lines (43 loc) · 907 Bytes
/
pypi.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
package modules
import (
"encoding/json"
"errors"
"net/http"
)
type PyPi struct{}
func (pp PyPi) Init() {}
func (pp PyPi) Name() string {
return "pypi"
}
type pyPiInfo struct {
Version string `json:"version"`
}
type pyPiResponse struct {
Info pyPiInfo `json:"info"`
}
func (pp PyPi) Run(upstream string, params interface{}) (version string, err error) {
conf := params.(map[string]interface{})
pkg, ok := conf["package"]
if !ok {
err = errors.New("Package name is missing")
return
}
resp, err := http.Get("https://pypi.python.org/pypi/" + pkg.(string) + "/json")
if err != nil {
return
}
defer resp.Body.Close()
// Decode
jsonResp := new(pyPiResponse)
err = json.NewDecoder(resp.Body).Decode(jsonResp)
if err != nil {
return
}
// Get package version
if jsonResp.Info.Version == "" {
err = errors.New("No Version")
return
}
version = jsonResp.Info.Version
return
}