return err from json unmarshal

Signed-off-by: Burton Rheutan <rheutan7@gmail.com>
This commit is contained in:
Burton Rheutan 2018-03-05 11:35:44 -06:00 committed by Alex Ellis
parent 26c8671865
commit 936beb2166

View File

@ -38,16 +38,16 @@ func requestStats(repo string) []byte {
return body return body
} }
func parseOrgStats(response []byte) dockerHubOrgStatsType { func parseOrgStats(response []byte) (dockerHubOrgStatsType, error) {
dockerHubOrgStats := dockerHubOrgStatsType{} dockerHubOrgStats := dockerHubOrgStatsType{}
json.Unmarshal(response, &dockerHubOrgStats) err := json.Unmarshal(response, &dockerHubOrgStats)
return dockerHubOrgStats return dockerHubOrgStats, err
} }
func parseRepoStats(response []byte) dockerHubRepoStatsType { func parseRepoStats(response []byte) (dockerHubRepoStatsType, error) {
dockerHubRepoStats := dockerHubRepoStatsType{} dockerHubRepoStats := dockerHubRepoStatsType{}
json.Unmarshal(response, &dockerHubRepoStats) err := json.Unmarshal(response, &dockerHubRepoStats)
return dockerHubRepoStats return dockerHubRepoStats, err
} }
func main() { func main() {
@ -64,10 +64,18 @@ func main() {
response := requestStats(request) response := requestStats(request)
if strings.Contains(request, "/") { if strings.Contains(request, "/") {
dockerHubRepoStats := parseRepoStats(response) dockerHubRepoStats, err := parseRepoStats(response)
fmt.Printf("Repo: %s has been pulled %d times from the Docker Hub", request, dockerHubRepoStats.PullCount) if err != nil {
log.Fatalln("Unable to parse response from Docker Hub for repository")
} else {
fmt.Printf("Repo: %s has been pulled %d times from the Docker Hub", request, dockerHubRepoStats.PullCount)
}
} else {
dockerHubOrgStats, err := parseOrgStats(response)
if err != nil {
log.Fatalln("Unable to parse response from Docker Hub for user/organisation")
} else { } else {
dockerHubOrgStats := parseOrgStats(response)
fmt.Printf("The organisation or user %s has %d repositories on the Docker hub.\n", request, dockerHubOrgStats.Count) fmt.Printf("The organisation or user %s has %d repositories on the Docker hub.\n", request, dockerHubOrgStats.Count)
} }
}
} }