cli: Move to folder and split up into different questions

Signed-off-by: Marcus Eide <eide@spotify.com>
This commit is contained in:
Marcus Eide
2023-02-13 13:02:10 +01:00
committed by Philipp Hugenroth
parent d9893cb794
commit ba46a19232
6 changed files with 130 additions and 77 deletions
@@ -18,7 +18,20 @@ import * as path from 'path';
import * as fs from 'fs-extra';
import yaml from 'yaml';
import { findPaths } from '@backstage/cli-common';
import { GithubAuthConfig } from './github';
export type GithubAuthConfig = {
auth: {
providers: {
github: {
development: {
clientId: string;
clientSecret: string;
enterpriseInstanceUrl?: string;
};
};
};
};
};
/* eslint-disable-next-line no-restricted-syntax */
const { targetRoot } = findPaths(__dirname);
@@ -15,10 +15,10 @@
*/
import chalk from 'chalk';
import inquirer from 'inquirer';
import { adminCli } from '../create-github-app';
import { adminCli } from '../../../create-github-app';
// TODO(tudi2d): Wrapper for admin CLI around `create-github-app` - potentially to be removed
export default async () => {
export const app = async () => {
// TODO(tudi2d): Make the GitHub Org optional
const input = await inquirer.prompt<{ org: string }>([
{
@@ -0,0 +1,64 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import inquirer from 'inquirer';
import { GithubAuthConfig, updateConfigFile, updateEnvFile } from '../file';
import { app } from './app';
import { oauth } from './oauth';
export const github = async () => {
const answers = await inquirer.prompt<{
useEnvForSecrets: boolean;
type: string;
}>([
{
type: 'confirm',
name: 'useEnvForSecrets',
message:
'Would you like to store sensitive configuration details such as secrets as environment variables? (recommended)',
},
{
type: 'list',
name: 'type',
message: 'Do you want to use a Github App or a Github OAuth?',
choices: ['GitHub OAuth', 'GitHub App'],
},
]);
const { type, useEnvForSecrets } = answers;
switch (type) {
case 'GitHub OAuth': {
const config: GithubAuthConfig = await oauth(useEnvForSecrets);
await updateConfigFile(config);
if (useEnvForSecrets) {
await updateEnvFile(config);
}
break;
}
case 'GitHub App': {
const { auth }: GithubAuthConfig = await app();
// TODO(tudi2d): Also change integrations
await updateConfigFile({ auth });
if (useEnvForSecrets) {
await updateEnvFile({ auth });
}
break;
}
default:
throw new Error(`Unknown selection: ${type}.`);
}
};
@@ -17,21 +17,7 @@
import { OAuthApp } from '@octokit/oauth-app';
import chalk from 'chalk';
import inquirer from 'inquirer';
import { Task } from '../../lib/tasks';
export type GithubAuthConfig = {
auth: {
providers: {
github: {
development: {
clientId: string;
clientSecret: string;
enterpriseInstanceUrl?: string;
};
};
};
};
};
import { Task } from '../../../../lib/tasks';
const validateCredentials = async (clientId: string, clientSecret: string) => {
try {
@@ -58,9 +44,7 @@ const validateCredentials = async (clientId: string, clientSecret: string) => {
}
};
export const github = async (
useEnvForSecrets?: boolean,
): Promise<GithubAuthConfig> => {
export const oauth = async (useEnvForSecrets?: boolean) => {
Task.log(`
To add GitHub authentication, you must create an OAuth App from the GitHub developer settings: ${chalk.blue(
'https://github.com/settings/developers',
@@ -74,8 +58,7 @@ export const github = async (
You can find the full documentation page here: ${chalk.blue(
'https://backstage.io/docs/auth/github/provider',
)}
`);
)}`);
const answers = await inquirer.prompt<{
clientSecret: string;
@@ -0,0 +1,45 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import inquirer from 'inquirer';
import { Task } from '../../../lib/tasks';
import { github } from './github';
export async function auth(): Promise<void> {
const answers = await inquirer.prompt<{
provider?: string;
}>([
{
type: 'list',
name: 'provider',
message: 'Please select a provider:',
choices: ['Github'],
},
]);
const { provider } = answers;
switch (provider) {
case 'Github': {
await github();
break;
}
default:
throw new Error(`Provider ${provider} not implemented yet.`);
}
Task.log(`Done setting up ${provider}!`);
}
+2 -54
View File
@@ -16,15 +16,11 @@
import chalk from 'chalk';
import inquirer from 'inquirer';
import { Task } from '../../lib/tasks';
import { github } from './github';
import { updateConfigFile, updateEnvFile } from './file';
import ghAuth from './gh-auth';
import { auth } from './auth';
export async function command(): Promise<void> {
const answers = await inquirer.prompt<{
shouldSetupAuth: boolean;
useEnvForSecrets?: boolean;
provider?: string;
}>([
{
@@ -32,20 +28,6 @@ export async function command(): Promise<void> {
name: 'shouldSetupAuth',
message: 'Do you want to set up Authentication for this project?',
},
{
type: 'confirm',
name: 'useEnvForSecrets',
message:
'Would you like to store sensitive configuration details such as secrets as environment variables? (recommended)',
when: ({ shouldSetupAuth }) => shouldSetupAuth,
},
{
type: 'list',
name: 'provider',
message: 'Please select a provider:',
choices: ['GitHub OAuth', 'GitHub App'],
when: ({ shouldSetupAuth }) => shouldSetupAuth,
},
]);
if (!answers.shouldSetupAuth) {
@@ -57,39 +39,5 @@ export async function command(): Promise<void> {
process.exit(1);
}
const { useEnvForSecrets } = answers;
switch (answers.provider) {
case 'GitHub OAuth': {
const config = await github(useEnvForSecrets);
await updateConfigFile(config);
if (useEnvForSecrets) {
await updateEnvFile(config);
}
break;
}
case 'GitHub App': {
const config = await ghAuth();
// TODO(tudi2d): Also change integrations
if (useEnvForSecrets) {
await updateEnvFile({
auth: config.auth,
});
config.auth.providers.github.development.clientId =
'${AUTH_GITHUB_CLIENT_ID}';
config.auth.providers.github.development.clientSecret =
'${AUTH_GITHUB_CLIENT_SECRET}';
await updateConfigFile({
auth: config.auth,
});
} else {
await updateConfigFile({ auth: config.auth });
}
break;
}
default:
throw new Error(`Provider ${answers.provider} not implemented yet.`);
}
Task.log(`Done setting up ${answers.provider}!`);
await auth();
}