2019-05-31 06:35:44 +00:00
|
|
|
import millify from 'millify'
|
|
|
|
import got from '../libs/got'
|
2019-11-18 15:52:07 +00:00
|
|
|
import { createBadgenHandler, PathArgs } from '../libs/create-badgen-handler'
|
2019-05-31 06:35:44 +00:00
|
|
|
|
2019-11-18 15:52:07 +00:00
|
|
|
export default createBadgenHandler({
|
2019-05-31 06:35:44 +00:00
|
|
|
title: 'Docker',
|
|
|
|
examples: {
|
|
|
|
'/docker/pulls/library/ubuntu': 'pulls (library)',
|
|
|
|
'/docker/stars/library/ubuntu': 'stars (library)',
|
2019-12-12 06:01:22 +00:00
|
|
|
'/docker/size/library/ubuntu': 'size (library)',
|
2019-05-31 06:35:44 +00:00
|
|
|
'/docker/pulls/amio/node-chrome': 'pulls (scoped)',
|
|
|
|
'/docker/stars/library/mongo?icon=docker&label=stars': 'stars (icon & label)',
|
2019-12-12 06:01:22 +00:00
|
|
|
'/docker/size/lukechilds/bitcoind/latest/amd64': 'size (scoped/tag/architecture)',
|
2019-11-18 15:52:07 +00:00
|
|
|
},
|
|
|
|
handlers: {
|
2019-12-12 06:01:22 +00:00
|
|
|
'/docker/:topic<stars|pulls>/:scope/:name': starPullHandler,
|
|
|
|
'/docker/size/:scope/:name/:tag?/:architecture?': sizeHandler
|
2019-05-31 06:35:44 +00:00
|
|
|
}
|
2019-11-18 15:52:07 +00:00
|
|
|
})
|
2019-05-31 06:35:44 +00:00
|
|
|
|
2019-12-12 06:01:22 +00:00
|
|
|
async function starPullHandler ({ topic, scope, name }: PathArgs) {
|
2019-05-31 06:35:44 +00:00
|
|
|
if (!['stars', 'pulls'].includes(topic)) {
|
|
|
|
return {
|
|
|
|
subject: 'docker',
|
|
|
|
status: 'unknown topic',
|
|
|
|
color: 'grey'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
const endpoint = `https://hub.docker.com/v2/repositories/${scope}/${name}`
|
|
|
|
const { pull_count, star_count } = await got(endpoint).then(res => res.body)
|
|
|
|
|
|
|
|
switch (topic) {
|
|
|
|
case 'stars':
|
|
|
|
return {
|
|
|
|
subject: 'docker stars',
|
|
|
|
status: millify(star_count),
|
|
|
|
color: 'blue'
|
|
|
|
}
|
|
|
|
case 'pulls':
|
|
|
|
return {
|
|
|
|
subject: 'docker pulls',
|
|
|
|
status: millify(pull_count),
|
|
|
|
color: 'blue'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-12 06:01:22 +00:00
|
|
|
|
|
|
|
async function sizeHandler ({ scope, name, tag, architecture }: PathArgs) {
|
|
|
|
tag = tag ? tag : 'latest'
|
|
|
|
architecture = architecture ? architecture : 'amd64'
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
const endpoint = `https://hub.docker.com/v2/repositories/${scope}/${name}/tags`
|
|
|
|
let body = await got(endpoint).then(res => res.body)
|
|
|
|
|
|
|
|
let results = [...body.results]
|
|
|
|
while (body.next) {
|
|
|
|
body = await got(body.next).then(res => res.body)
|
|
|
|
results = [...results, ...body.results]
|
|
|
|
}
|
|
|
|
|
|
|
|
const tagData = results.find(tagData => tagData.name === tag)
|
|
|
|
|
|
|
|
if (!tagData) {
|
|
|
|
return {
|
|
|
|
subject: 'docker',
|
|
|
|
status: 'unknown tag',
|
|
|
|
color: 'grey'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const imageData = tagData.images.find(image => image.architecture === architecture)
|
|
|
|
|
|
|
|
if (!imageData) {
|
|
|
|
return {
|
|
|
|
subject: 'docker',
|
|
|
|
status: 'unknown architecture',
|
|
|
|
color: 'grey'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const sizeInMegabytes = (imageData.size / 1024 / 1024).toFixed(2)
|
|
|
|
|
|
|
|
return {
|
|
|
|
subject: 'docker image size',
|
|
|
|
status: `${sizeInMegabytes} MB`,
|
|
|
|
color: 'blue'
|
|
|
|
}
|
|
|
|
}
|