Skip to content

Commit

Permalink
refactor the management mode and flags to handle for both secrets and…
Browse files Browse the repository at this point in the history
… env variables.
  • Loading branch information
Shaharshaki2 committed Sep 10, 2024
1 parent 40c314b commit 6c34a71
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 68 deletions.
31 changes: 15 additions & 16 deletions src/commands/code/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ 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> = [
APP_ENV_MANAGEMENT_MODES.SET,
APP_ENV_MANAGEMENT_MODES.DELETE,
const MODES_WITH_KEYS: Array<APP_VARIABLE_MANAGEMENT_MODES> = [
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<APP_ENV_MANAGEMENT_MODES>(
mode = await PromptService.promptSelectionWithAutoComplete<APP_VARIABLE_MANAGEMENT_MODES>(
'Select app environment variables management mode',
Object.values(APP_ENV_MANAGEMENT_MODES),
Object.values(APP_VARIABLE_MANAGEMENT_MODES),
);
}

return mode;
};

const promptForKeyIfNotProvided = async (
mode: APP_ENV_MANAGEMENT_MODES,
mode: APP_VARIABLE_MANAGEMENT_MODES,
appId: AppId,
key?: string,
region?: Region,
Expand All @@ -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',
);
}

Expand All @@ -64,7 +64,6 @@ const flagsWithModeRelationships: Relationship = {
flags: [
{
name: 'mode',

when: async (flags: Record<string, unknown>) => isValueRequired(flags.mode as (typeof MODES_WITH_KEYS)[number]),
},
],
Expand All @@ -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',
Expand All @@ -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());
Expand Down
30 changes: 15 additions & 15 deletions src/commands/code/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,37 @@ 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> = [
APP_SECRET_MANAGEMENT_MODES.SET,
APP_SECRET_MANAGEMENT_MODES.DELETE,
const MODES_WITH_KEYS: Array<APP_VARIABLE_MANAGEMENT_MODES> = [
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<APP_SECRET_MANAGEMENT_MODES>(
mode = await PromptService.promptSelectionWithAutoComplete<APP_VARIABLE_MANAGEMENT_MODES>(
'Select app secret variables management mode',
Object.values(APP_SECRET_MANAGEMENT_MODES),
Object.values(APP_VARIABLE_MANAGEMENT_MODES),
);
}

return mode;
};

const promptForKeyIfNotProvided = async (
mode: APP_SECRET_MANAGEMENT_MODES,
mode: APP_VARIABLE_MANAGEMENT_MODES,
appId: AppId,
key?: string,
region?: Region,
Expand All @@ -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',
);
}

Expand Down Expand Up @@ -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',
Expand All @@ -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());
Expand Down
5 changes: 0 additions & 5 deletions src/consts/manage-app-secret.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum APP_ENV_MANAGEMENT_MODES {
export enum APP_VARIABLE_MANAGEMENT_MODES {
LIST_KEYS = 'list-keys',
SET = 'set',
DELETE = 'delete',
Expand Down
12 changes: 6 additions & 6 deletions src/services/manage-app-env-service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<void>
> = {
[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,
Expand Down
13 changes: 6 additions & 7 deletions src/services/manage-app-secret-service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<ListAppSecretKeysResponse>(
{
query,
Expand Down Expand Up @@ -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<void>
> = {
[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,
Expand Down
9 changes: 0 additions & 9 deletions src/types/commands/manage-app-env.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/types/commands/manage-app-secret.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/types/commands/manage-app-variable.ts
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 6c34a71

Please sign in to comment.