From 6c34a7198a91228ab37d966021bdb4c290d77ec0 Mon Sep 17 00:00:00 2001 From: Shahar Shaki Date: Tue, 10 Sep 2024 11:41:23 +0300 Subject: [PATCH] refactor the management mode and flags to handle for both secrets and env variables. --- src/commands/code/env.ts | 31 +++++++++---------- src/commands/code/secret.ts | 30 +++++++++--------- src/consts/manage-app-secret.ts | 5 --- ...age-app-env.ts => manage-app-variables.ts} | 2 +- src/services/manage-app-env-service.ts | 12 +++---- src/services/manage-app-secret-service.ts | 13 ++++---- src/types/commands/manage-app-env.ts | 9 ------ src/types/commands/manage-app-secret.ts | 9 ------ src/types/commands/manage-app-variable.ts | 9 ++++++ 9 files changed, 52 insertions(+), 68 deletions(-) delete mode 100644 src/consts/manage-app-secret.ts rename src/consts/{manage-app-env.ts => manage-app-variables.ts} (59%) delete mode 100644 src/types/commands/manage-app-env.ts delete mode 100644 src/types/commands/manage-app-secret.ts create mode 100644 src/types/commands/manage-app-variable.ts diff --git a/src/commands/code/env.ts b/src/commands/code/env.ts index 8e852ac..46ba303 100644 --- a/src/commands/code/env.ts +++ b/src/commands/code/env.ts @@ -2,29 +2,29 @@ import { Flags } from '@oclif/core'; import { Relationship } from '@oclif/core/lib/interfaces/parser'; import { AuthenticatedCommand } from 'commands-base/authenticated-command'; -import { APP_ENV_MANAGEMENT_MODES } from 'consts/manage-app-env'; +import { APP_VARIABLE_MANAGEMENT_MODES } from 'consts/manage-app-variables'; import { DynamicChoicesService } from 'services/dynamic-choices-service'; import { handleEnvironmentRequest, listAppEnvKeys } from 'services/manage-app-env-service'; import { PromptService } from 'services/prompt-service'; -import { ManageAppEnvFlags } from 'types/commands/manage-app-env'; +import { ManageAppVariableFlags } from 'types/commands/manage-app-variable'; import { AppId } from 'types/general'; import { Region } from 'types/general/region'; import logger from 'utils/logger'; import { addRegionToFlags, chooseRegionIfNeeded, getRegionFromString } from 'utils/region'; -const MODES_WITH_KEYS: Array = [ - APP_ENV_MANAGEMENT_MODES.SET, - APP_ENV_MANAGEMENT_MODES.DELETE, +const MODES_WITH_KEYS: Array = [ + APP_VARIABLE_MANAGEMENT_MODES.SET, + APP_VARIABLE_MANAGEMENT_MODES.DELETE, ]; -const isKeyRequired = (mode: APP_ENV_MANAGEMENT_MODES) => MODES_WITH_KEYS.includes(mode); -const isValueRequired = (mode: APP_ENV_MANAGEMENT_MODES) => mode === APP_ENV_MANAGEMENT_MODES.SET; +const isKeyRequired = (mode: APP_VARIABLE_MANAGEMENT_MODES) => MODES_WITH_KEYS.includes(mode); +const isValueRequired = (mode: APP_VARIABLE_MANAGEMENT_MODES) => mode === APP_VARIABLE_MANAGEMENT_MODES.SET; -const promptForModeIfNotProvided = async (mode?: APP_ENV_MANAGEMENT_MODES) => { +const promptForModeIfNotProvided = async (mode?: APP_VARIABLE_MANAGEMENT_MODES) => { if (!mode) { - mode = await PromptService.promptSelectionWithAutoComplete( + mode = await PromptService.promptSelectionWithAutoComplete( 'Select app environment variables management mode', - Object.values(APP_ENV_MANAGEMENT_MODES), + Object.values(APP_VARIABLE_MANAGEMENT_MODES), ); } @@ -32,7 +32,7 @@ const promptForModeIfNotProvided = async (mode?: APP_ENV_MANAGEMENT_MODES) => { }; const promptForKeyIfNotProvided = async ( - mode: APP_ENV_MANAGEMENT_MODES, + mode: APP_VARIABLE_MANAGEMENT_MODES, appId: AppId, key?: string, region?: Region, @@ -47,12 +47,12 @@ const promptForKeyIfNotProvided = async ( return key; }; -const promptForValueIfNotProvided = async (mode: APP_ENV_MANAGEMENT_MODES, value?: string) => { +const promptForValueIfNotProvided = async (mode: APP_VARIABLE_MANAGEMENT_MODES, value?: string) => { if (!value && isValueRequired(mode)) { value = await PromptService.promptForHiddenInput( 'value', 'Enter value for environment variable', - 'You must enter a value value', + 'You must enter a value', ); } @@ -64,7 +64,6 @@ const flagsWithModeRelationships: Relationship = { flags: [ { name: 'mode', - when: async (flags: Record) => isValueRequired(flags.mode as (typeof MODES_WITH_KEYS)[number]), }, ], @@ -85,7 +84,7 @@ export default class Env extends AuthenticatedCommand { mode: Flags.string({ char: 'm', description: 'management mode', - options: Object.values(APP_ENV_MANAGEMENT_MODES), + options: Object.values(APP_VARIABLE_MANAGEMENT_MODES), }), key: Flags.string({ char: 'k', @@ -107,7 +106,7 @@ export default class Env extends AuthenticatedCommand { const { flags } = await this.parse(Env); const { region: strRegion } = flags; const region = getRegionFromString(strRegion); - let { mode, key, value, appId } = flags as ManageAppEnvFlags; + let { mode, key, value, appId } = flags as ManageAppVariableFlags; if (!appId) { appId = Number(await DynamicChoicesService.chooseApp()); diff --git a/src/commands/code/secret.ts b/src/commands/code/secret.ts index 0a96502..4dfe7f0 100644 --- a/src/commands/code/secret.ts +++ b/src/commands/code/secret.ts @@ -2,29 +2,29 @@ import { Flags } from '@oclif/core'; import { Relationship } from '@oclif/core/lib/interfaces/parser'; import { AuthenticatedCommand } from 'commands-base/authenticated-command'; -import { APP_SECRET_MANAGEMENT_MODES } from 'consts/manage-app-secret'; +import { APP_VARIABLE_MANAGEMENT_MODES } from 'consts/manage-app-variables'; import { DynamicChoicesService } from 'services/dynamic-choices-service'; import { handleSecretRequest, listAppSecretKeys } from 'services/manage-app-secret-service'; import { PromptService } from 'services/prompt-service'; -import { ManageAppSecretFlags } from 'types/commands/manage-app-secret'; +import { ManageAppVariableFlags } from 'types/commands/manage-app-variable'; import { AppId } from 'types/general'; import { Region } from 'types/general/region'; import logger from 'utils/logger'; import { addRegionToFlags, chooseRegionIfNeeded, getRegionFromString } from 'utils/region'; -const MODES_WITH_KEYS: Array = [ - APP_SECRET_MANAGEMENT_MODES.SET, - APP_SECRET_MANAGEMENT_MODES.DELETE, +const MODES_WITH_KEYS: Array = [ + APP_VARIABLE_MANAGEMENT_MODES.SET, + APP_VARIABLE_MANAGEMENT_MODES.DELETE, ]; -const isKeyRequired = (mode: APP_SECRET_MANAGEMENT_MODES) => MODES_WITH_KEYS.includes(mode); -const isValueRequired = (mode: APP_SECRET_MANAGEMENT_MODES) => mode === APP_SECRET_MANAGEMENT_MODES.SET; +const isKeyRequired = (mode: APP_VARIABLE_MANAGEMENT_MODES) => MODES_WITH_KEYS.includes(mode); +const isValueRequired = (mode: APP_VARIABLE_MANAGEMENT_MODES) => mode === APP_VARIABLE_MANAGEMENT_MODES.SET; -const promptForModeIfNotProvided = async (mode?: APP_SECRET_MANAGEMENT_MODES) => { +const promptForModeIfNotProvided = async (mode?: APP_VARIABLE_MANAGEMENT_MODES) => { if (!mode) { - mode = await PromptService.promptSelectionWithAutoComplete( + mode = await PromptService.promptSelectionWithAutoComplete( 'Select app secret variables management mode', - Object.values(APP_SECRET_MANAGEMENT_MODES), + Object.values(APP_VARIABLE_MANAGEMENT_MODES), ); } @@ -32,7 +32,7 @@ const promptForModeIfNotProvided = async (mode?: APP_SECRET_MANAGEMENT_MODES) => }; const promptForKeyIfNotProvided = async ( - mode: APP_SECRET_MANAGEMENT_MODES, + mode: APP_VARIABLE_MANAGEMENT_MODES, appId: AppId, key?: string, region?: Region, @@ -47,12 +47,12 @@ const promptForKeyIfNotProvided = async ( return key; }; -const promptForValueIfNotProvided = async (mode: APP_SECRET_MANAGEMENT_MODES, value?: string) => { +const promptForValueIfNotProvided = async (mode: APP_VARIABLE_MANAGEMENT_MODES, value?: string) => { if (!value && isValueRequired(mode)) { value = await PromptService.promptForHiddenInput( 'value', 'Enter value for secret variable', - 'You must enter a value value', + 'You must enter a value', ); } @@ -85,7 +85,7 @@ export default class Secret extends AuthenticatedCommand { mode: Flags.string({ char: 'm', description: 'management mode', - options: Object.values(APP_SECRET_MANAGEMENT_MODES), + options: Object.values(APP_VARIABLE_MANAGEMENT_MODES), }), key: Flags.string({ char: 'k', @@ -107,7 +107,7 @@ export default class Secret extends AuthenticatedCommand { const { flags } = await this.parse(Secret); const { region: strRegion } = flags; const region = getRegionFromString(strRegion); - let { mode, key, value, appId } = flags as ManageAppSecretFlags; + let { mode, key, value, appId } = flags as ManageAppVariableFlags; if (!appId) { appId = Number(await DynamicChoicesService.chooseApp()); diff --git a/src/consts/manage-app-secret.ts b/src/consts/manage-app-secret.ts deleted file mode 100644 index 3ae3545..0000000 --- a/src/consts/manage-app-secret.ts +++ /dev/null @@ -1,5 +0,0 @@ -export enum APP_SECRET_MANAGEMENT_MODES { - LIST_KEYS = 'list-keys', - SET = 'set', - DELETE = 'delete', -} diff --git a/src/consts/manage-app-env.ts b/src/consts/manage-app-variables.ts similarity index 59% rename from src/consts/manage-app-env.ts rename to src/consts/manage-app-variables.ts index 11c275d..4d92f35 100644 --- a/src/consts/manage-app-env.ts +++ b/src/consts/manage-app-variables.ts @@ -1,4 +1,4 @@ -export enum APP_ENV_MANAGEMENT_MODES { +export enum APP_VARIABLE_MANAGEMENT_MODES { LIST_KEYS = 'list-keys', SET = 'set', DELETE = 'delete', diff --git a/src/services/manage-app-env-service.ts b/src/services/manage-app-env-service.ts index c959ddc..ffdd2a4 100644 --- a/src/services/manage-app-env-service.ts +++ b/src/services/manage-app-env-service.ts @@ -1,6 +1,6 @@ import { StatusCodes } from 'http-status-codes'; -import { APP_ENV_MANAGEMENT_MODES } from 'consts/manage-app-env'; +import { APP_VARIABLE_MANAGEMENT_MODES } from 'consts/manage-app-variables'; import { appEnvironmentKeysUrl, appEnvironmentUrl } from 'consts/urls'; import { execute } from 'services/api-service'; import { listAppEnvironmentKeysResponseSchema } from 'services/schemas/manage-app-env-service-schemas'; @@ -130,17 +130,17 @@ const handleEnvironmentListKeys = async (appId: AppId, region: Region | undefine }; const MAP_MODE_TO_HANDLER: Record< - APP_ENV_MANAGEMENT_MODES, + APP_VARIABLE_MANAGEMENT_MODES, (appId: AppId, region: Region | undefined, key: string, value: string) => Promise > = { - [APP_ENV_MANAGEMENT_MODES.SET]: handleEnvironmentSet, - [APP_ENV_MANAGEMENT_MODES.DELETE]: handleEnvironmentDelete, - [APP_ENV_MANAGEMENT_MODES.LIST_KEYS]: handleEnvironmentListKeys, + [APP_VARIABLE_MANAGEMENT_MODES.SET]: handleEnvironmentSet, + [APP_VARIABLE_MANAGEMENT_MODES.DELETE]: handleEnvironmentDelete, + [APP_VARIABLE_MANAGEMENT_MODES.LIST_KEYS]: handleEnvironmentListKeys, }; export const handleEnvironmentRequest = async ( appId: AppId, - mode: APP_ENV_MANAGEMENT_MODES, + mode: APP_VARIABLE_MANAGEMENT_MODES, key?: string, value?: string, region?: Region, diff --git a/src/services/manage-app-secret-service.ts b/src/services/manage-app-secret-service.ts index d054931..8ba740c 100644 --- a/src/services/manage-app-secret-service.ts +++ b/src/services/manage-app-secret-service.ts @@ -1,6 +1,6 @@ import { StatusCodes } from 'http-status-codes'; -import { APP_SECRET_MANAGEMENT_MODES } from 'consts/manage-app-secret'; +import { APP_VARIABLE_MANAGEMENT_MODES } from 'consts/manage-app-variables'; import { appSecretKeysUrl, appSecretUrl } from 'consts/urls'; import { execute } from 'services/api-service'; import { listAppSecretKeysResponseSchema } from 'services/schemas/manage-app-secret-service-schemas'; @@ -34,7 +34,6 @@ export const listAppSecretKeys = async (appId: AppId, region?: Region): Promise< const path = appSecretKeysUrl(appId); const url = appsUrlBuilder(path); const query = addRegionToQuery({}, region); - const response = await execute( { query, @@ -129,17 +128,17 @@ const handleSecretListKeys = async (appId: AppId, region: Region | undefined) => }; const MAP_MODE_TO_HANDLER: Record< - APP_SECRET_MANAGEMENT_MODES, + APP_VARIABLE_MANAGEMENT_MODES, (appId: AppId, region: Region | undefined, key: string, value: string) => Promise > = { - [APP_SECRET_MANAGEMENT_MODES.SET]: handleSecretSet, - [APP_SECRET_MANAGEMENT_MODES.DELETE]: handleSecretDelete, - [APP_SECRET_MANAGEMENT_MODES.LIST_KEYS]: handleSecretListKeys, + [APP_VARIABLE_MANAGEMENT_MODES.SET]: handleSecretSet, + [APP_VARIABLE_MANAGEMENT_MODES.DELETE]: handleSecretDelete, + [APP_VARIABLE_MANAGEMENT_MODES.LIST_KEYS]: handleSecretListKeys, }; export const handleSecretRequest = async ( appId: AppId, - mode: APP_SECRET_MANAGEMENT_MODES, + mode: APP_VARIABLE_MANAGEMENT_MODES, key?: string, value?: string, region?: Region, diff --git a/src/types/commands/manage-app-env.ts b/src/types/commands/manage-app-env.ts deleted file mode 100644 index e940e16..0000000 --- a/src/types/commands/manage-app-env.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { APP_ENV_MANAGEMENT_MODES } from 'consts/manage-app-env'; -import { AppId } from 'types/general'; - -export type ManageAppEnvFlags = { - mode?: APP_ENV_MANAGEMENT_MODES; - key?: string; - value?: string; - appId?: AppId; -}; diff --git a/src/types/commands/manage-app-secret.ts b/src/types/commands/manage-app-secret.ts deleted file mode 100644 index be52462..0000000 --- a/src/types/commands/manage-app-secret.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { APP_SECRET_MANAGEMENT_MODES } from 'consts/manage-app-secret'; -import { AppId } from 'types/general'; - -export type ManageAppSecretFlags = { - mode?: APP_SECRET_MANAGEMENT_MODES; - key?: string; - value?: string; - appId?: AppId; -}; diff --git a/src/types/commands/manage-app-variable.ts b/src/types/commands/manage-app-variable.ts new file mode 100644 index 0000000..a286c45 --- /dev/null +++ b/src/types/commands/manage-app-variable.ts @@ -0,0 +1,9 @@ +import { APP_VARIABLE_MANAGEMENT_MODES } from 'consts/manage-app-variables'; +import { AppId } from 'types/general'; + +export type ManageAppVariableFlags = { + mode?: APP_VARIABLE_MANAGEMENT_MODES; + key?: string; + value?: string; + appId?: AppId; +};