From ce0fcbc90310ef9d2d9a1cba6ab88b75c07789fc Mon Sep 17 00:00:00 2001 From: Marcos Passos Date: Wed, 2 Jul 2025 16:35:45 -0300 Subject: [PATCH] Improve api key flow --- src/application/cli/command/apiKey/create.ts | 69 +++++++++++++------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/src/application/cli/command/apiKey/create.ts b/src/application/cli/command/apiKey/create.ts index 25eb7533..00ced76c 100644 --- a/src/application/cli/command/apiKey/create.ts +++ b/src/application/cli/command/apiKey/create.ts @@ -10,6 +10,7 @@ import {ErrorReason, HelpfulError} from '@/application/error'; import {UserApi} from '@/application/api/user'; import {FileSystem} from '@/application/fs/fileSystem'; import {WorkspaceApi} from '@/application/api/workspace'; +import {ProjectConfiguration} from '@/application/project/configuration/projectConfiguration'; export type CreateApiKeyInput = { name?: ApiKey['name'], @@ -43,31 +44,9 @@ export class CreateApiKeyCommand implements Command { const {configurationManager, api, fileSystem, io} = this.config; const configuration = await configurationManager.load(); - const environment = input.environment ?? await io.input.select({ - message: 'Which environment?', - options: ApplicationEnvironment.all() - .map( - value => ({ - label: ApplicationEnvironment.getLabel(value), - value: value, - }), - ), - }); - - const applicationSlug = environment === ApplicationEnvironment.PRODUCTION - ? configuration.applications.production - : configuration.applications.development; - - if (applicationSlug === undefined) { - throw new HelpfulError( - `No ${ApplicationEnvironment.getLabel(environment).toLowerCase()} application ` - + 'found in the project configuration.', - {reason: ErrorReason.INVALID_INPUT}, - ); - } - const notifier = io.output.notify('Loading information'); - + const environment = await this.getEnvironment(configuration, input.environment); + const applicationSlug = this.getApplicationSlug(configuration, environment); const defaultName = input.name ?? `${(await api.user.getUser()).username} (CLI)`; const features = await api.workspace.getFeatures({ organizationSlug: configuration.organization, @@ -131,4 +110,46 @@ export class CreateApiKeyCommand implements Command { io.output.confirm(`API key saved to \`${fileName}\``); } + + private getApplicationSlug(configuration: ProjectConfiguration, environment: ApplicationEnvironment): string { + const application = environment === ApplicationEnvironment.PRODUCTION + ? configuration.applications.production + : configuration.applications.development; + + if (application === undefined) { + throw new HelpfulError( + `No ${ApplicationEnvironment.getLabel(environment).toLowerCase()} application ` + + 'found in the project configuration.', + {reason: ErrorReason.INVALID_INPUT}, + ); + } + + return application; + } + + private getEnvironment( + configuration: ProjectConfiguration, + environment?: ApplicationEnvironment, + ): Promise { + if (environment !== undefined) { + return Promise.resolve(environment); + } + + if (configuration.applications.production === undefined) { + return Promise.resolve(ApplicationEnvironment.DEVELOPMENT); + } + + const {io: {input}} = this.config; + + return input.select({ + message: 'Select environment', + options: ApplicationEnvironment.all() + .map( + value => ({ + label: ApplicationEnvironment.getLabel(value), + value: value, + }), + ), + }); + } }