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

Commander => Add possibility to override the API endpoint

parent 9da5fcc2
Branches
Tags
No related merge requests found
Pipeline #25054 passed
import * as os from 'os';
import HttpManager from '../managers/HttpManager';
class Config {
private static _instance: Config;
public readonly apiURL: string;
private _apiURL!: string;
public readonly localConfig: {
folder: string; file: string;
......@@ -19,6 +20,16 @@ class Config {
};
}
get apiURL(): string {
return this._apiURL;
}
set apiURL(url: string) {
this._apiURL = url;
HttpManager.API_BASE_URL = this._apiURL;
}
public static get instance(): Config {
if ( !Config._instance ) {
Config._instance = new Config();
......
......@@ -5,6 +5,7 @@ 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';
class CommanderApp {
......@@ -12,9 +13,20 @@ class CommanderApp {
constructor() {
this.program
.name('Dojo CLI')
.name('dojo_cli')
.description('CLI for the Dojo application')
.version('1.0.0Ɑ');
.version('1.0.0Ɑ')
.showHelpAfterError()
.configureHelp({
showGlobalOptions: true,
sortOptions : true,
sortSubcommands : true
})
.option('-H, --host <string>', 'override the Dojo API endpoint.', Config.apiURL);
this.program.on('option:host', () => {
Config.apiURL = this.program.opts().host;
});
this.loginCommand();
this.testSessionCommand();
......@@ -25,8 +37,8 @@ class CommanderApp {
loginCommand() {
this.program.command('login')
.description('Login into the application')
.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.')
.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.')
.action(async (options) => {
const passwordPromise = new Promise((resolve, reject) => {
if ( !options.password ) {
......
......@@ -6,10 +6,9 @@ import logger from '../shared/logging/WinstonLogger';
class HttpManager {
private readonly API_BASE_URL: string = Config.apiURL;
public readonly LOGIN_URL: string = `${ this.API_BASE_URL }/login`;
public readonly TEST_SESSION_URL: string = `${ this.API_BASE_URL }/test_session`;
private _API_BASE_URL!: string;
public LOGIN_URL!: string;
public TEST_SESSION_URL!: string;
private static _instance: HttpManager;
......@@ -21,6 +20,17 @@ class HttpManager {
return HttpManager._instance;
}
private constructor() {
}
set API_BASE_URL(url: string) {
this._API_BASE_URL = url;
this.LOGIN_URL = `${ this._API_BASE_URL }/login`;
this.TEST_SESSION_URL = `${ this._API_BASE_URL }/test_session`;
}
registerAxiosInterceptor() {
this.registerRequestInterceptor();
this.registerResponseInterceptor();
......@@ -49,6 +59,7 @@ class HttpManager {
return response;
}, (error) => {
if ( error.response ) {
switch ( error.response.status ) {
case 401: // Unauthorized
logger.error('Session expired or inexistent. Please login again.');
......@@ -59,6 +70,11 @@ class HttpManager {
process.exit(1);
break;
}
} else {
logger.error('Error connecting to the server.');
process.exit(1);
}
return Promise.reject(error);
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment