From 3218ceaa75dfd0bff9e896788a8f14df941111db Mon Sep 17 00:00:00 2001 From: Alexis Tyler Date: Mon, 19 Apr 2021 14:10:04 +0930 Subject: [PATCH] feat: add switch-env to cli --- app/cli.ts | 48 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/app/cli.ts b/app/cli.ts index e79ff8610..1fb8ad581 100644 --- a/app/cli.ts +++ b/app/cli.ts @@ -69,8 +69,8 @@ const getUnraidApiPid = async () => { const commands = { /** - * Start a new API process. - */ + * Start a new API process. + */ async start() { // Load .env file dotEnv.config(); @@ -123,8 +123,8 @@ const commands = { } }, /** - * Stop a running API process. - */ + * Stop a running API process. + */ async stop() { // Find process called "unraid-api" const unraidApiPid = await getUnraidApiPid(); @@ -135,19 +135,20 @@ const commands = { return; } - console.log(`Sending process ${unraidApiPid} SIGTERM.`); + console.info('Stopping unraid-api process...'); process.kill(unraidApiPid, 'SIGTERM'); + console.info('Process stopped!'); }, /** - * Stop a running API process and then start it again. - */ + * Stop a running API process and then start it again. + */ async restart() { await this.stop(); await this.start(); }, /** - * Print API version. - */ + * Print API version. + */ async version() { console.log(`Unraid API v${version as string}`); }, @@ -173,6 +174,35 @@ const commands = { Unraid version: ${unraidVersion} `); + }, + async 'switch-env'() { + const envFile = await fs.promises.readFile('/boot/config/plugins/Unraid.net/env', 'utf-8').catch(() => ''); + // Match the env file env="production" which would be [1] = env and [2] = production + const matchArray = /([a-zA-Z]+)=["]*([a-zA-Z]+)["]*/.exec(envFile); + // Get item from index 2 of the regex match or return undefined + const [,,currentEnv] = matchArray && matchArray.length === 2 ? matchArray : []; + + // No env is set or file doesn't exist + if (!currentEnv) { + console.info('Switching env to "production"...'); + + // Default back to production + await fs.promises.writeFile('/boot/config/plugins/Unraid.net/env', 'env=production'); + return; + } + + // Switch from staging to production + if (currentEnv === 'staging') { + await fs.promises.writeFile('/boot/config/plugins/Unraid.net/env', 'env=production'); + } + + // Switch from production to staging + if (currentEnv === 'staging') { + await fs.promises.writeFile('/boot/config/plugins/Unraid.net/env', 'env=staging'); + } + + // Restart the process + return this.restart(); } };