Skip to content

Commit 89cb322

Browse files
olegz-codefreshpasha-codefresh
authored andcommitted
fix - Validate command exit code (#368)
* [bugfix] - Fixed status code on 'validate' command [other] - small refactoring * [other] - small refactoring * wip * just another commit * just another commit * update pipelines * wip * just another commit * [bugfix] - now process not exiting with --attach options * [other] - added validateCmd test for not valid yaml * bump version
1 parent 95799a4 commit 89cb322

File tree

8 files changed

+59
-23
lines changed

8 files changed

+59
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ Options:
5454
## Documentation
5555
For more information please visit the official <a href="http://cli.codefresh.io" target="_blank">CLI documentation</a> site.
5656

57+

codefresh-release.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ version: '1.0'
33

44
steps:
55

6+
main_clone:
7+
title: 'Cloning main repository...'
8+
type: git-clone
9+
repo: codefresh-io/cli
10+
revision: ${{CF_BRANCH}}
11+
git: cf_github
12+
613
fail_if_not_master:
714
title: "Validate running on master branch"
815
image: codefresh/build-cli

codefresh.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ version: "1.0"
33

44
steps:
55

6+
main_clone:
7+
title: 'Cloning main repository...'
8+
type: git-clone
9+
repo: codefresh-io/cli
10+
revision: ${{CF_BRANCH}}
11+
git: cf_github
12+
613
install_dependencies:
714
title: 'Installing testing dependencies'
815
image: codefresh/node-tester-image:8.8.0

lib/interface/cli/commad-line-interface.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ const PROCESS_ARGV = require('yargs-parser')(process.argv);
1515
async function startCommandLine() {
1616
const cliConfig = configManager.config();
1717

18-
let files;
19-
let config;
20-
2118
const configOptions = {
2219
configPath: PROCESS_ARGV.cfconfig,
2320
spec: { json: openapi },
@@ -30,13 +27,9 @@ async function startCommandLine() {
3027
}, cliConfig.request),
3128
};
3229

33-
await Promise.all([
34-
recursive(path.resolve(__dirname, 'commands')).then((result) => {
35-
files = result;
36-
}),
37-
Config.load(configOptions).then((result) => {
38-
config = result;
39-
}),
30+
const [files, config] = await Promise.all([
31+
recursive(path.resolve(__dirname, 'commands')),
32+
Config.load(configOptions),
4033
]);
4134

4235
sdk.configure(config);

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ const command = new Command({
2929
return yargs;
3030
},
3131
handler: async (argv) => {
32-
if (!argv.name) {
33-
throw new Error('Repo name must be provided');
34-
}
32+
if (!argv.name) throw new Error('Repo name must be provided');
3533

3634
const data = {
3735
name: argv.name,
@@ -43,5 +41,6 @@ const command = new Command({
4341
},
4442
});
4543

44+
4645
module.exports = command;
4746

lib/interface/cli/commands/root/root.sdk.spec.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,28 @@ let deleteCmd = require('./delete.cmd');
44
let versionCmd = require('./version.cmd');
55
let approveCmd = require('../workflow/approve.cmd');
66
let denyCmd = require('../workflow/approve.cmd');
7+
let validateCmd = require('./validate.cmd');
78

89
createCmd.requiresAuthentication = false;
910
replaceCmd.requiresAuthentication = false;
1011
deleteCmd.requiresAuthentication = false;
1112
versionCmd.requiresAuthentication = false;
1213
approveCmd.requiresAuthentication = false;
1314
denyCmd.requiresAuthentication = false;
15+
validateCmd.requiresAuthentication = false;
1416

1517
createCmd = createCmd.toCommand();
1618
replaceCmd = replaceCmd.toCommand();
1719
deleteCmd = deleteCmd.toCommand();
1820
versionCmd = versionCmd.toCommand();
1921
approveCmd = approveCmd.toCommand();
2022
denyCmd = denyCmd.toCommand();
21-
23+
validateCmd = validateCmd.toCommand();
2224

2325
jest.mock('../../helpers/validation', () => { // eslint-disable-line
2426
return {
2527
validatePipelineSpec: () => ({ valid: true }),
28+
validatePipelineYaml: () => ({ valid: false }),
2629
};
2730
});
2831

@@ -133,6 +136,19 @@ describe('root commands', () => {
133136
});
134137
});
135138

139+
describe('validate', () => {
140+
describe('Not valid yaml ', async () => {
141+
it('should throw error for not valid file', async () => {
142+
const argv = {
143+
filenames: ['./codefresh.yml'],
144+
};
145+
const result = await validateCmd.handler(argv)
146+
.catch(err => err);
147+
expect(result instanceof Error).toBe(true);
148+
});
149+
});
150+
});
151+
136152
describe('version', () => {
137153
describe('api', async () => {
138154
it('should handle getting version', async () => {

lib/interface/cli/commands/root/validate.cmd.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,21 @@ const { pathExists, watchFile } = require('../../helpers/general');
77

88
const VALID_MESSAGE = Style.green('Yaml is valid!');
99

10+
function _getResultMessage(result = {}) {
11+
return result.valid
12+
? VALID_MESSAGE
13+
: `${Style.red(result.message)}\n`;
14+
}
15+
1016
function printResult(result) {
11-
console.log(result.valid ? VALID_MESSAGE : Style.red(result.message), '\n');
17+
console.log(_getResultMessage(result));
18+
}
19+
20+
function _handleResult(result = {}, attach) {
21+
if (result.valid || attach) {
22+
return printResult(result);
23+
}
24+
throw Error(_getResultMessage(result));
1225
}
1326

1427
const validateCmd = new Command({
@@ -41,8 +54,7 @@ const validateCmd = new Command({
4154
filenames = filenames.map(f => path.resolve(process.cwd(), f));
4255

4356
if (_.isEmpty(filenames)) {
44-
console.log('No filename provided!');
45-
return;
57+
return console.log('No filename provided!');
4658
}
4759

4860
const checkPromises = filenames.map((filename) => {
@@ -66,10 +78,11 @@ const validateCmd = new Command({
6678
return;
6779
}
6880

69-
filenames.forEach(f => validatePipelineYaml(f).then((result) => {
70-
console.log(`Validation result for ${f}:`);
71-
printResult(result);
72-
}));
81+
filenames.forEach(f => validatePipelineYaml(f)
82+
.then((result) => {
83+
console.log(`Validation result for ${f}:`);
84+
printResult(result);
85+
}));
7386
return;
7487
}
7588

@@ -85,7 +98,7 @@ const validateCmd = new Command({
8598

8699
// even with --attach option validates file for first time
87100
const result = await validatePipelineYaml(filename);
88-
printResult(result);
101+
_handleResult(result, attach);
89102
},
90103
});
91104

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.36.0",
3+
"version": "0.36.2",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)