-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
115 lines (98 loc) · 3.54 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'use strict';
const commander = require('commander');
const JFLint = require('../index');
const pkg = require('../package.json');
const fs = require('fs');
const os = require('os');
const path = require('path');
class CommandLine {
/**
* Constructor
* @param {!NodeJS.Process} process Process
*/
constructor(process) {
this._process = process;
this._program = new commander.Command();
}
/**
* Execute a command
*/
execute() {
this._program
.version(pkg.version, '-v, --version')
.usage('[options] Jenkinsfile')
.option('-j, --jenkins-url <url>', 'Jenkins URL')
.option('-u, --username <username>', 'Username for Jenkins')
.option('-p, --password <password>', 'Password or API Token for Jenkins')
.option('--csrf-disabled', 'Specify if CSRF prevention is disabled on Jenkins')
.option('-c, --config <path>', 'Path to config json file')
.option('--ssl-verification-disabled', 'Disable SSL verification');
this._program.parse(this._process.argv);
if (this._program.args.length !== 1) {
this._program.outputHelp();
this._process.exit(1);
}
const jenkinsfile = this._program.args[0];
let config = {};
const configPath = this._program.config || this._findConfigPath(this._process.cwd());
try {
config = JSON.parse(fs.readFileSync(configPath));
} catch (e) {
if (this._program.config) {
// only throw when config path is specified by an option
this._process.stderr.write(`Error! Can't read config ${configPath}\n`);
this._process.exit(1);
}
}
const jenkinsUrl = this._program.jenkinsUrl || config.jenkinsUrl;
const username = this._program.username || config.username;
const password = this._program.password || config.password;
const csrfDisabled = this._program.csrfDisabled || config.csrfDisabled;
const sslVerificationDisabled = this._program.sslVerificationDisabled || config.sslVerificationDisabled;
if(sslVerificationDisabled) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
}
if (!jenkinsUrl) {
this._program.outputHelp();
this._process.exit(1);
}
const jflint = new JFLint(jenkinsUrl, username, password, csrfDisabled);
return jflint.lintJenkinsfile(jenkinsfile).then(res => {
this._interpretResult(res);
})
.catch(e => {
this._process.stderr.write(`${e.message}\n`);
this._process.exit(1);
});
}
_findConfigPath(dir) {
const name = '.jflintrc';
const filename = path.normalize(path.join(dir, name));
if (this._isFileExist(filename)) {
return filename;
}
const parent = path.resolve(dir, '../');
if (dir === parent) {
return path.join(os.homedir(), '.jflintrc');
}
return this._findConfigPath(parent);
}
_isFileExist(path) {
try {
fs.statSync(path);
return true;
} catch (e) {
return false;
}
}
_interpretResult(res) {
if (/^Jenkinsfile successfully validated.\r?\n$/.test(res)) {
this._process.stdout.write(res);
this._process.exit(0);
} else {
this._process.stderr.write(res);
this._process.exit(1);
}
}
}
module.exports = CommandLine;