mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-09 19:22:51 +00:00
refactor: correctly use commands and options of commander
This commit is contained in:
@@ -514,7 +514,7 @@ export class Api {
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
if (error.error.includes('not responding') || _options.retryOnAnyError) {
|
||||
if (error.error.message.includes('not responding') || _options.retryOnAnyError) {
|
||||
const seconds = 5;
|
||||
|
||||
Logger.warn(`GitLab was not responding. Waiting ${seconds}s and retrying...`);
|
||||
|
||||
68
src/cli.ts
68
src/cli.ts
@@ -13,11 +13,18 @@
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {Logger} from '@openstapps/logger';
|
||||
import {AddLogLevel} from '@openstapps/logger/lib/transformations/add-log-level';
|
||||
import {Colorize} from '@openstapps/logger/lib/transformations/colorize';
|
||||
import * as commander from 'commander';
|
||||
import {readFileSync} from 'fs';
|
||||
import {join} from 'path';
|
||||
import {Api, ApiRequestOptions} from './api';
|
||||
|
||||
Logger.setTransformations([
|
||||
new AddLogLevel(),
|
||||
new Colorize(),
|
||||
]);
|
||||
|
||||
const pkgJson = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'))
|
||||
.toString());
|
||||
|
||||
@@ -25,44 +32,33 @@ commander
|
||||
.version(pkgJson.version);
|
||||
|
||||
commander
|
||||
.option('-u, --url [url]', 'GitLab API URL', 'https://gitlab.com/api/v4/')
|
||||
.option('-c, --call <call>', 'API call/sub url')
|
||||
.option('-m, --method [method]', 'HTTP verb to use for the API call', 'GET')
|
||||
.option('-d, --data [data]', 'JSON representation of data to send');
|
||||
.option('-t, --token [token]', 'GitLab API token', process.env.GITLAB_PRIVATE_TOKEN)
|
||||
.option('-u, --url [url]', 'GitLab API URL', 'https://gitlab.com/api/v4/');
|
||||
|
||||
commander
|
||||
.command('request <call> [method] [data]')
|
||||
.action(async (call, method, data) => {
|
||||
const options: ApiRequestOptions = {};
|
||||
|
||||
if (method !== 'GET') {
|
||||
options.method = method;
|
||||
}
|
||||
|
||||
if (typeof data !== 'undefined') {
|
||||
options.data = JSON.parse(data);
|
||||
}
|
||||
|
||||
const gitLabApi = new Api(commander.url, commander.token);
|
||||
|
||||
const result = await gitLabApi.makeGitLabAPIRequest(call, options);
|
||||
|
||||
// tslint:disable-next-line:no-console
|
||||
console.log(result);
|
||||
});
|
||||
|
||||
commander
|
||||
.parse(process.argv);
|
||||
|
||||
if (typeof commander.call === 'undefined') {
|
||||
commander.outputHelp();
|
||||
process.exit(1);
|
||||
if (typeof commander.token !== 'string' || commander.token.length === 0) {
|
||||
Logger.warn('You probably want to supply a GitLab token either via option or environment variable (GITLAB_PRIVATE_TOKEN).');
|
||||
}
|
||||
|
||||
if (typeof commander.token === 'undefined') {
|
||||
if (typeof process.env.GITLAB_PRIVATE_TOKEN === 'undefined') {
|
||||
Logger.warn('You probably want to supply a GitLab token either via option or environment variable (GITLAB_PRIVATE_TOKEN).');
|
||||
}
|
||||
|
||||
commander.token = process.env.GITLAB_PRIVATE_TOKEN;
|
||||
}
|
||||
|
||||
const gitLabApi = new Api(commander.url, commander.token);
|
||||
|
||||
const options: ApiRequestOptions = {};
|
||||
|
||||
if (commander.method !== 'GET') {
|
||||
options.method = commander.method;
|
||||
}
|
||||
|
||||
if (typeof commander.data !== 'undefined') {
|
||||
options.data = JSON.parse(commander.data);
|
||||
}
|
||||
|
||||
gitLabApi.makeGitLabAPIRequest(commander.call, options)
|
||||
.then((result) => {
|
||||
Logger.ok(result);
|
||||
process.exit(1);
|
||||
}, async (err) => {
|
||||
await Logger.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user