From fc9b0dd2aa3cc0ee5d9cee34830fed58c2278d1b Mon Sep 17 00:00:00 2001 From: Kevin Stubbs Date: Mon, 8 Jul 2024 13:54:51 +0700 Subject: [PATCH 1/4] Add delete site command. More human-friendly 404 message for admin commands. --- .changeset/tough-zebras-pull.md | 6 +++ packages/cli/src/cli/commands/sites/admins.ts | 46 +++++++++++++++---- packages/cli/src/cli/commands/sites/site.ts | 15 ++++++ packages/cli/src/cli/index.ts | 30 ++++++++++++ packages/cli/src/lib/addonApiHelper.ts | 25 ++++++++++ 5 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 .changeset/tough-zebras-pull.md diff --git a/.changeset/tough-zebras-pull.md b/.changeset/tough-zebras-pull.md new file mode 100644 index 00000000..91948339 --- /dev/null +++ b/.changeset/tough-zebras-pull.md @@ -0,0 +1,6 @@ +--- +"@pantheon-systems/pcc-cli": minor +--- + +New command: site delete. +Improved error messages for incorrect/invalid site id for admin actions. diff --git a/packages/cli/src/cli/commands/sites/admins.ts b/packages/cli/src/cli/commands/sites/admins.ts index 595097d7..86ff2f18 100644 --- a/packages/cli/src/cli/commands/sites/admins.ts +++ b/packages/cli/src/cli/commands/sites/admins.ts @@ -1,3 +1,5 @@ +import axios from "axios"; +import chalk from "chalk"; import ora from "ora"; import AddOnApiHelper from "../../../lib/addonApiHelper"; import { errorHandler } from "../../exceptions"; @@ -5,27 +7,51 @@ import { errorHandler } from "../../exceptions"; type listAdminsSchemaParams = { siteId: string }; export const listAdminsSchema = errorHandler( async ({ siteId }: listAdminsSchemaParams) => { - const spinner = ora("Retrieving admins...").start(); - const result = await AddOnApiHelper.listAdmins(siteId); - spinner.succeed(); - console.log(JSON.stringify(result, null, 4)); + try { + const spinner = ora("Retrieving admins...").start(); + const result = await AddOnApiHelper.listAdmins(siteId); + spinner.succeed(); + console.log(JSON.stringify(result, null, 4)); + } catch (e) { + if (axios.isAxiosError(e) && e.response?.status === 404) { + console.log(chalk.red(`\nSite ID not recognized`)); + } else { + throw e; + } + } }, ); type removeAdminSchemaParams = { siteId: string; email: string }; export const removeAdminSchema = errorHandler( async ({ siteId, email }: removeAdminSchemaParams) => { - const spinner = ora("Removing admin...").start(); - await AddOnApiHelper.removeAdmin(siteId, email); - spinner.succeed(); + try { + const spinner = ora("Removing admin...").start(); + await AddOnApiHelper.removeAdmin(siteId, email); + spinner.succeed(); + } catch (e) { + if (axios.isAxiosError(e) && e.response?.status === 404) { + console.log(chalk.red(`\nSite ID not recognized`)); + } else { + throw e; + } + } }, ); type addAdminSchemaParams = { siteId: string; email: string }; export const addAdminSchema = errorHandler( async ({ siteId, email }: addAdminSchemaParams) => { - const spinner = ora("Adding admin...").start(); - await AddOnApiHelper.addAdmin(siteId, email); - spinner.succeed(); + try { + const spinner = ora("Adding admin...").start(); + await AddOnApiHelper.addAdmin(siteId, email); + spinner.succeed(); + } catch (e) { + if (axios.isAxiosError(e) && e.response?.status === 404) { + console.log(chalk.red(`\nSite ID not recognized`)); + } else { + throw e; + } + } }, ); diff --git a/packages/cli/src/cli/commands/sites/site.ts b/packages/cli/src/cli/commands/sites/site.ts index 55b26dc7..410eee89 100644 --- a/packages/cli/src/cli/commands/sites/site.ts +++ b/packages/cli/src/cli/commands/sites/site.ts @@ -18,6 +18,21 @@ export const createSite = errorHandler(async (url: string) => { } }); +export const deleteSite = errorHandler<{ + id: string; + transferToSiteId: string | null | undefined; + force: boolean; +}>(async ({ id, transferToSiteId, force }) => { + const spinner = ora("Deleting site...").start(); + try { + const siteId = await AddOnApiHelper.deleteSite(id, transferToSiteId, force); + spinner.succeed(`Successfully deleted the site with id "${siteId}"`); + } catch (e) { + spinner.fail(); + throw e; + } +}); + export const listSites = errorHandler<{ withStatus?: boolean; }>(async ({ withStatus }) => { diff --git a/packages/cli/src/cli/index.ts b/packages/cli/src/cli/index.ts index dfaab14b..55f7f0f0 100755 --- a/packages/cli/src/cli/index.ts +++ b/packages/cli/src/cli/index.ts @@ -30,6 +30,7 @@ import { import { configurableSiteProperties, createSite, + deleteSite, listSites, SITE_EXAMPLES, updateSiteConfig, @@ -379,6 +380,35 @@ yargs(hideBin(process.argv)) }, async (args) => await createSite(args.url as string), ) + .command( + "create [options]", + "Delete site.", + (yargs) => { + yargs.option("id", { + describe: "Site id", + type: "string", + demandOption: true, + }); + yargs.option("transferToSiteId", { + describe: + "Id of site to transfer connected documents to. Required if force is not used.", + type: "string", + demandOption: false, + }); + yargs.option("force", { + describe: + "If true, delete even if documents are connected to it.", + type: "string", + demandOption: false, + }); + }, + async (args) => + await deleteSite({ + id: args.id as string, + transferToSiteId: args.transferToSiteId as string, + force: args.force === "true", + }), + ) .command( "componentschema [command] [options]", "Make changes & inspect the component schema for a site", diff --git a/packages/cli/src/lib/addonApiHelper.ts b/packages/cli/src/lib/addonApiHelper.ts index eb839db1..968f2217 100644 --- a/packages/cli/src/lib/addonApiHelper.ts +++ b/packages/cli/src/lib/addonApiHelper.ts @@ -2,6 +2,7 @@ import { SmartComponentMapZod } from "@pantheon-systems/pcc-sdk-core/types"; import axios, { AxiosError, HttpStatusCode } from "axios"; import { Credentials } from "google-auth-library"; import ora from "ora"; +import queryString from "query-string"; import login from "../cli/commands/login"; import { HTTPNotFound, UserNotLoggedIn } from "../cli/exceptions"; import { getApiConfig } from "./apiConfig"; @@ -290,6 +291,30 @@ class AddOnApiHelper { return resp.data.id as string; } + static async deleteSite( + id: string, + transferToSiteId: string | null | undefined, + force: boolean, + ): Promise { + const idToken = await this.getIdToken(); + + const resp = await axios.delete( + queryString.stringifyUrl({ + url: `${(await getApiConfig()).SITE_ENDPOINT}/${id}`, + query: { + transferToSiteId, + force, + }, + }), + { + headers: { + Authorization: `Bearer ${idToken}`, + }, + }, + ); + return resp.data.id as string; + } + static async listSites({ withConnectionStatus, }: { From 653b742348a69c618dad49a746694d42095cf6f3 Mon Sep 17 00:00:00 2001 From: Kevin Stubbs Date: Mon, 8 Jul 2024 18:34:20 +0700 Subject: [PATCH 2/4] Fix lint --- packages/cli/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index 15e4f208..969da6d0 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -5,7 +5,8 @@ ### Patch Changes - 0c75736: CLI replaces "--version" command with "version" -- 1fd7c4d: Added support for setting preferred webhook events. Webhook notifications will only be sent on events matching preferred events +- 1fd7c4d: Added support for setting preferred webhook events. Webhook + notifications will only be sent on events matching preferred events - Updated dependencies [183ad17] - @pantheon-systems/pcc-sdk-core@3.6.1 From faee0c9bb0b8ecc97aa05a4e4b1fea433ac671c1 Mon Sep 17 00:00:00 2001 From: Kevin Stubbs Date: Tue, 9 Jul 2024 00:26:01 +0700 Subject: [PATCH 3/4] Undo messaging changes --- packages/cli/src/cli/commands/sites/admins.ts | 46 ++++--------------- 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/packages/cli/src/cli/commands/sites/admins.ts b/packages/cli/src/cli/commands/sites/admins.ts index 86ff2f18..595097d7 100644 --- a/packages/cli/src/cli/commands/sites/admins.ts +++ b/packages/cli/src/cli/commands/sites/admins.ts @@ -1,5 +1,3 @@ -import axios from "axios"; -import chalk from "chalk"; import ora from "ora"; import AddOnApiHelper from "../../../lib/addonApiHelper"; import { errorHandler } from "../../exceptions"; @@ -7,51 +5,27 @@ import { errorHandler } from "../../exceptions"; type listAdminsSchemaParams = { siteId: string }; export const listAdminsSchema = errorHandler( async ({ siteId }: listAdminsSchemaParams) => { - try { - const spinner = ora("Retrieving admins...").start(); - const result = await AddOnApiHelper.listAdmins(siteId); - spinner.succeed(); - console.log(JSON.stringify(result, null, 4)); - } catch (e) { - if (axios.isAxiosError(e) && e.response?.status === 404) { - console.log(chalk.red(`\nSite ID not recognized`)); - } else { - throw e; - } - } + const spinner = ora("Retrieving admins...").start(); + const result = await AddOnApiHelper.listAdmins(siteId); + spinner.succeed(); + console.log(JSON.stringify(result, null, 4)); }, ); type removeAdminSchemaParams = { siteId: string; email: string }; export const removeAdminSchema = errorHandler( async ({ siteId, email }: removeAdminSchemaParams) => { - try { - const spinner = ora("Removing admin...").start(); - await AddOnApiHelper.removeAdmin(siteId, email); - spinner.succeed(); - } catch (e) { - if (axios.isAxiosError(e) && e.response?.status === 404) { - console.log(chalk.red(`\nSite ID not recognized`)); - } else { - throw e; - } - } + const spinner = ora("Removing admin...").start(); + await AddOnApiHelper.removeAdmin(siteId, email); + spinner.succeed(); }, ); type addAdminSchemaParams = { siteId: string; email: string }; export const addAdminSchema = errorHandler( async ({ siteId, email }: addAdminSchemaParams) => { - try { - const spinner = ora("Adding admin...").start(); - await AddOnApiHelper.addAdmin(siteId, email); - spinner.succeed(); - } catch (e) { - if (axios.isAxiosError(e) && e.response?.status === 404) { - console.log(chalk.red(`\nSite ID not recognized`)); - } else { - throw e; - } - } + const spinner = ora("Adding admin...").start(); + await AddOnApiHelper.addAdmin(siteId, email); + spinner.succeed(); }, ); From 6055547ba0d75518bb322ebf68d2625ad35ddb5c Mon Sep 17 00:00:00 2001 From: Kevin Stubbs Date: Tue, 9 Jul 2024 00:26:47 +0700 Subject: [PATCH 4/4] Update changelog --- .changeset/tough-zebras-pull.md | 1 - 1 file changed, 1 deletion(-) diff --git a/.changeset/tough-zebras-pull.md b/.changeset/tough-zebras-pull.md index 91948339..7a2cdbbd 100644 --- a/.changeset/tough-zebras-pull.md +++ b/.changeset/tough-zebras-pull.md @@ -3,4 +3,3 @@ --- New command: site delete. -Improved error messages for incorrect/invalid site id for admin actions.