From 88d10ca5d2084f94f70c51664b210156139fb8da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <michael@minelli.me> Date: Fri, 28 Jul 2023 17:17:04 +0200 Subject: [PATCH] Transfert some config to clients shared module --- NodeApp/src/commander/CommanderApp.ts | 14 +++++++------- NodeApp/src/config/Config.ts | 17 ----------------- NodeApp/src/managers/DojoBackendManager.ts | 8 ++++---- NodeApp/src/managers/GitlabManager.ts | 16 ++++++++-------- NodeApp/src/managers/HttpManager.ts | 8 ++++---- NodeApp/src/sharedByClients | 2 +- 6 files changed, 24 insertions(+), 41 deletions(-) diff --git a/NodeApp/src/commander/CommanderApp.ts b/NodeApp/src/commander/CommanderApp.ts index 8fad32a..d7558e9 100644 --- a/NodeApp/src/commander/CommanderApp.ts +++ b/NodeApp/src/commander/CommanderApp.ts @@ -1,8 +1,8 @@ -import { Command } from 'commander'; -import Config from '../config/Config'; -import EnonceCommand from './enonce/EnonceCommand'; -import SessionCommand from './session/SessionCommand'; -import ExerciceCommand from './exercice/ExerciceCommand'; +import { Command } from 'commander'; +import EnonceCommand from './enonce/EnonceCommand'; +import SessionCommand from './session/SessionCommand'; +import ExerciceCommand from './exercice/ExerciceCommand'; +import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; class CommanderApp { @@ -19,10 +19,10 @@ class CommanderApp { sortOptions : true, sortSubcommands : true }) - .option('-H, --host <string>', 'override the Dojo API endpoint', Config.apiURL); + .option('-H, --host <string>', 'override the Dojo API endpoint', ClientsSharedConfig.apiURL); this.program.on('option:host', () => { - Config.apiURL = this.program.opts().host; + ClientsSharedConfig.apiURL = this.program.opts().host; }); this.registerCommands(); diff --git a/NodeApp/src/config/Config.ts b/NodeApp/src/config/Config.ts index f65322a..f19ec8d 100644 --- a/NodeApp/src/config/Config.ts +++ b/NodeApp/src/config/Config.ts @@ -2,28 +2,11 @@ import getAppDataPath from 'appdata-path'; class Config { - public apiURL: string; - - public gitlab: { - apiURL: string - dojoAccount: { id: number; username: string; }; - }; - public readonly localConfig: { folder: string; file: string; }; constructor() { - this.apiURL = process.env.API_URL || ''; - - this.gitlab = { - apiURL : process.env.GITLAB_API_URL || '', - dojoAccount: { - id : Number(process.env.GITLAB_DOJO_ACCOUNT_ID) || -1, - username: process.env.GITLAB_DOJO_ACCOUNT_USERNAME || '' - } - }; - this.localConfig = { folder: getAppDataPath('DojoCLI'), file : process.env.LOCAL_CONFIG_FILE || '' diff --git a/NodeApp/src/managers/DojoBackendManager.ts b/NodeApp/src/managers/DojoBackendManager.ts index ce05f43..4a538e1 100644 --- a/NodeApp/src/managers/DojoBackendManager.ts +++ b/NodeApp/src/managers/DojoBackendManager.ts @@ -1,5 +1,4 @@ import axios, { AxiosError } from 'axios'; -import Config from '../config/Config'; import ora from 'ora'; import ApiRoutes from '../sharedByClients/types/ApiRoutes'; import { StatusCodes } from 'http-status-codes'; @@ -7,11 +6,12 @@ import Enonce from '../sharedByClients/models/Enonce'; import GitlabUser from '../shared/types/Gitlab/GitlabUser'; import Exercice from '../sharedByClients/models/Exercice'; import DojoResponse from '../shared/types/DojoResponse'; +import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; class DojoBackendManager { public getApiUrl(route: ApiRoutes): string { - return `${ Config.apiURL }${ route }`; + return `${ ClientsSharedConfig.apiURL }${ route }`; } public async getEnonce(nameOrUrl: string): Promise<Enonce | undefined> { @@ -43,9 +43,9 @@ class DojoBackendManager { if ( error instanceof AxiosError ) { if ( error.response ) { if ( error.response.status === StatusCodes.NOT_FOUND ) { - spinner.fail(`Template not found or access denied. Please check the template ID or url. Also, please check that the template have public/internal visibility or that your and Dojo account (${ Config.gitlab.dojoAccount.username }) have at least reporter role to the template (if private).`); + spinner.fail(`Template not found or access denied. Please check the template ID or url. Also, please check that the template have public/internal visibility or that your and Dojo account (${ ClientsSharedConfig.gitlab.dojoAccount.username }) have at least reporter role to the template (if private).`); } else if ( error.response.status === StatusCodes.UNAUTHORIZED ) { - spinner.fail(`Please check that the template have public/internal visibility or that your and Dojo account (${ Config.gitlab.dojoAccount.username }) have at least reporter role to the template (if private).`); + spinner.fail(`Please check that the template have public/internal visibility or that your and Dojo account (${ ClientsSharedConfig.gitlab.dojoAccount.username }) have at least reporter role to the template (if private).`); } else { spinner.fail(`Template error: ${ error.response.statusText }`); } diff --git a/NodeApp/src/managers/GitlabManager.ts b/NodeApp/src/managers/GitlabManager.ts index b2d71c7..9f00a2e 100644 --- a/NodeApp/src/managers/GitlabManager.ts +++ b/NodeApp/src/managers/GitlabManager.ts @@ -1,17 +1,17 @@ -import LocalConfig from '../config/LocalConfig'; -import LocalConfigKeys from '../types/LocalConfigKeys'; -import axios from 'axios'; -import Config from '../config/Config'; -import ora from 'ora'; -import GitlabUser from '../shared/types/Gitlab/GitlabUser'; -import GitlabRoutes from '../shared/types/Gitlab/GitlabRoutes'; +import LocalConfig from '../config/LocalConfig'; +import LocalConfigKeys from '../types/LocalConfigKeys'; +import axios from 'axios'; +import ora from 'ora'; +import GitlabUser from '../shared/types/Gitlab/GitlabUser'; +import GitlabRoutes from '../shared/types/Gitlab/GitlabRoutes'; +import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; class GitlabManager { private _token: string | null = null; private getApiUrl(route: GitlabRoutes): string { - return `${ Config.gitlab.apiURL }${ route }`; + return `${ ClientsSharedConfig.gitlab.apiURL }${ route }`; } get isLogged(): boolean { diff --git a/NodeApp/src/managers/HttpManager.ts b/NodeApp/src/managers/HttpManager.ts index a8e0de6..10c8bfe 100644 --- a/NodeApp/src/managers/HttpManager.ts +++ b/NodeApp/src/managers/HttpManager.ts @@ -1,10 +1,10 @@ import axios, { AxiosRequestHeaders } from 'axios'; -import Config from '../config/Config'; import SessionManager from './SessionManager'; import FormData from 'form-data'; import logger from '../shared/logging/WinstonLogger'; import GitlabManager from './GitlabManager'; import { StatusCodes } from 'http-status-codes'; +import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; class HttpManager { @@ -21,7 +21,7 @@ class HttpManager { config.headers = { ...config.headers, ...(config.data as FormData).getHeaders() } as AxiosRequestHeaders; } - if ( config.url && (config.url.indexOf(Config.apiURL) !== -1) ) { + if ( config.url && (config.url.indexOf(ClientsSharedConfig.apiURL) !== -1) ) { if ( config.data && Object.keys(config.data).length > 0 ) { config.headers['Content-Type'] = 'multipart/form-data'; } @@ -31,7 +31,7 @@ class HttpManager { } } - if ( GitlabManager.isLogged && config.url && config.url.indexOf(Config.gitlab.apiURL) !== -1 ) { + if ( GitlabManager.isLogged && config.url && config.url.indexOf(ClientsSharedConfig.gitlab.apiURL) !== -1 ) { config.headers['PRIVATE-TOKEN'] = GitlabManager.token; } @@ -49,7 +49,7 @@ class HttpManager { }, (error) => { if ( error.response ) { if ( this.handleCommandErrors ) { - if ( error.response.url && error.response.url.indexOf(Config.apiURL) !== -1 ) { + if ( error.response.url && error.response.url.indexOf(ClientsSharedConfig.apiURL) !== -1 ) { switch ( error.response.status ) { case StatusCodes.UNAUTHORIZED: // Unauthorized logger.error('Session expired or inexistent. Please login again.'); diff --git a/NodeApp/src/sharedByClients b/NodeApp/src/sharedByClients index 3cd4b0c..8fe8e94 160000 --- a/NodeApp/src/sharedByClients +++ b/NodeApp/src/sharedByClients @@ -1 +1 @@ -Subproject commit 3cd4b0c0e18fb8e8f52062cf4b171c8c67d4baea +Subproject commit 8fe8e9417a527cf2182a9acc440e68b99024487e -- GitLab