diff --git a/src/api.ts b/src/api.ts index a8f31357..a9e0c5c9 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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...`); diff --git a/src/cli.ts b/src/cli.ts index e2826d4d..2910f443 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -13,11 +13,18 @@ * this program. If not, see . */ 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 ', '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 [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); - });