Skip to content

Commit 581e626

Browse files
Saas 1281 output layer redone (#264)
* output layer redone * output refactored * dates redone * remove "get" helper * fix accidentally removed values * remove draftlog from output * remove gray color from repos + repo context validation * update version
1 parent e27dc1c commit 581e626

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+430
-336
lines changed

lib/interface/cli/commands/board/get.cmd.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const debug = require('debug')('codefresh:cli:create:pipelines2');
22
const Command = require('../../Command');
33
const CFError = require('cf-errors');
44
const { board: boardLogic } = require('../../../../logic').api;
5-
const { specifyOutputForArray } = require('../../helpers/get');
5+
const Output = require('../../../../output/Output');
66

77
const getRoot = require('../root/get.cmd');
88

@@ -25,11 +25,11 @@ const command = new Command({
2525
});
2626
},
2727
handler: async (argv) => {
28-
const { id, name, output } = argv;
28+
const { id, name } = argv;
2929
if (id) {
3030
try {
3131
const board = await boardLogic.getBoardById(id);
32-
specifyOutputForArray(output, [board], argv.pretty);
32+
Output.print(board);
3333
} catch (err) {
3434
debug(err.toString());
3535
const message = `Board '${id}' was not found`;
@@ -41,7 +41,7 @@ const command = new Command({
4141
} else if (name) {
4242
try {
4343
const board = await boardLogic.getBoardByName(name);
44-
specifyOutputForArray(output, [board], argv.pretty);
44+
Output.print(board);
4545
} catch (err) {
4646
debug(err.toString());
4747
const message = `Board '${name}' was not found`;
@@ -51,7 +51,8 @@ const command = new Command({
5151
});
5252
}
5353
} else {
54-
specifyOutputForArray(output, await boardLogic.getAll({ name }), argv.pretty);
54+
const boards = await boardLogic.getAll({ name });
55+
Output.print(boards);
5556
}
5657
},
5758
});

lib/interface/cli/commands/cluster/get.cmd.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const debug = require('debug')('codefresh:cli:get:cluster');
22
const Command = require('../../Command');
33
const { cluster } = require('../../../../logic').api;
4-
const { specifyOutputForArray } = require('../../helpers/get');
4+
const Output = require('../../../../output/Output');
55
const getRoot = require('../root/get.cmd');
66

77

@@ -21,7 +21,7 @@ const command = new Command({
2121
},
2222
handler: async (argv) => {
2323
const clusters = await cluster.getAllClusters();
24-
specifyOutputForArray(argv.output, clusters, argv.pretty);
24+
Output.print(clusters);
2525
},
2626
});
2727

lib/interface/cli/commands/composition/get.cmd.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
const debug = require('debug')('codefresh:cli:create:context');
21
const Command = require('../../Command');
3-
const CFError = require('cf-errors');
42
const _ = require('lodash');
53
const { composition } = require('../../../../logic').api;
6-
const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers/get');
4+
const Output = require('../../../../output/Output');
75
const getRoot = require('../root/get.cmd');
8-
6+
const Promise = require('bluebird');
97

