Skip to content

Commit afdebef

Browse files
authored
Improve api key flow (#113)
1 parent 28ad98a commit afdebef

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

src/application/cli/command/apiKey/create.ts

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {ErrorReason, HelpfulError} from '@/application/error';
1010
import {UserApi} from '@/application/api/user';
1111
import {FileSystem} from '@/application/fs/fileSystem';
1212
import {WorkspaceApi} from '@/application/api/workspace';
13+
import {ProjectConfiguration} from '@/application/project/configuration/projectConfiguration';
1314

1415
export type CreateApiKeyInput = {
1516
name?: ApiKey['name'],
@@ -43,31 +44,9 @@ export class CreateApiKeyCommand implements Command<CreateApiKeyInput> {
4344
const {configurationManager, api, fileSystem, io} = this.config;
4445
const configuration = await configurationManager.load();
4546

46-
const environment = input.environment ?? await io.input.select({
47-
message: 'Which environment?',
48-
options: ApplicationEnvironment.all()
49-
.map(
50-
value => ({
51-
label: ApplicationEnvironment.getLabel(value),
52-
value: value,
53-
}),
54-
),
55-
});
56-
57-
const applicationSlug = environment === ApplicationEnvironment.PRODUCTION
58-
? configuration.applications.production
59-
: configuration.applications.development;
60-
61-
if (applicationSlug === undefined) {
62-
throw new HelpfulError(
63-
`No ${ApplicationEnvironment.getLabel(environment).toLowerCase()} application `
64-
+ 'found in the project configuration.',
65-
{reason: ErrorReason.INVALID_INPUT},
66-
);
67-
}
68-
6947
const notifier = io.output.notify('Loading information');
70-
48+
const environment = await this.getEnvironment(configuration, input.environment);
49+
const applicationSlug = this.getApplicationSlug(configuration, environment);
7150
const defaultName = input.name ?? `${(await api.user.getUser()).username} (CLI)`;
7251
const features = await api.workspace.getFeatures({
7352
organizationSlug: configuration.organization,
@@ -131,4 +110,46 @@ export class CreateApiKeyCommand implements Command<CreateApiKeyInput> {
131110

132111
io.output.confirm(`API key saved to \`${fileName}\``);
133112
}
113+
114+
private getApplicationSlug(configuration: ProjectConfiguration, environment: ApplicationEnvironment): string {
115+
const application = environment === ApplicationEnvironment.PRODUCTION
116+
? configuration.applications.production
117+
: configuration.applications.development;
118+
119+
if (application === undefined) {
120+
throw new HelpfulError(
121+
`No ${ApplicationEnvironment.getLabel(environment).toLowerCase()} application `
122+
+ 'found in the project configuration.',
123+
{reason: ErrorReason.INVALID_INPUT},
124+
);
125+
}
126+
127+
return application;
128+
}
129+
130+
private getEnvironment(
131+
configuration: ProjectConfiguration,
132+
environment?: ApplicationEnvironment,
133+
): Promise<ApplicationEnvironment> {
134+
if (environment !== undefined) {
135+
return Promise.resolve(environment);
136+
}
137+
138+
if (configuration.applications.production === undefined) {
139+
return Promise.resolve(ApplicationEnvironment.DEVELOPMENT);
140+
}
141+
142+
const {io: {input}} = this.config;
143+
144+
return input.select({
145+
message: 'Select environment',
146+
options: ApplicationEnvironment.all()
147+
.map(
148+
value => ({
149+
label: ApplicationEnvironment.getLabel(value),
150+
value: value,
151+
}),
152+
),
153+
});
154+
}
134155
}

0 commit comments

Comments
 (0)