-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappend.go
82 lines (70 loc) · 1.54 KB
/
append.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
package main
import (
"encoding/csv"
"fmt"
"io"
"log"
"net/http"
"os"
"regexp"
"time"
)
func readRepoNames(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
reader := csv.NewReader(file)
column, err := reader.Read()
if err != nil {
return nil, err
}
return column[1:], nil
}
func queryPolls(repo string) (string, error) {
url := fmt.Sprintf("https://hub.docker.com/v2/repositories/l7mp/%s", repo)
res, err := http.Get(url)
if err != nil {
return "", err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
res.Body.Close()
re := regexp.MustCompile(`"pull_count":([0-9]+),`)
matches := re.FindSubmatch(body)
if len(matches) < 2 {
return "", nil
}
return string(matches[1]), nil
}
func main() {
fileName := "pull-stats.csv"
repos, err := readRepoNames(fileName)
if err != nil {
log.Fatalf("Unable to read %s: %s", fileName, err.Error())
}
row := []string{time.Now().Format("2006-01-02")}
for _, r := range repos {
polls, err := queryPolls(r)
if err != nil {
log.Printf("Unable to query %s: %s", r, err.Error())
}
row = append(row, polls)
}
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
log.Fatalf("Unable to open %s: %s", fileName, err.Error())
}
defer f.Close()
w := csv.NewWriter(f)
if err := w.Write(row); err != nil {
log.Fatalf("Unable to write data: %s", err.Error())
}
w.Flush()
if err := w.Error(); err != nil {
log.Fatalf("Unable to write data: %s", err.Error())
}
}