From 9c174a2fefc811ee7a1bfa5b6311c6d97e5a8891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Minelli?= <git@minelli.swiss> Date: Mon, 7 Oct 2024 18:05:48 +0200 Subject: [PATCH] Adapt to new config location --- NodeApp/src/commander/CommanderApp.ts | 15 +++++----- NodeApp/src/commander/UpgradeCommand.ts | 4 +-- .../subcommands/AssignmentCreateCommand.ts | 6 ++-- .../auth/subcommands/AuthTestCommand.ts | 4 +-- .../subcommands/ExerciseCorrectionCommand.ts | 2 +- .../subcommands/ExerciseCreateCommand.ts | 6 ++-- NodeApp/src/helpers/AccessesHelper.ts | 4 +-- NodeApp/src/helpers/Dojo/ExerciseRunHelper.ts | 2 +- NodeApp/src/helpers/GlobalHelper.ts | 2 +- NodeApp/src/managers/GitlabManager.ts | 6 ++-- NodeApp/src/managers/HttpManager.ts | 14 ++++------ NodeApp/src/managers/SessionManager.ts | 28 +++++++++---------- 12 files changed, 45 insertions(+), 48 deletions(-) diff --git a/NodeApp/src/commander/CommanderApp.ts b/NodeApp/src/commander/CommanderApp.ts index 30e9cfe..0ac70a8 100644 --- a/NodeApp/src/commander/CommanderApp.ts +++ b/NodeApp/src/commander/CommanderApp.ts @@ -1,19 +1,20 @@ import { Command, Option } from 'commander'; -import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig.js'; import AssignmentCommand from './assignment/AssignmentCommand.js'; import ExerciseCommand from './exercise/ExerciseCommand.js'; import SharedConfig from '../shared/config/SharedConfig.js'; import boxen from 'boxen'; -import { stateConfigFile } from '../config/ConfigFiles.js'; import semver from 'semver/preload.js'; import { version } from '../config/Version.js'; -import Config from '../config/Config.js'; import CompletionCommand from './completion/CompletionCommand.js'; import AuthCommand from './auth/AuthCommand.js'; import SessionCommand from './auth/SessionCommand.js'; import UpgradeCommand from './UpgradeCommand.js'; import TextStyle from '../types/TextStyle.js'; import TagCommand from './tag/TagCommand'; +import ConfigFiles from '../config/ConfigFiles'; +import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; +import Config from '../config/Config'; +import SettingsCommand from './settings/SettingsCommand'; class CommanderApp { @@ -33,7 +34,7 @@ class CommanderApp { } } - constructor() { + public async init() { this.program .name('dojo') .description('CLI of the Dojo application') @@ -95,8 +96,8 @@ ${ TextStyle.CODE(' dojo upgrade ') }`, { private informNewVersion() { if ( SharedConfig.production ) { - const latestDojoCliVersion = stateConfigFile.getParam('latestDojoCliVersion') as string | null || '0.0.0'; - const latestDojoCliVersionNotification = stateConfigFile.getParam('latestDojoCliVersionNotification') as number | null || 0; + const latestDojoCliVersion = ConfigFiles.stateConfigFile.getParam('latestDojoCliVersion') as string | null || '0.0.0'; + const latestDojoCliVersionNotification = ConfigFiles.stateConfigFile.getParam('latestDojoCliVersionNotification') as number | null || 0; if ( semver.lt(version as string, latestDojoCliVersion) && (new Date()).getTime() - latestDojoCliVersionNotification >= Config.versionUpdateInformationPeriodHours * 60 * 60 * 1000 ) { console.log(boxen(`The ${ latestDojoCliVersion } version of the DojoCLI is available. You can upgrade the DojoCLI by executing this command: @@ -109,7 +110,7 @@ ${ TextStyle.CODE(' dojo upgrade ') }`, { padding : 1, textAlignment : 'left' })); - stateConfigFile.setParam('latestDojoCliVersionNotification', (new Date()).getTime()); + ConfigFiles.stateConfigFile.setParam('latestDojoCliVersionNotification', (new Date()).getTime()); } } } diff --git a/NodeApp/src/commander/UpgradeCommand.ts b/NodeApp/src/commander/UpgradeCommand.ts index 18a96ab..653fdc8 100644 --- a/NodeApp/src/commander/UpgradeCommand.ts +++ b/NodeApp/src/commander/UpgradeCommand.ts @@ -1,9 +1,9 @@ import CommanderCommand from './CommanderCommand.js'; import ora from 'ora'; -import Config from '../config/Config.js'; import TextStyle from '../types/TextStyle.js'; import os from 'os'; import { spawn } from 'child_process'; +import Config from '../config/Config'; class UpgradeCommand extends CommanderCommand { @@ -18,7 +18,7 @@ class UpgradeCommand extends CommanderCommand { private displayInstructions(upgradeCommand: string, preAlpha: boolean) { upgradeCommand = TextStyle.CODE(` ${ upgradeCommand } `); - + console.log(TextStyle.BLOCK(`You can upgrade DojoCLI on ${ os.platform() === 'darwin' ? 'macOS' : 'Linux' }:`)); ora().start().info(`By executing this command: ${ upgradeCommand }`); console.log('or'); diff --git a/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts b/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts index 86a5bc5..d5cab19 100644 --- a/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts +++ b/NodeApp/src/commander/assignment/subcommands/AssignmentCreateCommand.ts @@ -6,7 +6,7 @@ import DojoBackendManager from '../../../managers/DojoBackendManager.js'; import Toolbox from '../../../shared/helpers/Toolbox.js'; import * as Gitlab from '@gitbeaker/rest'; import TextStyle from '../../../types/TextStyle.js'; -import GitlabManager from '../../../managers/GitlabManager.js'; +import Config from '../../../config/Config'; type CommandOptions = { name: string, template?: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean } @@ -35,7 +35,7 @@ class AssignmentCreateCommand extends CommanderCommand { await AccessesHelper.checkTeachingStaff(); - this.members = await GitlabManager.fetchMembers(options); + this.members = await Config.gitlabManager.fetchMembers(options); if ( !this.members ) { throw new Error(); } @@ -82,7 +82,7 @@ class AssignmentCreateCommand extends CommanderCommand { if ( options.clone ) { console.log(TextStyle.BLOCK('Please wait while we are cloning the repository...')); - await GitlabManager.cloneRepository(options.clone, this.assignment.gitlabCreationInfo.ssh_url_to_repo, undefined, true, 0); + await Config.gitlabManager.cloneRepository(options.clone, this.assignment.gitlabCreationInfo.ssh_url_to_repo, undefined, true, 0); } } diff --git a/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts b/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts index e2e9d67..d75bc62 100644 --- a/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts +++ b/NodeApp/src/commander/auth/subcommands/AuthTestCommand.ts @@ -1,6 +1,6 @@ import CommanderCommand from '../../CommanderCommand.js'; import SessionManager from '../../../managers/SessionManager.js'; -import GitlabManager from '../../../managers/GitlabManager.js'; +import Config from '../../../config/Config'; class AuthTestCommand extends CommanderCommand { @@ -14,7 +14,7 @@ class AuthTestCommand extends CommanderCommand { protected async commandAction(): Promise<void> { await SessionManager.testSession(); - await GitlabManager.testToken(); + await Config.gitlabManager.testToken(); } } diff --git a/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts b/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts index 650d150..dd3d066 100644 --- a/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts +++ b/NodeApp/src/commander/exercise/subcommands/ExerciseCorrectionCommand.ts @@ -1,12 +1,12 @@ import CommanderCommand from '../../CommanderCommand.js'; import ora from 'ora'; import DojoBackendManager from '../../../managers/DojoBackendManager.js'; -import Config from '../../../config/Config.js'; import Assignment from '../../../sharedByClients/models/Assignment.js'; import inquirer from 'inquirer'; import open from 'open'; import chalk from 'chalk'; import TextStyle from '../../../types/TextStyle.js'; +import Config from '../../../config/Config'; type CorrectionResume = { name: string, value: string } diff --git a/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts b/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts index 3db1510..a942775 100644 --- a/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts +++ b/NodeApp/src/commander/exercise/subcommands/ExerciseCreateCommand.ts @@ -6,8 +6,8 @@ import Assignment from '../../../sharedByClients/models/Assignment.js'; import Exercise from '../../../sharedByClients/models/Exercise.js'; import * as Gitlab from '@gitbeaker/rest'; import TextStyle from '../../../types/TextStyle.js'; -import GitlabManager from '../../../managers/GitlabManager.js'; import inquirer from 'inquirer'; +import Config from '../../../config/Config'; type CommandOptions = { assignment: string, members_id?: Array<number>, members_username?: Array<string>, clone?: string | boolean } @@ -35,7 +35,7 @@ class ExerciseCreateCommand extends CommanderCommand { await AccessesHelper.checkStudent(); - this.members = await GitlabManager.fetchMembers(options); + this.members = await Config.gitlabManager.fetchMembers(options); if ( !this.members ) { throw new Error(); } @@ -116,7 +116,7 @@ class ExerciseCreateCommand extends CommanderCommand { if ( options.clone ) { console.log(TextStyle.BLOCK('Please wait while we are cloning the repository...')); - await GitlabManager.cloneRepository(options.clone, this.exercise.gitlabCreationInfo.ssh_url_to_repo, `DojoExercise_${ this.exercise.assignmentName }`, true, 0); + await Config.gitlabManager.cloneRepository(options.clone, this.exercise.gitlabCreationInfo.ssh_url_to_repo, `DojoExercise_${ this.exercise.assignmentName }`, true, 0); } } diff --git a/NodeApp/src/helpers/AccessesHelper.ts b/NodeApp/src/helpers/AccessesHelper.ts index a07b7aa..cc5f2a4 100644 --- a/NodeApp/src/helpers/AccessesHelper.ts +++ b/NodeApp/src/helpers/AccessesHelper.ts @@ -1,5 +1,5 @@ import SessionManager from '../managers/SessionManager.js'; -import GitlabManager from '../managers/GitlabManager.js'; +import Config from '../config/Config'; class AccessesHelper { @@ -10,7 +10,7 @@ class AccessesHelper { throw new Error(); } - if ( testGitlab && !(await GitlabManager.testToken(true)).every(result => result) ) { + if ( testGitlab && !(await Config.gitlabManager.testToken(true)).every(result => result) ) { throw new Error(); } } diff --git a/NodeApp/src/helpers/Dojo/ExerciseRunHelper.ts b/NodeApp/src/helpers/Dojo/ExerciseRunHelper.ts index 235bc74..0503c66 100644 --- a/NodeApp/src/helpers/Dojo/ExerciseRunHelper.ts +++ b/NodeApp/src/helpers/Dojo/ExerciseRunHelper.ts @@ -5,7 +5,6 @@ import AssignmentFile from '../../shared/types/Dojo/Assign import ExerciseDockerCompose from '../../sharedByClients/helpers/Dojo/ExerciseDockerCompose.js'; import ExerciseResultsSanitizerAndValidator from '../../sharedByClients/helpers/Dojo/ExerciseResultsSanitizerAndValidator.js'; import fs from 'node:fs'; -import ClientsSharedConfig from '../../sharedByClients/config/ClientsSharedConfig.js'; import SharedAssignmentHelper from '../../shared/helpers/Dojo/SharedAssignmentHelper.js'; import path from 'path'; import ExerciseCheckerError from '../../shared/types/Dojo/ExerciseCheckerError.js'; @@ -15,6 +14,7 @@ import util from 'util'; import { exec } from 'child_process'; import SharedConfig from '../../shared/config/SharedConfig.js'; import TextStyle from '../../types/TextStyle.js'; +import ClientsSharedConfig from '../../sharedByClients/config/ClientsSharedConfig'; const execAsync = util.promisify(exec); diff --git a/NodeApp/src/helpers/GlobalHelper.ts b/NodeApp/src/helpers/GlobalHelper.ts index d79317f..dc9d244 100644 --- a/NodeApp/src/helpers/GlobalHelper.ts +++ b/NodeApp/src/helpers/GlobalHelper.ts @@ -1,10 +1,10 @@ import { Command, Option } from 'commander'; -import Config from '../config/Config.js'; import SessionManager from '../managers/SessionManager.js'; import TextStyle from '../types/TextStyle.js'; import ora from 'ora'; import DojoBackendManager from '../managers/DojoBackendManager.js'; import Assignment from '../sharedByClients/models/Assignment.js'; +import Config from '../config/Config'; class GlobalHelper { diff --git a/NodeApp/src/managers/GitlabManager.ts b/NodeApp/src/managers/GitlabManager.ts index 0e6c92e..a39a26e 100644 --- a/NodeApp/src/managers/GitlabManager.ts +++ b/NodeApp/src/managers/GitlabManager.ts @@ -11,8 +11,8 @@ type getGitlabUser = (param: number | string) => Promise<UserSchema | undefined> class GitlabManager extends SharedGitlabManager { - constructor() { - super('', GlobalHelper.refreshGitlabTokenFunction.bind(GlobalHelper)); + constructor(public gitlabUrl: string, clientId?: string, urlRedirect?: string, urlToken?: string) { + super(gitlabUrl, '', clientId, urlRedirect, urlToken, GlobalHelper.refreshGitlabTokenFunction.bind(GlobalHelper)); } public async testToken(verbose: boolean = true): Promise<[ boolean, boolean ]> { @@ -213,4 +213,4 @@ class GitlabManager extends SharedGitlabManager { } -export default new GitlabManager(); +export default GitlabManager; diff --git a/NodeApp/src/managers/HttpManager.ts b/NodeApp/src/managers/HttpManager.ts index 760f19c..ce1bc53 100644 --- a/NodeApp/src/managers/HttpManager.ts +++ b/NodeApp/src/managers/HttpManager.ts @@ -2,13 +2,13 @@ import axios, { AxiosError, AxiosRequestHeaders } from 'axios'; import SessionManager from './SessionManager.js'; import FormData from 'form-data'; import { StatusCodes } from 'http-status-codes'; -import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig.js'; import { version } from '../config/Version.js'; import DojoBackendResponse from '../shared/types/Dojo/DojoBackendResponse.js'; import DojoStatusCode from '../shared/types/Dojo/DojoStatusCode.js'; import boxen from 'boxen'; -import { stateConfigFile } from '../config/ConfigFiles.js'; import TextStyle from '../types/TextStyle.js'; +import ConfigFiles from '../config/ConfigFiles'; +import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; class HttpManager { @@ -25,9 +25,7 @@ class HttpManager { config.headers = { ...config.headers, ...config.data.getHeaders() } as AxiosRequestHeaders; } - if ( config.url && (config.url.indexOf(ClientsSharedConfig.apiURL) !== -1) ) { - - + if ( config.url && config.url.indexOf(ClientsSharedConfig.apiURL) !== -1 ) { config.headers['Accept-Encoding'] = 'gzip'; if ( config.data && Object.keys(config.data as { [key: string]: unknown }).length > 0 ) { @@ -106,11 +104,11 @@ class HttpManager { if ( response.headers['dojocli-latest-version'] ) { const latestDojoCliVersion = response.headers['dojocli-latest-version']; - const storedLatestDojoCliVersion = stateConfigFile.getParam('latestDojoCliVersion') as string | null || '0.0.0'; + const storedLatestDojoCliVersion = ConfigFiles.stateConfigFile.getParam('latestDojoCliVersion') as string | null || '0.0.0'; if ( latestDojoCliVersion !== storedLatestDojoCliVersion ) { - stateConfigFile.setParam('latestDojoCliVersion', latestDojoCliVersion); - stateConfigFile.setParam('latestDojoCliVersionNotification', 0); + ConfigFiles.stateConfigFile.setParam('latestDojoCliVersion', latestDojoCliVersion); + ConfigFiles.stateConfigFile.setParam('latestDojoCliVersionNotification', 0); } } diff --git a/NodeApp/src/managers/SessionManager.ts b/NodeApp/src/managers/SessionManager.ts index 7dd62cc..cffc6b9 100644 --- a/NodeApp/src/managers/SessionManager.ts +++ b/NodeApp/src/managers/SessionManager.ts @@ -7,21 +7,19 @@ import ora from 'ora'; import Permissions from '../types/Permissions.js'; import ApiRoute from '../sharedByClients/types/Dojo/ApiRoute.js'; import DojoBackendManager from './DojoBackendManager.js'; -import Config from '../config/Config.js'; -import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig.js'; import DojoGitlabCredentials from '../sharedByClients/types/Dojo/DojoGitlabCredentials.js'; import * as http from 'http'; import EventEmitter from 'events'; -import SharedConfig from '../shared/config/SharedConfig.js'; import chalk from 'chalk'; import inquirer from 'inquirer'; import GitlabToken from '../shared/types/Gitlab/GitlabToken.js'; import open from 'open'; -import { sessionConfigFile } from '../config/ConfigFiles.js'; import TextStyle from '../types/TextStyle.js'; import DojoBackendHelper from '../sharedByClients/helpers/Dojo/DojoBackendHelper.js'; -import GitlabManager from './GitlabManager.js'; import { StatusCodes } from 'http-status-codes'; +import ConfigFiles from '../config/ConfigFiles'; +import Config from '../config/Config'; +import ClientsSharedConfig from '../sharedByClients/config/ClientsSharedConfig'; class LoginServer { @@ -82,7 +80,7 @@ class SessionManager { } get apiToken(): string { - const apisToken = sessionConfigFile.getParam(LocalConfigKeys.APIS_TOKEN) as null | { [key: string]: string }; + const apisToken = ConfigFiles.sessionConfigFile.getParam(LocalConfigKeys.APIS_TOKEN) as null | { [key: string]: string }; if ( apisToken !== null && ClientsSharedConfig.apiURL in apisToken ) { return apisToken[ClientsSharedConfig.apiURL]; @@ -92,12 +90,12 @@ class SessionManager { } set apiToken(token: string) { - let apisToken = sessionConfigFile.getParam(LocalConfigKeys.APIS_TOKEN) as null | { [key: string]: string }; + let apisToken = ConfigFiles.sessionConfigFile.getParam(LocalConfigKeys.APIS_TOKEN) as null | { [key: string]: string }; if ( apisToken === null ) { apisToken = {}; } apisToken[ClientsSharedConfig.apiURL] = token; - sessionConfigFile.setParam(LocalConfigKeys.APIS_TOKEN, apisToken); + ConfigFiles.sessionConfigFile.setParam(LocalConfigKeys.APIS_TOKEN, apisToken); try { const payload = jwt.decode(token); @@ -111,14 +109,14 @@ class SessionManager { } get gitlabCredentials(): DojoGitlabCredentials { - return sessionConfigFile.getParam(LocalConfigKeys.GITLAB) as DojoGitlabCredentials; + return ConfigFiles.sessionConfigFile.getParam(LocalConfigKeys.GITLAB) as DojoGitlabCredentials; } set gitlabCredentials(credentials: DojoGitlabCredentials) { - sessionConfigFile.setParam(LocalConfigKeys.GITLAB, credentials); + ConfigFiles.sessionConfigFile.setParam(LocalConfigKeys.GITLAB, credentials); if ( credentials.accessToken ) { - GitlabManager.setToken(credentials.accessToken); + Config.gitlabManager.setToken(credentials.accessToken); } } @@ -127,7 +125,7 @@ class SessionManager { console.log(`${ indent }Please open the following URL in your web browser and accept to give the requested permissions to Dojo:`); console.log(TextStyle.URL(`${ indent }${ Config.login.gitlab.url.code }`)); console.log(`${ indent }Then, copy the code at the end of the redirected url and paste it bellow.`); - console.log(`${ indent }Example of url (here the code is 123456): ${ TextStyle.URL(SharedConfig.login.gitlab.url.redirect + '?code=') }${ chalk.green('123456') }`); + console.log(`${ indent }Example of url (here the code is 123456): ${ TextStyle.URL(ClientsSharedConfig.login.gitlab.url.redirect + '?code=') }${ chalk.green('123456') }`); return (await inquirer.prompt({ type : 'password', name : 'code', @@ -194,7 +192,7 @@ class SessionManager { throw error; } - ora(`Login with Gitlab (${ SharedConfig.gitlab.URL }):`).start().info(); + ora(`Login with Gitlab (${ ClientsSharedConfig.gitlab.URL }):`).start().info(); let gitlabCode: string; if ( !headless ) { @@ -209,7 +207,7 @@ class SessionManager { }).start(); let gitlabTokens: GitlabToken; try { - gitlabTokens = await GitlabManager.getTokens(gitlabCode); + gitlabTokens = await Config.gitlabManager.getTokens(gitlabCode); this.gitlabCredentials = { refreshToken: gitlabTokens.refresh_token, accessToken : gitlabTokens.access_token @@ -220,7 +218,7 @@ class SessionManager { throw error; } - const isGitlabTokensValid = (await GitlabManager.testToken()).every(value => value); + const isGitlabTokensValid = (await Config.gitlabManager.testToken()).every(value => value); if ( !isGitlabTokensValid ) { throw new Error('Gitlab tokens are invalid'); } -- GitLab