From e828cb5b208c91213dfa225fc6886017659c9966 Mon Sep 17 00:00:00 2001 From: yaroslav-codefresh Date: Fri, 14 Dec 2018 15:45:11 +0200 Subject: [PATCH 1/4] add and get command --- lib/interface/cli/commands/repo/add.cmd.js | 77 +++++++++++++++++++++ lib/interface/cli/commands/repo/get.cmd.js | 69 +++++++++++++++++++ lib/interface/cli/commands/root/add.cmd.js | 25 +++++++ lib/logic/api/index.js | 2 + lib/logic/api/repository.js | 80 ++++++++++++++++++++++ lib/logic/entities/CodefreshRepo.js | 22 ++++++ lib/logic/entities/GitRepo.js | 21 ++++++ package.json | 1 + yarn.lock | 26 ++++++- 9 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 lib/interface/cli/commands/repo/add.cmd.js create mode 100644 lib/interface/cli/commands/repo/get.cmd.js create mode 100644 lib/interface/cli/commands/root/add.cmd.js create mode 100644 lib/logic/api/repository.js create mode 100644 lib/logic/entities/CodefreshRepo.js create mode 100644 lib/logic/entities/GitRepo.js diff --git a/lib/interface/cli/commands/repo/add.cmd.js b/lib/interface/cli/commands/repo/add.cmd.js new file mode 100644 index 000000000..0a4ba42cf --- /dev/null +++ b/lib/interface/cli/commands/repo/add.cmd.js @@ -0,0 +1,77 @@ +const debug = require('debug')('codefresh:cli:create:context'); +const Command = require('../../Command'); +const CFError = require('cf-errors'); +const _ = require('lodash'); +const { repository } = require('../../../../logic').api; +const addRoot = require('../root/add.cmd'); +const yargs = require('yargs'); + +const command = new Command({ + command: 'repository [fullname]', + aliases: ['repo'], + parent: addRoot, + description: 'Add a repository', + webDocs: { + category: 'Repository', + title: 'Add Repository', + }, + builder: (yargs) => { + yargs + .positional('fullname', { + describe: 'Full name of repo (template: "/")', + required: true, + }) + .option('context', { + describe: 'Name of the git context to use, if not passed the default will be used', + alias: 'c', + }); + return yargs; + }, + handler: async (argv) => { + let { context, fullname } = argv; + + if (!fullname) { + console.log('Full repo name is not provided'); + return; + } + + const [owner, repoName] = fullname.split('/'); + if (!owner) { + console.log('Owner name not provided!'); + console.log('Please follow the repo name template: "/"'); + return; + } + if (!repoName) { + console.log('Repo name not provided!'); + console.log('Please follow the repo name template: "/"'); + return; + } + + try { + // throws on not found + const gitRepo = await repository.getAvailableGitRepo(owner, repoName, context); + console.log(`Found repository "${gitRepo.name}" at context "${context || gitRepo.provider}"`); + if (!context) { + context = gitRepo.provider; + } + + // following the ui logic on duplication + const existing = await repository.getRepo(fullname, context).catch(() => null); // throws on not found + if (existing) { + const contextPrefix = `${context}-${context}`; + console.log(`Repo with such name already exists -- adding context prefix: ${contextPrefix}`); + fullname = `${contextPrefix}/${fullname}`; + } + + // throws on duplicate, not found, other errors... + await repository.createRepository(fullname, owner, repoName, context); + console.log(`Repository added: ${fullname}`); + } catch (e) { + console.log('Cannot add repository:'); + console.log(` - ${e.message.replace('service', 'repo')}`); + } + }, +}); + +module.exports = command; + diff --git a/lib/interface/cli/commands/repo/get.cmd.js b/lib/interface/cli/commands/repo/get.cmd.js new file mode 100644 index 000000000..31e875734 --- /dev/null +++ b/lib/interface/cli/commands/repo/get.cmd.js @@ -0,0 +1,69 @@ +const Command = require('../../Command'); +const _ = require('lodash'); +const { repository } = require('../../../../logic').api; +const { specifyOutputForArray } = require('../../helpers/get'); +const getRoot = require('../root/get.cmd'); +const Spinner = require('ora'); + +const command = new Command({ + command: 'repository [names..]', + aliases: ['repo'], + parent: getRoot, + description: 'Add a repository', + webDocs: { + category: 'Repository', + title: 'Add Repository', + }, + builder: (yargs) => { + yargs + .positional('names', { + describe: 'Names for filtering repos', + }) + .option('available', { + describe: 'Get all available git repos from provided or default git context', + alias: 'a', + }) + .option('limit', { + describe: 'Maximum displayed repos number', + alias: 'l', + default: 25, + }) + .option('context', { + describe: 'Name of the git context to use, if not passed the default will be used', + alias: 'c', + }); + return yargs; + }, + handler: async (argv) => { + const { context, names, available, limit } = argv; + + const loadRepos = available ? repository.getAllAvailableGitRepos : repository.getAllRepo; + const contextText = context ? `git context "${context}"` : 'default user git context'; + const filterProperty = available ? 'info.repo_shortcut' : 'info.serviceName'; + + const spinner = Spinner(`Loading ${available ? `git repos for ${contextText}` : 'codefresh'}`).start(); + try { + let repos = await loadRepos(context); + spinner.succeed('Successfully loaded repos!'); + + + if (!_.isEmpty(names)) { + repos = repos.filter((r) => { + return names.reduce((bool, name) => bool || _.get(r, filterProperty).includes(name), false); + }); + } + specifyOutputForArray(argv.output, repos.slice(0, limit), argv.pretty); + + const lengthDiff = repos.length - limit; + if (lengthDiff > 0) { + console.log(`... ${lengthDiff} more repos available - pass greater --limit option to show more`); + } + } catch (e) { + spinner.fail('Failed to load repositories:'); + console.log(` - ${e.message}`); + } + }, +}); + +module.exports = command; + diff --git a/lib/interface/cli/commands/root/add.cmd.js b/lib/interface/cli/commands/root/add.cmd.js new file mode 100644 index 000000000..450296395 --- /dev/null +++ b/lib/interface/cli/commands/root/add.cmd.js @@ -0,0 +1,25 @@ +const CFError = require('cf-errors'); +const Command = require('../../Command'); +const { crudFilenameOption } = require('../../helpers/general'); +const { context, pipeline } = require('../../../../logic').api; +const yargs = require('yargs'); +const { validatePipelineFile } = require('../../helpers/validation'); + +const add = new Command({ + root: true, + command: 'add', + description: 'Add a resource', + // usage: 'Supported resources: \n\t\'context\'\n\t\'pipeline\'', + webDocs: { + description: 'Add a resource', + category: 'Operate On Resources', + title: 'Add', + weight: 20, + }, + builder: (yargs) => { + return yargs + .demandCommand(1, 'You need at least one command before moving on'); + }, +}); + +module.exports = add; diff --git a/lib/logic/api/index.js b/lib/logic/api/index.js index 0ed222e8f..b6860b40a 100644 --- a/lib/logic/api/index.js +++ b/lib/logic/api/index.js @@ -14,6 +14,7 @@ const team = require('./team'); const board = require('./board'); const section = require('./section'); const cluster = require('./cluster'); +const repository = require('./repository'); module.exports = { user, @@ -32,4 +33,5 @@ module.exports = { board, section, cluster, + repository, }; diff --git a/lib/logic/api/repository.js b/lib/logic/api/repository.js new file mode 100644 index 000000000..33234bd3c --- /dev/null +++ b/lib/logic/api/repository.js @@ -0,0 +1,80 @@ +const { sendHttpRequest } = require('./helper'); +const GitRepo = require('../entities/GitRepo'); +const CodefreshRepo = require('../entities/CodefreshRepo'); + +const getRepo = async (name, context) => { + const userOptions = { + url: `/api/services/${encodeURIComponent(name)}`, + method: 'get', + qs: { context }, + }; + + return sendHttpRequest(userOptions); +}; + +const getAllRepo = async (context) => { + const userOptions = { + url: '/api/repos/existing', + method: 'get', + qs: { context, thin: '' }, + }; + + const response = await sendHttpRequest(userOptions); + return response.map(CodefreshRepo.fromResponse); +}; + +const createRepository = async (name, owner, repo, context) => { + const body = { + serviceDetails: { + name, + scm: { + name: repo, + owner: { + name: owner, + }, + }, + }, + }; + const userOptions = { + url: '/api/services/', + method: 'post', + qs: { context }, + body, + }; + + return sendHttpRequest(userOptions); +}; + +/** + * @throws if repo does not exist or user does not have the right permissions or if the context is not configured + * */ +const getAvailableGitRepo = async (owner, repo, context) => { + const userOptions = { + url: `/api/repos/${owner}/${repo}`, + method: 'get', + qs: { context }, + }; + + return sendHttpRequest(userOptions); +}; + + +const getAllAvailableGitRepos = async (context) => { + const userOptions = { + url: '/api/repos', + method: 'get', + qs: { context }, + }; + + const response = await sendHttpRequest(userOptions); + return response.map(GitRepo.fromResponse); +}; + + +module.exports = { + createRepository, + getAvailableGitRepo, + getRepo, + getAllRepo, + getAllAvailableGitRepos, +}; diff --git a/lib/logic/entities/CodefreshRepo.js b/lib/logic/entities/CodefreshRepo.js new file mode 100644 index 000000000..12e5eafae --- /dev/null +++ b/lib/logic/entities/CodefreshRepo.js @@ -0,0 +1,22 @@ +const Entity = require('./Entity'); + +class CodefreshRepo extends Entity { + constructor(data) { + super(); + this.entityType = 'codefresh-repo'; + this.info = data; + this.defaultColumns = ['git_context', 'owner', 'name', 'name_id']; + this.wideColumns = this.defaultColumns; + } + + static fromResponse(response) { + const data = Object.assign({}, response); + data.name_id = response.serviceName; + data.git_context = response.provider; + data.owner = response.owner.login; + data.repo_shortcut = `${data.owner}/${data.name}`; + return new CodefreshRepo(data); + } +} + +module.exports = CodefreshRepo; diff --git a/lib/logic/entities/GitRepo.js b/lib/logic/entities/GitRepo.js new file mode 100644 index 000000000..417a28745 --- /dev/null +++ b/lib/logic/entities/GitRepo.js @@ -0,0 +1,21 @@ +const Entity = require('./Entity'); + +class GitRepo extends Entity { + constructor(data) { + super(); + this.entityType = 'git-repo'; + this.info = data; + this.defaultColumns = ['git_context', 'owner', 'name', 'repo_shortcut']; + this.wideColumns = this.defaultColumns; + } + + static fromResponse(response) { + const data = Object.assign({}, response); + data.git_context = response.provider; + data.owner = response.owner.login; + data.repo_shortcut = `${data.owner}/${data.name}`; + return new GitRepo(data); + } +} + +module.exports = GitRepo; diff --git a/package.json b/package.json index b4f1aebad..193b17099 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "mkdirp": "^0.5.1", "moment": "^2.19.4", "mongodb": "^3.0.1", + "ora": "^3.0.0", "prettyjson": "^1.2.1", "recursive-readdir": "^2.2.1", "request": "^2.88.0", diff --git a/yarn.lock b/yarn.lock index fdcb5566a..f7fe5d9b1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -716,7 +716,7 @@ chalk@1.1.3, chalk@^1.0.0, chalk@^1.1.1, chalk@^1.1.3: strip-ansi "^3.0.0" supports-color "^2.0.0" -chalk@2.4.1: +chalk@2.4.1, chalk@^2.3.1: version "2.4.1" resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.1.tgz#18c49ab16a037b6eb0152cc83e3471338215b66e" integrity sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ== @@ -810,6 +810,11 @@ cli-progress@^1.6.1: dependencies: colors "^1.1.2" +cli-spinners@^1.1.0: + version "1.3.1" + resolved "https://registry.yarnpkg.com/cli-spinners/-/cli-spinners-1.3.1.tgz#002c1990912d0d59580c93bd36c056de99e4259a" + integrity sha512-1QL4544moEsDVH9T/l6Cemov/37iv1RtoKf7NJ04A60+4MREXNfx/QvavbH6QoGdsD4N4Mwy49cmaINR/o2mdg== + cli-width@^2.0.0: version "2.2.0" resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639" @@ -3259,6 +3264,13 @@ lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.3.0: version "4.17.5" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511" +log-symbols@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-2.2.0.tgz#5740e1c5d6f0dfda4ad9323b5332107ef6b4c40a" + integrity sha512-VeIAFslyIerEJLXHziedo2basKbMKtTw3vfn5IzG0XTjhAVEJyNHnL2p7vc+wBDSdQuUpNw3M2u6xb9QsAY5Eg== + dependencies: + chalk "^2.0.1" + longest@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" @@ -3692,6 +3704,18 @@ optionator@^0.8.1, optionator@^0.8.2: type-check "~0.3.2" wordwrap "~1.0.0" +ora@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/ora/-/ora-3.0.0.tgz#8179e3525b9aafd99242d63cc206fd64732741d0" + integrity sha512-LBS97LFe2RV6GJmXBi6OKcETKyklHNMV0xw7BtsVn2MlsgsydyZetSCbCANr+PFLmDyv4KV88nn0eCKza665Mg== + dependencies: + chalk "^2.3.1" + cli-cursor "^2.1.0" + cli-spinners "^1.1.0" + log-symbols "^2.2.0" + strip-ansi "^4.0.0" + wcwidth "^1.0.1" + os-homedir@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3" From 3a33f354b113ae1861cd7121e16e5483b4006ff5 Mon Sep 17 00:00:00 2001 From: yaroslav-codefresh Date: Mon, 17 Dec 2018 16:28:00 +0200 Subject: [PATCH 2/4] delete command + styles + comments --- lib/interface/cli/commands/repo/add.cmd.js | 12 ++--- lib/interface/cli/commands/repo/delete.cmd.js | 45 +++++++++++++++++++ lib/interface/cli/commands/repo/get.cmd.js | 6 +-- lib/interface/cli/commands/root/add.cmd.js | 11 +---- lib/logic/api/repository.js | 40 ++++++++++++++--- lib/logic/entities/CodefreshRepo.js | 2 +- lib/logic/entities/GitRepo.js | 2 +- .../formatters/CodefreshRepo.formatter.js | 8 ++++ lib/output/formatters/GitRepo.formatter.js | 8 ++++ lib/output/formatters/index.js | 2 + 10 files changed, 107 insertions(+), 29 deletions(-) create mode 100644 lib/interface/cli/commands/repo/delete.cmd.js create mode 100644 lib/output/formatters/CodefreshRepo.formatter.js create mode 100644 lib/output/formatters/GitRepo.formatter.js diff --git a/lib/interface/cli/commands/repo/add.cmd.js b/lib/interface/cli/commands/repo/add.cmd.js index 0a4ba42cf..0be77e2d1 100644 --- a/lib/interface/cli/commands/repo/add.cmd.js +++ b/lib/interface/cli/commands/repo/add.cmd.js @@ -1,16 +1,12 @@ -const debug = require('debug')('codefresh:cli:create:context'); const Command = require('../../Command'); -const CFError = require('cf-errors'); -const _ = require('lodash'); const { repository } = require('../../../../logic').api; const addRoot = require('../root/add.cmd'); -const yargs = require('yargs'); const command = new Command({ command: 'repository [fullname]', aliases: ['repo'], parent: addRoot, - description: 'Add a repository', + description: 'Add a repository from git provider ("context") to codefresh', webDocs: { category: 'Repository', title: 'Add Repository', @@ -56,15 +52,15 @@ const command = new Command({ } // following the ui logic on duplication - const existing = await repository.getRepo(fullname, context).catch(() => null); // throws on not found + const existing = await repository.get(fullname, context).catch(() => null); // throws on not found if (existing) { const contextPrefix = `${context}-${context}`; console.log(`Repo with such name already exists -- adding context prefix: ${contextPrefix}`); - fullname = `${contextPrefix}/${fullname}`; + fullname = `${contextPrefix}/${fullname}`; // aka github-github/codefresh-io/cf-api } // throws on duplicate, not found, other errors... - await repository.createRepository(fullname, owner, repoName, context); + await repository.create(fullname, owner, repoName, context); console.log(`Repository added: ${fullname}`); } catch (e) { console.log('Cannot add repository:'); diff --git a/lib/interface/cli/commands/repo/delete.cmd.js b/lib/interface/cli/commands/repo/delete.cmd.js new file mode 100644 index 000000000..ae39f1788 --- /dev/null +++ b/lib/interface/cli/commands/repo/delete.cmd.js @@ -0,0 +1,45 @@ +const Command = require('../../Command'); +const { repository } = require('../../../../logic').api; +const deleteRoot = require('../root/delete.cmd'); + +const command = new Command({ + command: 'repository [name_id]', + aliases: ['repo'], + parent: deleteRoot, + description: 'Remove repository by name_id', + webDocs: { + category: 'Repository', + title: 'Delete Repository', + }, + builder: (yargs) => { + yargs + .positional('name_id', { + describe: 'Repository "name_id" (can be retrieved with "get" command)', + required: true, + }) + .option('context', { + describe: 'Name of the git context to use, if not passed the default will be used', + alias: 'c', + }); + return yargs; + }, + handler: async (argv) => { + const { name_id: name, context } = argv; + + if (!name) { + console.log('Repository name_id not provided'); + return; + } + + try { + await repository.deleteRepo(name, context); + console.log(`Successfully deleted repo: ${name}`); + } catch (e) { + console.log('Failed to delete repo:'); + console.log(` - ${e.message}`); + } + }, +}); + +module.exports = command; + diff --git a/lib/interface/cli/commands/repo/get.cmd.js b/lib/interface/cli/commands/repo/get.cmd.js index 31e875734..34c54dc45 100644 --- a/lib/interface/cli/commands/repo/get.cmd.js +++ b/lib/interface/cli/commands/repo/get.cmd.js @@ -9,10 +9,10 @@ const command = new Command({ command: 'repository [names..]', aliases: ['repo'], parent: getRoot, - description: 'Add a repository', + description: 'Get repositories', webDocs: { category: 'Repository', - title: 'Add Repository', + title: 'Get Repositories', }, builder: (yargs) => { yargs @@ -37,7 +37,7 @@ const command = new Command({ handler: async (argv) => { const { context, names, available, limit } = argv; - const loadRepos = available ? repository.getAllAvailableGitRepos : repository.getAllRepo; + const loadRepos = available ? repository.getAllAvailableGitRepos : repository.getAll; const contextText = context ? `git context "${context}"` : 'default user git context'; const filterProperty = available ? 'info.repo_shortcut' : 'info.serviceName'; diff --git a/lib/interface/cli/commands/root/add.cmd.js b/lib/interface/cli/commands/root/add.cmd.js index 450296395..ffc93d1ff 100644 --- a/lib/interface/cli/commands/root/add.cmd.js +++ b/lib/interface/cli/commands/root/add.cmd.js @@ -1,20 +1,13 @@ -const CFError = require('cf-errors'); const Command = require('../../Command'); -const { crudFilenameOption } = require('../../helpers/general'); -const { context, pipeline } = require('../../../../logic').api; -const yargs = require('yargs'); -const { validatePipelineFile } = require('../../helpers/validation'); const add = new Command({ root: true, command: 'add', description: 'Add a resource', - // usage: 'Supported resources: \n\t\'context\'\n\t\'pipeline\'', + usage: 'codefresh add --help', webDocs: { - description: 'Add a resource', - category: 'Operate On Resources', title: 'Add', - weight: 20, + weight: 70, }, builder: (yargs) => { return yargs diff --git a/lib/logic/api/repository.js b/lib/logic/api/repository.js index 33234bd3c..e062cd980 100644 --- a/lib/logic/api/repository.js +++ b/lib/logic/api/repository.js @@ -2,7 +2,10 @@ const { sendHttpRequest } = require('./helper'); const GitRepo = require('../entities/GitRepo'); const CodefreshRepo = require('../entities/CodefreshRepo'); -const getRepo = async (name, context) => { +/** + * codefresh repo (aka "service") + * */ +const get = async (name, context) => { const userOptions = { url: `/api/services/${encodeURIComponent(name)}`, method: 'get', @@ -12,7 +15,22 @@ const getRepo = async (name, context) => { return sendHttpRequest(userOptions); }; -const getAllRepo = async (context) => { +/** + * codefresh repo (aka "service") + * */ +const deleteRepo = async (name, context) => { + const userOptions = { + url: `/api/services/${encodeURIComponent(name)}`, + method: 'delete', + qs: { context }, + }; + return sendHttpRequest(userOptions); +}; + +/** + * codefresh repo (aka "service") + * */ +const getAll = async (context) => { const userOptions = { url: '/api/repos/existing', method: 'get', @@ -23,7 +41,10 @@ const getAllRepo = async (context) => { return response.map(CodefreshRepo.fromResponse); }; -const createRepository = async (name, owner, repo, context) => { +/** + * codefresh repo (aka "service") + * */ +const create = async (name, owner, repo, context) => { const body = { serviceDetails: { name, @@ -46,6 +67,8 @@ const createRepository = async (name, owner, repo, context) => { }; /** + * get available repo from git provider (github, bitbucket...) + * * @throws if repo does not exist or user does not have the right permissions or if the context is not configured * */ const getAvailableGitRepo = async (owner, repo, context) => { @@ -58,7 +81,9 @@ const getAvailableGitRepo = async (owner, repo, context) => { return sendHttpRequest(userOptions); }; - +/** + * get all available repos from git provider (github, bitbucket...) + * */ const getAllAvailableGitRepos = async (context) => { const userOptions = { url: '/api/repos', @@ -72,9 +97,10 @@ const getAllAvailableGitRepos = async (context) => { module.exports = { - createRepository, + create, getAvailableGitRepo, - getRepo, - getAllRepo, + get, + getAll, getAllAvailableGitRepos, + deleteRepo, }; diff --git a/lib/logic/entities/CodefreshRepo.js b/lib/logic/entities/CodefreshRepo.js index 12e5eafae..c0368dc8f 100644 --- a/lib/logic/entities/CodefreshRepo.js +++ b/lib/logic/entities/CodefreshRepo.js @@ -6,7 +6,7 @@ class CodefreshRepo extends Entity { this.entityType = 'codefresh-repo'; this.info = data; this.defaultColumns = ['git_context', 'owner', 'name', 'name_id']; - this.wideColumns = this.defaultColumns; + this.wideColumns = this.defaultColumns.concat([]); } static fromResponse(response) { diff --git a/lib/logic/entities/GitRepo.js b/lib/logic/entities/GitRepo.js index 417a28745..e86860946 100644 --- a/lib/logic/entities/GitRepo.js +++ b/lib/logic/entities/GitRepo.js @@ -6,7 +6,7 @@ class GitRepo extends Entity { this.entityType = 'git-repo'; this.info = data; this.defaultColumns = ['git_context', 'owner', 'name', 'repo_shortcut']; - this.wideColumns = this.defaultColumns; + this.wideColumns = this.defaultColumns.concat(['private', 'ssh_url']); } static fromResponse(response) { diff --git a/lib/output/formatters/CodefreshRepo.formatter.js b/lib/output/formatters/CodefreshRepo.formatter.js new file mode 100644 index 000000000..acf452c11 --- /dev/null +++ b/lib/output/formatters/CodefreshRepo.formatter.js @@ -0,0 +1,8 @@ +const { Formatter, Style} = require('../../output'); + + +const FORMATTER = Formatter.build() + .style('git_context', Style.gray) + .style('name_id', Style.cyan); + +module.exports = FORMATTER; diff --git a/lib/output/formatters/GitRepo.formatter.js b/lib/output/formatters/GitRepo.formatter.js new file mode 100644 index 000000000..7e56d3193 --- /dev/null +++ b/lib/output/formatters/GitRepo.formatter.js @@ -0,0 +1,8 @@ +const { Formatter, Style} = require('../../output'); + + +const FORMATTER = Formatter.build() + .style('git_context', Style.gray) + .style('repo_shortcut', Style.cyan); + +module.exports = FORMATTER; diff --git a/lib/output/formatters/index.js b/lib/output/formatters/index.js index 4bc1cd5e8..c8a08df7e 100644 --- a/lib/output/formatters/index.js +++ b/lib/output/formatters/index.js @@ -15,6 +15,8 @@ const formatters = { TriggerEvent: require('./TriggerEvent.formatter'), Composition: require('./Composition.formatter'), Environment: require('./Environment.formatter'), + GitRepo: require('./GitRepo.formatter'), + CodefreshRepo: require('./CodefreshRepo.formatter'), }; /* eslint-enable */ From e2f896baa47c3f6eadd8d90cb3f7d67836ccd83d Mon Sep 17 00:00:00 2001 From: yaroslav-codefresh Date: Mon, 17 Dec 2018 17:02:29 +0200 Subject: [PATCH 3/4] docs updated + pull request refactoring --- lib/interface/cli/commands/repo/add.cmd.js | 18 +++++++++++++----- lib/interface/cli/commands/repo/delete.cmd.js | 7 +++++-- lib/interface/cli/commands/repo/get.cmd.js | 10 ++++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/interface/cli/commands/repo/add.cmd.js b/lib/interface/cli/commands/repo/add.cmd.js index 0be77e2d1..165ad4537 100644 --- a/lib/interface/cli/commands/repo/add.cmd.js +++ b/lib/interface/cli/commands/repo/add.cmd.js @@ -6,21 +6,24 @@ const command = new Command({ command: 'repository [fullname]', aliases: ['repo'], parent: addRoot, - description: 'Add a repository from git provider ("context") to codefresh', + description: 'Add a repository from git context to codefresh', webDocs: { category: 'Repository', title: 'Add Repository', + weight: 10, }, builder: (yargs) => { yargs .positional('fullname', { - describe: 'Full name of repo (template: "/")', + describe: 'Full name of repo (template: "owner/repo")', required: true, }) .option('context', { describe: 'Name of the git context to use, if not passed the default will be used', alias: 'c', - }); + }) + .example('codefresh add repo codefresh-io/some-repo', 'Add repo "some-repo" of "codefresh-io" owner from default git context') + .example('codefresh add repo codefresh-io/some-repo -c bitbucket', 'Add repo "some-repo" of "codefresh-io" owner from git context "bitbucket"'); return yargs; }, handler: async (argv) => { @@ -52,11 +55,16 @@ const command = new Command({ } // following the ui logic on duplication - const existing = await repository.get(fullname, context).catch(() => null); // throws on not found - if (existing) { + try { + await repository.get(fullname, context); const contextPrefix = `${context}-${context}`; console.log(`Repo with such name already exists -- adding context prefix: ${contextPrefix}`); fullname = `${contextPrefix}/${fullname}`; // aka github-github/codefresh-io/cf-api + } catch (e) { + // if response is "not found" - all is ok, else re-throw + if (!e.statusCode || e.statusCode !== 404) { + throw e; + } } // throws on duplicate, not found, other errors... diff --git a/lib/interface/cli/commands/repo/delete.cmd.js b/lib/interface/cli/commands/repo/delete.cmd.js index ae39f1788..dc4ac1abe 100644 --- a/lib/interface/cli/commands/repo/delete.cmd.js +++ b/lib/interface/cli/commands/repo/delete.cmd.js @@ -10,17 +10,20 @@ const command = new Command({ webDocs: { category: 'Repository', title: 'Delete Repository', + description: 'Remove repository by name_id ("name_id" can be retrieved with "get" command, typically "repo_owner/repo_name")', + weight: 30, }, builder: (yargs) => { yargs .positional('name_id', { - describe: 'Repository "name_id" (can be retrieved with "get" command)', + describe: 'Repository "name_id" (can be retrieved with "get" command, typically "repo_owner/repo_name")', required: true, }) .option('context', { describe: 'Name of the git context to use, if not passed the default will be used', alias: 'c', - }); + }) + .example('codefresh delete repo codefresh-io/some-repo', 'Delete codefresh repo with name_id "codefresh-io/some-repo"'); return yargs; }, handler: async (argv) => { diff --git a/lib/interface/cli/commands/repo/get.cmd.js b/lib/interface/cli/commands/repo/get.cmd.js index 34c54dc45..8b4020c52 100644 --- a/lib/interface/cli/commands/repo/get.cmd.js +++ b/lib/interface/cli/commands/repo/get.cmd.js @@ -9,10 +9,11 @@ const command = new Command({ command: 'repository [names..]', aliases: ['repo'], parent: getRoot, - description: 'Get repositories', + description: 'You can either get codefresh repos (previously added) or any repo from your git context (using option "--available" and "--context")', webDocs: { category: 'Repository', title: 'Get Repositories', + weight: 20, }, builder: (yargs) => { yargs @@ -31,7 +32,12 @@ const command = new Command({ .option('context', { describe: 'Name of the git context to use, if not passed the default will be used', alias: 'c', - }); + }) + .example('codefresh get repo', 'Get all codefresh repos') + .example('codefresh get repo codefresh-io', 'Get codefresh repos containing "codefresh-io" in its name') + .example('codefresh get repo some-repo', 'Get codefresh repos containing "some-repo" in its name') + .example('codefresh get repo -a', 'Get all available repos from default git context') + .example('codefresh get repo -a -c bitbucket', 'Get all available repos from "bitbucket" git context'); return yargs; }, handler: async (argv) => { From 8eef2d06f5bd294da0a34478cefb1a2849f8e2d2 Mon Sep 17 00:00:00 2001 From: yaroslav-codefresh Date: Mon, 17 Dec 2018 17:05:52 +0200 Subject: [PATCH 4/4] version updated --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 193b17099..9678a2cc2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codefresh", - "version": "0.9.1", + "version": "0.9.6", "description": "Codefresh command line utility", "main": "index.js", "preferGlobal": true,