Skip to content
Snippets Groups Projects
Commit 5575e4b6 authored by michael.minelli's avatar michael.minelli
Browse files

Commander => Add gitlab register & test accesses

parent 3a614190
No related branches found
No related tags found
No related merge requests found
Pipeline #25093 passed
import { Command } from 'commander'; import { Command } from 'commander';
import prompt from 'prompt';
import colors from '@colors/colors/safe';
import SessionManager from '../managers/SessionManager'; import SessionManager from '../managers/SessionManager';
import axios from 'axios';
import HttpManager from '../managers/HttpManager';
import logger from '../shared/logging/WinstonLogger';
import Config from '../Config/Config'; import Config from '../Config/Config';
import GitlabManager from '../managers/GitlabManager';
import chalk from 'chalk';
import inquirer from 'inquirer';
class CommanderApp { class CommanderApp {
...@@ -29,7 +27,8 @@ class CommanderApp { ...@@ -29,7 +27,8 @@ class CommanderApp {
}); });
this.loginCommand(); this.loginCommand();
this.testSessionCommand(); this.gitlabRegisterCommand();
this.testAccessesCommand();
this.program.parse(); this.program.parse();
} }
...@@ -40,38 +39,42 @@ class CommanderApp { ...@@ -40,38 +39,42 @@ class CommanderApp {
.requiredOption('-u, --user <string>', '[required] username to use when connecting to server.') .requiredOption('-u, --user <string>', '[required] username to use when connecting to server.')
.option('-p, --password <string>', 'password to use when connecting to server. If password is not given it\'s asked.') .option('-p, --password <string>', 'password to use when connecting to server. If password is not given it\'s asked.')
.action(async (options) => { .action(async (options) => {
const passwordPromise = new Promise((resolve, reject) => {
if ( !options.password ) { if ( !options.password ) {
prompt.get({ options.password = (await inquirer.prompt({
properties: { type : 'password',
password: { name : 'password',
description: colors.cyan('Please enter your password'), message: 'Please enter your password',
required : true, mask : ''
hidden : true })).password;
} }
}
}, (err, result) => { console.log(chalk.cyan('Please wait while we are logging in you to Dojo...'));
options.password = result.password;
resolve(undefined); await SessionManager.login(options.user, options.password);
}); });
} else {
resolve(undefined);
} }
});
await passwordPromise; gitlabRegisterCommand() {
this.program.command('gitlab_register')
.description('Register the gitlab token')
.argument('<token>', 'Personal access token from GitLab with api scope.')
.action(async (token) => {
console.log(chalk.cyan('Please wait while we are testing your Gitlab token...'));
SessionManager.login(options.user, options.password); GitlabManager.token = token;
await GitlabManager.testToken();
}); });
} }
testSessionCommand() { testAccessesCommand() {
this.program.command('test-session') this.program.command('test_accesses')
.description('Test availability of session') .description('Test availability of registered Dojo API and Gitlab API sessions')
.action(async (options) => { .action(async _ => {
const response = await axios.get(HttpManager.TEST_SESSION_URL); console.log(chalk.cyan('Please wait while we are testing your Dojo accesses...'));
await SessionManager.testSession();
logger.info('Session is valid'); console.log(chalk.cyan('Please wait while we are testing your Gitlab accesses...'));
await GitlabManager.testToken();
}); });
} }
} }
......
...@@ -3,8 +3,8 @@ import User from '../models/User'; ...@@ -3,8 +3,8 @@ import User from '../models/User';
import LocalConfig from '../Config/LocalConfig/LocalConfig'; import LocalConfig from '../Config/LocalConfig/LocalConfig';
import LocalConfigKeys from '../Config/LocalConfig/LocalConfigKeys'; import LocalConfigKeys from '../Config/LocalConfig/LocalConfigKeys';
import axios, { AxiosError } from 'axios'; import axios, { AxiosError } from 'axios';
import logger from '../shared/logging/WinstonLogger';
import HttpManager from './HttpManager'; import HttpManager from './HttpManager';
import ora from 'ora';
class SessionManager { class SessionManager {
...@@ -45,6 +45,7 @@ class SessionManager { ...@@ -45,6 +45,7 @@ class SessionManager {
} }
async login(user: string, password: string) { async login(user: string, password: string) {
const spinner: ora.Ora = ora('Logging in').start();
try { try {
const response = await axios.post(HttpManager.LOGIN_URL, { const response = await axios.post(HttpManager.LOGIN_URL, {
user : user, user : user,
...@@ -55,21 +56,48 @@ class SessionManager { ...@@ -55,21 +56,48 @@ class SessionManager {
} }
}); });
logger.info('Login successful'); spinner.succeed('Logged in');
} catch ( error ) { } catch ( error ) {
if ( error instanceof AxiosError ) { if ( error instanceof AxiosError ) {
if ( error.response ) { if ( error.response ) {
if ( error.response.status === 404 ) { if ( error.response.status === 404 ) {
logger.error(`User not found or password incorrect`); spinner.fail('User not found or password incorrect');
} else { } else {
logger.error(`Login error: ${ error.response.statusText }`); spinner.fail(`Login error: ${ error.response.statusText }`);
} }
} }
} else { } else {
logger.error(`Login error: ${ error }`); spinner.fail(`Login error: ${ error }`);
} }
} }
} }
async testSession(verbose: boolean = true): Promise<boolean> {
HttpManager.handleCommandErrors = false;
let result: boolean = false;
const spinner: ora.Ora = ora('Testing Dojo session');
if ( verbose ) {
spinner.start();
}
try {
const response = await axios.get(HttpManager.TEST_SESSION_URL, {});
result = true;
if ( verbose ) {
spinner.succeed('The session is valid');
}
} catch ( error ) {
if ( verbose ) {
spinner.succeed('The session is invalid');
}
}
return true;
}
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment