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 * as os from 'os';
import HttpManager from '../managers/HttpManager';
class Config { class Config {
private static _instance: Config; private static _instance: Config;
public readonly apiURL: string; private _apiURL!: string;
public readonly localConfig: { public readonly localConfig: {
folder: string; file: string; folder: string; file: string;
...@@ -19,6 +20,16 @@ class Config { ...@@ -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 { public static get instance(): Config {
if ( !Config._instance ) { if ( !Config._instance ) {
Config._instance = new Config(); Config._instance = new Config();
......
...@@ -5,6 +5,7 @@ import SessionManager from '../managers/SessionManager'; ...@@ -5,6 +5,7 @@ import SessionManager from '../managers/SessionManager';
import axios from 'axios'; import axios from 'axios';
import HttpManager from '../managers/HttpManager'; import HttpManager from '../managers/HttpManager';
import logger from '../shared/logging/WinstonLogger'; import logger from '../shared/logging/WinstonLogger';
import Config from '../Config/Config';
class CommanderApp { class CommanderApp {
...@@ -12,9 +13,20 @@ class CommanderApp { ...@@ -12,9 +13,20 @@ class CommanderApp {
constructor() { constructor() {
this.program this.program
.name('Dojo CLI') .name('dojo_cli')
.description('CLI for the Dojo application') .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.loginCommand();
this.testSessionCommand(); this.testSessionCommand();
...@@ -25,8 +37,8 @@ class CommanderApp { ...@@ -25,8 +37,8 @@ class CommanderApp {
loginCommand() { loginCommand() {
this.program.command('login') this.program.command('login')
.description('Login into the application') .description('Login into the application')
.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) => { const passwordPromise = new Promise((resolve, reject) => {
if ( !options.password ) { if ( !options.password ) {
......
...@@ -6,10 +6,9 @@ import logger from '../shared/logging/WinstonLogger'; ...@@ -6,10 +6,9 @@ import logger from '../shared/logging/WinstonLogger';
class HttpManager { class HttpManager {
private readonly API_BASE_URL: string = Config.apiURL; private _API_BASE_URL!: string;
public LOGIN_URL!: string;
public readonly LOGIN_URL: string = `${ this.API_BASE_URL }/login`; public TEST_SESSION_URL!: string;
public readonly TEST_SESSION_URL: string = `${ this.API_BASE_URL }/test_session`;
private static _instance: HttpManager; private static _instance: HttpManager;
...@@ -21,6 +20,17 @@ class HttpManager { ...@@ -21,6 +20,17 @@ class HttpManager {
return HttpManager._instance; 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() { registerAxiosInterceptor() {
this.registerRequestInterceptor(); this.registerRequestInterceptor();
this.registerResponseInterceptor(); this.registerResponseInterceptor();
...@@ -49,6 +59,7 @@ class HttpManager { ...@@ -49,6 +59,7 @@ class HttpManager {
return response; return response;
}, (error) => { }, (error) => {
if ( error.response ) {
switch ( error.response.status ) { switch ( error.response.status ) {
case 401: // Unauthorized case 401: // Unauthorized
logger.error('Session expired or inexistent. Please login again.'); logger.error('Session expired or inexistent. Please login again.');
...@@ -59,6 +70,11 @@ class HttpManager { ...@@ -59,6 +70,11 @@ class HttpManager {
process.exit(1); process.exit(1);
break; break;
} }
} else {
logger.error('Error connecting to the server.');
process.exit(1);
}
return Promise.reject(error); 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