Skip to content
Snippets Groups Projects
Commit 86b4cfbb authored by Alex's avatar Alex
Browse files

Add hub stats function

parent 37ad1e8e
No related branches found
No related tags found
No related merge requests found
DockerHubStats
FROM golang:1.7.3-alpine
MAINTAINER alexellis2@gmail.com
ENTRYPOINT []
RUN apk --no-cache add make
WORKDIR /go/src/github.com/alexellis/faas/sample-functions/DockerHubStats
COPY . /go/src/github.com/alexellis/faas/sample-functions/DockerHubStats
RUN make
COPY fwatchdog /usr/bin/fwatchdog
ENV fprocess "/go/bin/DockerHubStats"
CMD [ "/usr/bin/fwatchdog"]
.PHONY: install image
install:
@go install .
image:
@docker build -t alexellis2/dockerhub-stats .
Building:
```
# docker service rm hubstats ; docker service create --network=functions --name=hubstats alexellis2/dockerhub-stats
```
Query the function through the gateway:
```
# curl -X POST -d "alexellis2" -v http://localhost:8080/function/hubstats
```
\ No newline at end of file
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
type dockerHubStatsType struct {
Count int `json:"count"`
}
func sanitizeInput(input string) string {
parts := strings.Split(input, "\n")
return strings.Trim(parts[0], " ")
}
func requestHubStats(org string) dockerHubStatsType {
client := http.Client{}
res, err := client.Get("https://hub.docker.com/v2/repositories/" + org)
if err != nil {
log.Fatalln("Unable to reach Docker Hub server.")
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatalln("Unable to parse response from server.")
}
dockerHubStats := dockerHubStatsType{}
json.Unmarshal(body, &dockerHubStats)
return dockerHubStats
}
func main() {
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal("Unable to read standard input:", err)
}
org := string(input)
if len(input) == 0 {
log.Fatalln("A username or organisation is required.")
}
org = sanitizeInput(org)
dockerHubStats := requestHubStats(org)
fmt.Printf("The organisation or user %s has %d repositories on the Docker hub.\n", org, dockerHubStats.Count)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment