savory
Version:
A command-line interface for operating your Codefresh account
86 lines (82 loc) • 4.06 kB
JavaScript
const
fp = require('lodash/fp'),
chalk = require('chalk'),
bytes = require('bytes'),
jsYaml = require('js-yaml'),
moment = require('moment'),
{ middleware: httpClientMiddleware } = require('../../lib/api_client'),
{
jsonFormatter,
tableFormatter,
errorFormatter
} = require('../style');
const
DEFAULT_API_RESULT_LIMIT = 25,
CODEFRESH_NATIVE_REGISTRY = ["r.cfcr.io"],
CODEFRESH_VOLUME_TAG = "volume",
CAPTION_NONE = "none",
CHARS_ID = 15,
CHARS_NAME = 50,
CHARS_TAG = 20,
CHARS_PULL = 50;
module.exports = {
command: ["list [id..]", "ls"],
builder: (yargs) => {
yargs
.usage('Retrieves a list of images in your Codefresh account.\nAdd specific image IDs in order to list them exclusively, or get the complete list.')
.option('output', {
choices: ["summary", "yaml", "json"],
default: "summary",
describe: "Choose a output format for the returned data"
})
.option('external', {
describe: "Include registries hosted outside of Codefresh (external)",
alias: "e",
default: false,
type: "boolean"
})
.option('api-limit', {
type: "number",
default: DEFAULT_API_RESULT_LIMIT,
describe: "Limit the amount of results returned from the API"
})
.group(["external"], "Image filter options:")
.example('$0 images list 9b189b30b6008 ce7013050364b')
.example('$0 images list -e');
},
describe: "Retrieve a list of Docker images",
handler: fp.pipe(httpClientMiddleware, ({ _httpClient, apiLimit, output, id, external }) => {
(fp.isArray(id)
? Promise.all(id.slice(0, apiLimit).map((id) => _httpClient(`images/${id}`)))
: _httpClient('images/', { limit: apiLimit }).then(fp.get('docs')))
.then(fp.pipe(
{
"summary": (rawResult)=> {
let processedResult = fp.pipe(
fp.map(({ internalImageId, imageDisplayName, tags, created, size } = {}) => {
return (fp.isEmpty(tags) ? [{}] : tags)
.filter(({ tag, registry } = {})=> ((tag !== CODEFRESH_VOLUME_TAG ) && (external || !tag || CODEFRESH_NATIVE_REGISTRY.includes(registry))))
.map(({ tag, registry, repository })=> [
fp.truncate({ length: CHARS_ID, omission: ".." }, internalImageId),
fp.truncate({ length: CHARS_NAME, omission: ".." }, imageDisplayName),
fp.truncate({ length: CHARS_TAG, omission: ".." }, tag || chalk.gray(`<${CAPTION_NONE}>`)),
moment(created).fromNow(),
bytes(size),
fp.truncate({ length: CHARS_PULL, omission: ".." }, tag ? `${registry}/${repository}:${tag}` : "")
])
}),
fp.flatten
)(rawResult);
return [
processedResult.length ? `\n Displaying ${processedResult.length} images out of ${ rawResult.length } returned values:\n` : `No viewable items returned`,
processedResult.length && tableFormatter({ title: ["Id", "Name", "Tag", "Created", "Size", "Pull"] }, processedResult),
].filter(Boolean).join('\n');
},
"json": jsonFormatter,
"yaml": jsYaml.safeDump
}[output],
console.log
))
.catch(fp.pipe(errorFormatter, console.warn));
})
};