import yargs = require('yargs'); import * as chalk from 'chalk'; import { Auth } from '../app/auth'; import { log } from '../app/log'; import { out } from '../app/cli_utils'; var cmd = 'validate'; var cmdArgs = '[v]'; export var command = `${cmd} ${cmdArgs}`; export var desc = 'Validates authorisation credentials that are registered'; //export var builder = {}; //export function builder(args:yargs.Argv){} export function builder( args: yargs.Argv ) { return args .usage(` ${desc} ${chalk.bold('Usage:')}: ${cmd} ${cmdArgs}`) .options({ a: {alias:'all',desc:'Test all registered credentials'} }) } export var testers: any = {}; export function handler( argv: any ) { Auth.all().forEach(( auth: Auth ) => { if ( !testers[auth.remote.toString()] ) { return log.warn('Tester not implemented for ' + auth.remote); } testers[auth.remote.toString()](auth); }) } var msg = ( auth: Auth, str: string, color: string ) => out(chalk[color](str) + chalk[color].bold(auth.name) + chalk[color](' to remote ') + chalk[color].bold(auth.remote.toString())); var starting = ( auth: Auth ) => msg(auth, 'Starting test for ', 'cyan') var success = ( auth: Auth ) => msg(auth, 'Success for ', 'green'); var failed = ( auth: Auth ) => msg(auth, 'Failed for ', 'red'); testers.bitbucket = function ( auth: Auth ) { starting(auth) auth.getConnection().get('user').then(( data: any ) => { if ( data.type && data.type === 'user' ) { return success(auth) } failed(auth); }) } testers.github = function ( auth: Auth ) { starting(auth) auth.getConnection().get('user').then(( data: any ) => { if ( data.type && data.type === 'User' ) { return success(auth) } failed(auth); }) } testers.bitbucket_server = function ( auth: Auth ) { starting(auth) auth.getConnection().get('projects').then(( data: any ) => { if ( typeof data.limit === 'number' ) { return success(auth) } failed(auth); }) } testers.jenkins = function ( auth: Auth ) { starting(auth) auth.getConnection().get('').then(( data: any ) => { if ( typeof data.mode === 'string' ) { return success(auth) } failed(auth); }) } testers.jira = function ( auth: Auth ) { starting(auth) auth.getConnection().get('myself').then(( data: any ) => { if ( typeof data.active === 'boolean' && data.active === true ) { return success(auth) } failed(auth); }) }