From ba46a192328b7c488c99e9c9d425407f4e32ecc5 Mon Sep 17 00:00:00 2001 From: Marcus Eide Date: Mon, 13 Feb 2023 13:02:10 +0100 Subject: [PATCH] cli: Move to folder and split up into different questions Signed-off-by: Marcus Eide --- .../cli/src/commands/admin/{ => auth}/file.ts | 15 ++++- .../admin/{gh-auth.ts => auth/github/app.ts} | 4 +- .../src/commands/admin/auth/github/index.ts | 64 +++++++++++++++++++ .../admin/{github.ts => auth/github/oauth.ts} | 23 +------ packages/cli/src/commands/admin/auth/index.ts | 45 +++++++++++++ packages/cli/src/commands/admin/command.ts | 56 +--------------- 6 files changed, 130 insertions(+), 77 deletions(-) rename packages/cli/src/commands/admin/{ => auth}/file.ts (88%) rename packages/cli/src/commands/admin/{gh-auth.ts => auth/github/app.ts} (92%) create mode 100644 packages/cli/src/commands/admin/auth/github/index.ts rename packages/cli/src/commands/admin/{github.ts => auth/github/oauth.ts} (90%) create mode 100644 packages/cli/src/commands/admin/auth/index.ts diff --git a/packages/cli/src/commands/admin/file.ts b/packages/cli/src/commands/admin/auth/file.ts similarity index 88% rename from packages/cli/src/commands/admin/file.ts rename to packages/cli/src/commands/admin/auth/file.ts index 6135c53ebf..14a8a151f8 100644 --- a/packages/cli/src/commands/admin/file.ts +++ b/packages/cli/src/commands/admin/auth/file.ts @@ -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); diff --git a/packages/cli/src/commands/admin/gh-auth.ts b/packages/cli/src/commands/admin/auth/github/app.ts similarity index 92% rename from packages/cli/src/commands/admin/gh-auth.ts rename to packages/cli/src/commands/admin/auth/github/app.ts index ee5bbfb2c6..234f8d1e29 100644 --- a/packages/cli/src/commands/admin/gh-auth.ts +++ b/packages/cli/src/commands/admin/auth/github/app.ts @@ -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 }>([ { diff --git a/packages/cli/src/commands/admin/auth/github/index.ts b/packages/cli/src/commands/admin/auth/github/index.ts new file mode 100644 index 0000000000..8ea7e63bc9 --- /dev/null +++ b/packages/cli/src/commands/admin/auth/github/index.ts @@ -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}.`); + } +}; diff --git a/packages/cli/src/commands/admin/github.ts b/packages/cli/src/commands/admin/auth/github/oauth.ts similarity index 90% rename from packages/cli/src/commands/admin/github.ts rename to packages/cli/src/commands/admin/auth/github/oauth.ts index 0cb900c85d..4daccdfd8c 100644 --- a/packages/cli/src/commands/admin/github.ts +++ b/packages/cli/src/commands/admin/auth/github/oauth.ts @@ -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 => { +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; diff --git a/packages/cli/src/commands/admin/auth/index.ts b/packages/cli/src/commands/admin/auth/index.ts new file mode 100644 index 0000000000..9af145dcf3 --- /dev/null +++ b/packages/cli/src/commands/admin/auth/index.ts @@ -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 { + 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}!`); +} diff --git a/packages/cli/src/commands/admin/command.ts b/packages/cli/src/commands/admin/command.ts index 33dd7bd20f..4c49631a80 100644 --- a/packages/cli/src/commands/admin/command.ts +++ b/packages/cli/src/commands/admin/command.ts @@ -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 { const answers = await inquirer.prompt<{ shouldSetupAuth: boolean; - useEnvForSecrets?: boolean; provider?: string; }>([ { @@ -32,20 +28,6 @@ export async function command(): Promise { 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 { 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(); }