|
1 | 1 | const Command = require('../../Command');
|
2 | 2 | const _ = require('lodash');
|
3 |
| -const { repository } = require('../../../../logic').api; |
4 |
| -const { specifyOutputForArray } = require('../../helpers/get'); |
| 3 | +const { repository, context: Context } = require('../../../../logic').api; |
| 4 | +const Output = require('../../../../output/Output'); |
5 | 5 | const getRoot = require('../root/get.cmd');
|
6 | 6 | const Spinner = require('ora');
|
| 7 | +const CFError = require('cf-errors'); // eslint-disable-line |
7 | 8 |
|
8 | 9 | const command = new Command({
|
9 | 10 | command: 'repository [names..]',
|
@@ -41,32 +42,52 @@ const command = new Command({
|
41 | 42 | return yargs;
|
42 | 43 | },
|
43 | 44 | handler: async (argv) => {
|
44 |
| - const { context, names, available, limit } = argv; |
| 45 | + let { context, names, available, limit } = argv; |
| 46 | + |
| 47 | + let ctxList = await Context.getContexts({}); |
| 48 | + ctxList = ctxList.filter(c => c.type.startsWith('git') |
| 49 | + && ((context && c.name === context) || (!context && c.info.metadata.default))); |
| 50 | + |
| 51 | + if (_.isEmpty(ctxList)) { |
| 52 | + throw new CFError(context ? `No such context: ${context}` : 'Default git context is not specified'); |
| 53 | + } |
| 54 | + |
| 55 | + let contextText; |
| 56 | + if (!context) { |
| 57 | + context = ctxList[0].name; |
| 58 | + contextText = `default user git context: "${context}"`; |
| 59 | + } else { |
| 60 | + contextText = `git context: "${context}"`; |
| 61 | + } |
45 | 62 |
|
46 | 63 | const loadRepos = available ? repository.getAllAvailableGitRepos : repository.getAll;
|
47 |
| - const contextText = context ? `git context "${context}"` : 'default user git context'; |
48 | 64 | const filterProperty = available ? 'info.repo_shortcut' : 'info.serviceName';
|
49 | 65 |
|
50 |
| - const spinner = Spinner(`Loading ${available ? `git repos for ${contextText}` : 'codefresh'}`).start(); |
| 66 | + const spinner = Spinner(`Loading git repos for ${contextText}`); |
| 67 | + if (available) { |
| 68 | + spinner.start(); |
| 69 | + } |
51 | 70 | try {
|
52 | 71 | let repos = await loadRepos(context);
|
53 |
| - spinner.succeed('Successfully loaded repos!'); |
54 |
| - |
| 72 | + spinner.stop(); |
55 | 73 |
|
56 | 74 | if (!_.isEmpty(names)) {
|
57 | 75 | repos = repos.filter((r) => {
|
58 | 76 | return names.reduce((bool, name) => bool || _.get(r, filterProperty).includes(name), false);
|
59 | 77 | });
|
60 | 78 | }
|
61 |
| - specifyOutputForArray(argv.output, repos.slice(0, limit), argv.pretty); |
| 79 | + Output.print(repos.slice(0, limit)); |
62 | 80 |
|
63 | 81 | const lengthDiff = repos.length - limit;
|
64 | 82 | if (lengthDiff > 0) {
|
65 | 83 | console.log(`... ${lengthDiff} more repos available - pass greater --limit option to show more`);
|
66 | 84 | }
|
67 | 85 | } catch (e) {
|
68 |
| - spinner.fail('Failed to load repositories:'); |
69 |
| - console.log(` - ${e.message}`); |
| 86 | + spinner.stop(); |
| 87 | + throw new CFError({ |
| 88 | + message: 'Failed to load repositories', |
| 89 | + cause: e, |
| 90 | + }); |
70 | 91 | }
|
71 | 92 | },
|
72 | 93 | });
|
|
0 commit comments