-
Notifications
You must be signed in to change notification settings - Fork 94
/
composer.go
105 lines (82 loc) · 2.05 KB
/
composer.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
type ComposerJSON struct {
Require map[string]string `json:"require"`
RequireDev map[string]string `json:"require-dev"`
}
type ComposerLookup struct {
Packages []string
Verbose bool
}
func NewComposerLookup(verbose bool) PackageResolver {
return &ComposerLookup{Packages: []string{}, Verbose: verbose}
}
func (c *ComposerLookup) ReadPackagesFromFile(filename string) error {
rawfile, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
data := ComposerJSON{}
err = json.Unmarshal([]byte(rawfile), &data)
if err != nil {
return err
}
for pkgname := range data.Require {
c.Packages = append(c.Packages, pkgname)
}
for pkgname := range data.RequireDev {
c.Packages = append(c.Packages, pkgname)
}
return nil
}
func (c *ComposerLookup) PackagesNotInPublic() []string {
notavail := []string{}
for _, pkg := range c.Packages {
if pkg == "php" {
continue
}
if !c.isAvailableInPublic(pkg, 0) {
notavail = append(notavail, pkg)
}
}
return notavail
}
func (c *ComposerLookup) isAvailableInPublic(pkgname string, retry int) bool {
if retry > 3 {
fmt.Printf(" [W] Maximum number of retries exhausted for package %s\n", pkgname)
return false
}
if c.Verbose {
fmt.Printf("Checking: https://packagist.org/packages/%s : ", pkgname)
}
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
resp, err := client.Get("https://packagist.org/packages/" + pkgname)
if err != nil {
fmt.Printf(" [W] Error when trying to request https://packagist.org/packages/%s : %s\n", pkgname, err)
return false
}
defer resp.Body.Close()
if c.Verbose {
fmt.Printf("%s\n", resp.Status)
}
if resp.StatusCode == http.StatusOK {
return true
}
if resp.StatusCode == 429 {
fmt.Printf(" [!] Server responded with 429 (Too many requests), throttling and retrying..\n")
time.Sleep(10 * time.Second)
retry = retry + 1
c.isAvailableInPublic(pkgname, retry)
}
return false
}