/
docker.go
52 lines (45 loc) · 1.18 KB
/
docker.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
package adapter
import (
"fmt"
"github.com/dustin/go-humanize"
"github.com/tidwall/gjson"
"github.com/zcong1993/badge-service/utils"
)
var defaultErrorResp = makeUnknownTopicInput("docker")
// VALID_TOPICS is valid topic docker api support
var VALID_TOPICS = []string{"stars", "pulls"}
// DockerApi is docker hub api provider
func DockerApi(args ...string) BadgeInput {
if len(args) != 3 {
return ErrorInput
}
topic := args[0]
namespace := args[1]
name := args[2]
if !utils.IsOneOf(VALID_TOPICS, topic) {
return defaultErrorResp
}
endpoint := fmt.Sprintf("https://hub.docker.com/v2/repositories/%s/%s", namespace, name)
resp, err := utils.Get(endpoint)
if err != nil {
return ErrorInput
}
pullCount := gjson.Get(string(resp), "pull_count").Float()
starCount := gjson.Get(string(resp), "star_count").Float()
switch topic {
case "stars":
return BadgeInput{
Subject: "docker stars",
Status: utils.StringOrDefault(humanize.SI(starCount, ""), "0"),
Color: "blue",
}
case "pulls":
return BadgeInput{
Subject: "docker pulls",
Status: utils.StringOrDefault(humanize.SI(pullCount, ""), "0"),
Color: "blue",
}
default:
return defaultErrorResp
}
}