108
const command = new Command({
119
command: 'compositions [id|name..]',
@@ -31,14 +29,11 @@ const command = new Command({
3129
let compositions = [];
3230
// TODO:need to decide for one way for error handeling
3331
if (!_.isEmpty(compositionIds)) {
34-
for (const id of compositionIds) {
35-
const currComposition = await composition.getCompositionByIdentifier(id);
36-
compositions.push(currComposition);
37-
}
32+
compositions = await Promise.map(compositionIds, id => composition.getCompositionByIdentifier(id));
3833
} else {
3934
compositions = await composition.getCompositions();
4035
}
41-
specifyOutputForArray(argv.output, compositions, argv.pretty);
36+
Output.print(compositions);
4237
},
4338
});
4439

lib/interface/cli/commands/context/get.cmd.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
const debug = require('debug')('codefresh:cli:create:context');
21
const Command = require('../../Command');
3-
const CFError = require('cf-errors');
42
const _ = require('lodash');
5-
const { printError } = require('../../helpers/general');
63
const { context } = require('../../../../logic').api;
7-
const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers/get');
4+
const Output = require('../../../../output/Output');
85
const getRoot = require('../root/get.cmd');
9-
6+
const Promise = require('bluebird');
107

118
const command = new Command({
129
command: 'contexts [name..]',
@@ -47,14 +44,11 @@ const command = new Command({
4744

4845
let contexts = [];
4946
if (!_.isEmpty(names)) {
50-
for (const name of names) {
51-
const currContext = await context.getContextByName(name, data.decrypt);
52-
contexts.push(currContext);
53-
}
47+
contexts = await Promise.map(names, name => context.getContextByName(name, data.decrypt));
5448
} else {
5549
contexts = await context.getContexts(data);
5650
}
57-
specifyOutputForArray(argv.output, contexts, argv.pretty);
51+
Output.print(contexts);
5852
},
5953
});
6054

lib/interface/cli/commands/environment/get.cmd.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
const debug = require('debug')('codefresh:cli:create:context');
21
const Command = require('../../Command');
3-
const CFError = require('cf-errors');
42
const _ = require('lodash');
53
const { environment } = require('../../../../logic').api;
6-
const { specifyOutputForSingle, specifyOutputForArray } = require('../../helpers/get');
4+
const Output = require('../../../../output/Output');
75
const getRoot = require('../root/get.cmd');
8-
6+
const Promise = require('bluebird');
97

108
const command = new Command({
119
command: 'environments [id..]',
@@ -32,14 +30,11 @@ const command = new Command({
3230
let environments = [];
3331
// TODO:need to decide for one way for error handeling
3432
if (!_.isEmpty(environmentIds)) {
35-
for (const id of environmentIds) {
36-
const currEnvironment = await environment.getEnvironmentById(id);
37-
environments.push(currEnvironment);
38-
}
33+
environments = await Promise.map(environments, id => environment.getEnvironmentById(id));
3934
} else {
4035
environments = await environment.getEnvironments();
4136
}
42-
specifyOutputForArray(argv.output, environments, argv.pretty);
37+
Output.print(environments);
4338
},
4439
});
4540

lib/interface/cli/commands/helm/repo/apply.cmd.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
const debug = require('debug')('codefresh:cli:create:pipelines2');
21
const Command = require('../../../Command');
3-
const CFError = require('cf-errors');
4-
const _ = require('lodash');
52
const { helm } = require('../../../../../logic').api;
6-
const { printError } = require('../../../helpers/general');
7-
const { specifyOutputForArray } = require('../../../helpers/get');
83

94
const applyRoot = require('../../root/apply.cmd');
105

lib/interface/cli/commands/helm/repo/get.cmd.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const Command = require('../../../Command');
33
const CFError = require('cf-errors');
44
const _ = require('lodash');
55
const { helm } = require('../../../../../logic').api;
6-
const { printError } = require('../../../helpers/general');
7-
const { specifyOutputForArray } = require('../../../helpers/get');
6+
const Output = require('../../../../../output/Output');
87

98
const getRoot = require('../../root/get.cmd');
109

@@ -28,7 +27,7 @@ const command = new Command({
2827
return yargs;
2928
},
3029
handler: async (argv) => {
31-
const { name: names, output } = argv;
30+
const { name: names } = argv;
3231

3332
if (!_.isEmpty(names)) {
3433
const repos = [];
@@ -38,7 +37,7 @@ const command = new Command({
3837
repos.push(currRepo);
3938
} catch (err) {
4039
if (repos.length) {
41-
specifyOutputForArray(output, repos, argv.pretty);
40+
Output.print(repos);
4241
}
4342

4443
debug(err.toString());
@@ -49,9 +48,10 @@ const command = new Command({
4948
});
5049
}
5150
}
52-
specifyOutputForArray(output, repos, argv.pretty);
51+
Output.print(repos);
5352
} else {
54-
specifyOutputForArray(output, await helm.getAllRepos(), argv.pretty);
53+
const repos = await helm.getAllRepos();
54+
Output.print(repos);
5555
}
5656
},
5757
});

lib/interface/cli/commands/image/get.cmd.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
const debug = require('debug')('codefresh:cli:create:context');
21
const Command = require('../../Command');
3-
const CFError = require('cf-errors');
42
const _ = require('lodash');
53
const DEFAULTS = require('../../defaults');
64
const { prepareKeyValueFromCLIEnvOption } = require('../../helpers/general');
75
const { image } = require('../../../../logic').api;
8-
const { specifyOutputForArray } = require('../../helpers/get');
6+
const Output = require('../../../../output/Output');
97
const getRoot = require('../root/get.cmd');
8+
const Promise = require('bluebird');
109

1110
const command = new Command({
1211
command: 'images [id..]',
@@ -83,15 +82,11 @@ const command = new Command({
8382
let images = [];
8483
// TODO:need to decide for one way for error handeling
8584
if (!_.isEmpty(imageIds)) {
86-
for (const id of imageIds) {
87-
const currImage = await image.getImageById({
88-
id,
89-
allRegistries,
90-
});
91-
_.forEach(currImage, (img) => {
92-
images.push(img);
93-
});
94-
}
85+
images = await Promise.map(imageIds, id => image.getImageById({
86+
id,
87+
allRegistries,
88+
}));
89+
images = _.flatten(images);
9590
} else {
9691
images = await image.getAll({
9792
labels,
@@ -105,7 +100,7 @@ const command = new Command({
105100
offset,
106101
});
107102
}
108-
specifyOutputForArray(argv.output, images, argv.pretty);
103+
Output.print(images);
109104
},
110105
});
111106

lib/interface/cli/commands/pipeline/get.cmd.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const _ = require('lodash');
55
const DEFAULTS = require('../../defaults');
66
const { pipeline } = require('../../../../logic').api;
77
const { prepareKeyValueFromCLIEnvOption, printError } = require('../../helpers/general');
8-
const { specifyOutputForArray } = require('../../helpers/get');
8+
const Output = require('../../../../output/Output');
99

1010
const getRoot = require('../root/get.cmd');
1111

@@ -46,7 +46,7 @@ const command = new Command({
4646
});
4747
},
4848
handler: async (argv) => {
49-
const { id: ids, name, output, d: decryptVariables, pretty} = argv;
49+
const { id: ids, name, d: decryptVariables} = argv;
5050
const limit = argv.limit;
5151
const offset = (argv.page - 1) * limit;
5252
const labels = prepareKeyValueFromCLIEnvOption(argv.label);
@@ -60,7 +60,7 @@ const command = new Command({
6060
pipelines.push(currPipeline);
6161
} catch (err) {
6262
if (pipelines.length) {
63-
specifyOutputForArray(output, pipelines, pretty);
63+
Output.print(pipelines);
6464
}
6565

6666
debug(err.toString());
@@ -71,14 +71,15 @@ const command = new Command({
7171
});
7272
}
7373
}
74-
specifyOutputForArray(output, pipelines, pretty);
74+
Output.print(pipelines);
7575
} else {
76-
specifyOutputForArray(output, await pipeline.getAll({
76+
const pipelines = await pipeline.getAll({
7777
limit,
7878
offset,
7979
name,
8080
labels,
81-
}), pretty);
81+
});
82+
Output.print(pipelines);
8283
}
8384
},
8485
});

lib/interface/cli/commands/repo/get.cmd.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const Command = require('../../Command');
22
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');
55
const getRoot = require('../root/get.cmd');
66
const Spinner = require('ora');
7+
const CFError = require('cf-errors'); // eslint-disable-line
78

89
const command = new Command({
910
command: 'repository [names..]',
@@ -41,32 +42,52 @@ const command = new Command({
4142
return yargs;
4243
},
4344
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+
}
4562

4663
const loadRepos = available ? repository.getAllAvailableGitRepos : repository.getAll;
47-
const contextText = context ? `git context "${context}"` : 'default user git context';
4864
const filterProperty = available ? 'info.repo_shortcut' : 'info.serviceName';
4965

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+
}
5170
try {
5271
let repos = await loadRepos(context);
53-
spinner.succeed('Successfully loaded repos!');
54-
72+
spinner.stop();
5573

5674
if (!_.isEmpty(names)) {
5775
repos = repos.filter((r) => {
5876
return names.reduce((bool, name) => bool || _.get(r, filterProperty).includes(name), false);
5977
});
6078
}
61-
specifyOutputForArray(argv.output, repos.slice(0, limit), argv.pretty);
79+
Output.print(repos.slice(0, limit));
6280

6381
const lengthDiff = repos.length - limit;
6482
if (lengthDiff > 0) {
6583
console.log(`... ${lengthDiff} more repos available - pass greater --limit option to show more`);
6684
}
6785
} 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+
});
7091
}
7192
},
7293
});

0 commit comments

Comments
 (0)