-
Notifications
You must be signed in to change notification settings - Fork 64
/
helm_hub.go
88 lines (72 loc) · 1.57 KB
/
helm_hub.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
package registries
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
var (
// ErrMultipleCharts multiple charts found
ErrMultipleCharts = errors.New("multiple charts found")
// ErrNoChartsFound no charts found for the name
ErrNoChartsFound = fmt.Errorf("Could not find the chart")
hubCache = artifacthubDump{}
mu sync.Mutex
)
const (
baseURL = "https://artifacthub.io"
userAgent = "helm-exporter/1.0"
)
type artifacthubDump []hubChart
type hubChart struct {
Name string `json:"name"`
Version string `json:"version"`
Repository struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"repository"`
}
func httpGet(endpoint string, data interface{}) error {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", baseURL, endpoint), nil)
if err != nil {
return err
}
req.Header.Set("User-Agent", userAgent)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(data)
}
func init() {
err := update()
if err != nil {
log.Warnf("failed to update artifacthub cache due to %v ", err)
}
go func() {
for {
time.Sleep(time.Hour)
log.Info("updating artifacthub dump")
err := update()
if err != nil {
log.Warnf("failed to update artifacthub cache due to %v ", err)
}
}
}()
}
func update() error {
data := &artifacthubDump{}
err := httpGet("api/v1/helm-exporter", data)
if err != nil {
return err
}
mu.Lock()
hubCache = *data
mu.Unlock()
return nil
}