|
| 1 | +const Command = require('../../Command'); |
| 2 | +const _ = require('lodash'); |
| 3 | +const { repository } = require('../../../../logic').api; |
| 4 | +const { specifyOutputForArray } = require('../../helpers/get'); |
| 5 | +const getRoot = require('../root/get.cmd'); |
| 6 | +const Spinner = require('ora'); |
| 7 | + |
| 8 | +const command = new Command({ |
| 9 | + command: 'repository [names..]', |
| 10 | + aliases: ['repo'], |
| 11 | + parent: getRoot, |
| 12 | + description: 'You can either get codefresh repos (previously added) or any repo from your git context (using option "--available" and "--context")', |
| 13 | + webDocs: { |
| 14 | + category: 'Repository', |
| 15 | + title: 'Get Repositories', |
| 16 | + weight: 20, |
| 17 | + }, |
| 18 | + builder: (yargs) => { |
| 19 | + yargs |
| 20 | + .positional('names', { |
| 21 | + describe: 'Names for filtering repos', |
| 22 | + }) |
| 23 | + .option('available', { |
| 24 | + describe: 'Get all available git repos from provided or default git context', |
| 25 | + alias: 'a', |
| 26 | + }) |
| 27 | + .option('limit', { |
| 28 | + describe: 'Maximum displayed repos number', |
| 29 | + alias: 'l', |
| 30 | + default: 25, |
| 31 | + }) |
| 32 | + .option('context', { |
| 33 | + describe: 'Name of the git context to use, if not passed the default will be used', |
| 34 | + alias: 'c', |
| 35 | + }) |
| 36 | + .example('codefresh get repo', 'Get all codefresh repos') |
| 37 | + .example('codefresh get repo codefresh-io', 'Get codefresh repos containing "codefresh-io" in its name') |
| 38 | + .example('codefresh get repo some-repo', 'Get codefresh repos containing "some-repo" in its name') |
| 39 | + .example('codefresh get repo -a', 'Get all available repos from default git context') |
| 40 | + .example('codefresh get repo -a -c bitbucket', 'Get all available repos from "bitbucket" git context'); |
| 41 | + return yargs; |
| 42 | + }, |
| 43 | + handler: async (argv) => { |
| 44 | + const { context, names, available, limit } = argv; |
| 45 | + |
| 46 | + const loadRepos = available ? repository.getAllAvailableGitRepos : repository.getAll; |
| 47 | + const contextText = context ? `git context "${context}"` : 'default user git context'; |
| 48 | + const filterProperty = available ? 'info.repo_shortcut' : 'info.serviceName'; |
| 49 | + |
| 50 | + const spinner = Spinner(`Loading ${available ? `git repos for ${contextText}` : 'codefresh'}`).start(); |
| 51 | + try { |
| 52 | + let repos = await loadRepos(context); |
| 53 | + spinner.succeed('Successfully loaded repos!'); |
| 54 | + |
| 55 | + |
| 56 | + if (!_.isEmpty(names)) { |
| 57 | + repos = repos.filter((r) => { |
| 58 | + return names.reduce((bool, name) => bool || _.get(r, filterProperty).includes(name), false); |
| 59 | + }); |
| 60 | + } |
| 61 | + specifyOutputForArray(argv.output, repos.slice(0, limit), argv.pretty); |
| 62 | + |
| 63 | + const lengthDiff = repos.length - limit; |
| 64 | + if (lengthDiff > 0) { |
| 65 | + console.log(`... ${lengthDiff} more repos available - pass greater --limit option to show more`); |
| 66 | + } |
| 67 | + } catch (e) { |
| 68 | + spinner.fail('Failed to load repositories:'); |
| 69 | + console.log(` - ${e.message}`); |
| 70 | + } |
| 71 | + }, |
| 72 | +}); |
| 73 | + |
| 74 | +module.exports = command; |
| 75 | + |
0 commit comments