Files
openstapps/src/cli.ts
2019-11-19 12:06:54 +01:00

69 lines
2.1 KiB
TypeScript

/*
* Copyright (C) 2018 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Logger} from '@openstapps/logger';
import * as commander from 'commander';
import {readFileSync} from 'fs';
import {join} from 'path';
import {Api, ApiRequestOptions} from './api';
const pkgJson = JSON.parse(readFileSync(join(__dirname, '..', 'package.json'))
.toString());
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');
commander
.parse(process.argv);
if (typeof commander.call === 'undefined') {
commander.outputHelp();
process.exit(1);
}
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);
});