Skip to content

Commit

Permalink
Shaharsh/feat/list keys by region (#101)
Browse files Browse the repository at this point in the history
* When setting or deleting value in specific region, list-keys for this region and not the default region.

* refactor the management mode and flags to handle for both secrets and env variables.
  • Loading branch information
Shaharshaki2 authored Sep 10, 2024
1 parent 97247b5 commit bad9856
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 76 deletions.
41 changes: 23 additions & 18 deletions src/commands/code/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,43 @@ 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, appId: AppId, key?: string) => {
const promptForKeyIfNotProvided = async (
mode: APP_VARIABLE_MANAGEMENT_MODES,
appId: AppId,
key?: string,
region?: Region,
) => {
if (!key && isKeyRequired(mode)) {
const existingKeys = await listAppEnvKeys(appId);
const existingKeys = await listAppEnvKeys(appId, region);
key = await PromptService.promptSelectionWithAutoComplete('Enter key for environment variable', existingKeys, {
includeInputInSelection: true,
});
Expand All @@ -41,12 +47,12 @@ const promptForKeyIfNotProvided = async (mode: APP_ENV_MANAGEMENT_MODES, appId:
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 @@ -58,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 @@ -79,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 @@ -101,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 All @@ -110,7 +115,7 @@ export default class Env extends AuthenticatedCommand {
const selectedRegion = await chooseRegionIfNeeded(region, { appId });

mode = await promptForModeIfNotProvided(mode);
key = await promptForKeyIfNotProvided(mode, appId, key);
key = await promptForKeyIfNotProvided(mode, appId, key, selectedRegion);
value = await promptForValueIfNotProvided(mode, value);
this.preparePrintCommand(this, { appId, mode, key, value, region: selectedRegion });

Expand Down
44 changes: 23 additions & 21 deletions src/commands/code/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,43 @@ 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, appId: AppId, key?: string) => {
const promptForKeyIfNotProvided = async (
mode: APP_VARIABLE_MANAGEMENT_MODES,
appId: AppId,
key?: string,
region?: Region,
) => {
if (!key && isKeyRequired(mode)) {
const existingKeys = await listAppSecretKeys(appId);
const existingKeys = await listAppSecretKeys(appId, region);
key = await PromptService.promptSelectionWithAutoComplete('Enter key for secret variable', existingKeys, {
includeInputInSelection: true,
});
Expand All @@ -41,12 +47,12 @@ const promptForKeyIfNotProvided = async (mode: APP_SECRET_MANAGEMENT_MODES, appI
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 @@ -79,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 @@ -101,22 +107,18 @@ 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());
}

const selectedRegion = await chooseRegionIfNeeded(region, { appId });

mode = await promptForModeIfNotProvided(mode);
key = await promptForKeyIfNotProvided(mode, appId, key);
key = await promptForKeyIfNotProvided(mode, appId, key, selectedRegion);
value = await promptForValueIfNotProvided(mode, value);
this.preparePrintCommand(this, { appId, mode, key, value, region: selectedRegion });
console.log("1")

await handleSecretRequest(appId, mode, key, value, selectedRegion);
console.log("4")
} catch (error: any) {
logger.debug(error, this.DEBUG_TAG);

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 bad9856

Please sign in to comment.