-
Notifications
You must be signed in to change notification settings - Fork 779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement a basic wrangler delete
#2045
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
feat: implement a basic `wrangler delete` | ||
|
||
This PR adds a simple (but useful!) implementation for `wrangler delete`. Of note, it'll delete a given service, including all it's bindings. It uses the same api as the dashboard. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id"; | ||
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch"; | ||
import { mockConsoleMethods } from "./helpers/mock-console"; | ||
import { mockConfirm } from "./helpers/mock-dialogs"; | ||
import { runInTempDir } from "./helpers/run-in-tmp"; | ||
import { runWrangler } from "./helpers/run-wrangler"; | ||
import writeWranglerToml from "./helpers/write-wrangler-toml"; | ||
|
||
describe("delete", () => { | ||
mockAccountId(); | ||
mockApiToken(); | ||
runInTempDir(); | ||
|
||
afterEach(() => { | ||
unsetAllMocks(); | ||
}); | ||
|
||
const std = mockConsoleMethods(); | ||
|
||
it("should delete an entire service by name", async () => { | ||
mockConfirm({ | ||
text: `Are you sure you want to delete my-script? This action cannot be undone.`, | ||
result: true, | ||
}); | ||
mockDeleteWorkerRequest({ name: "my-script" }); | ||
await runWrangler("delete --name my-script"); | ||
|
||
expect(std).toMatchInlineSnapshot(` | ||
Object { | ||
"debug": "", | ||
"err": "", | ||
"out": "Successfully deleted my-script", | ||
"warn": "", | ||
} | ||
`); | ||
}); | ||
|
||
it("should delete a script by configuration", async () => { | ||
mockConfirm({ | ||
text: `Are you sure you want to delete test-name? This action cannot be undone.`, | ||
result: true, | ||
}); | ||
writeWranglerToml(); | ||
mockDeleteWorkerRequest(); | ||
await runWrangler("delete"); | ||
|
||
expect(std).toMatchInlineSnapshot(` | ||
Object { | ||
"debug": "", | ||
"err": "", | ||
"out": "Successfully deleted test-name", | ||
"warn": "", | ||
} | ||
`); | ||
}); | ||
|
||
it("shouldn't delete a service when doing a --dry-run", async () => { | ||
await runWrangler("delete --name xyz --dry-run"); | ||
|
||
expect(std).toMatchInlineSnapshot(` | ||
Object { | ||
"debug": "", | ||
"err": "", | ||
"out": "--dry-run: exiting now.", | ||
"warn": "", | ||
} | ||
`); | ||
}); | ||
|
||
it('shouldn\'t delete when the user says "no"', async () => { | ||
mockConfirm({ | ||
text: `Are you sure you want to delete xyz? This action cannot be undone.`, | ||
result: false, | ||
}); | ||
|
||
await runWrangler("delete --name xyz"); | ||
|
||
expect(std).toMatchInlineSnapshot(` | ||
Object { | ||
"debug": "", | ||
"err": "", | ||
"out": "", | ||
"warn": "", | ||
} | ||
`); | ||
}); | ||
}); | ||
|
||
/** Create a mock handler for the request to upload a worker script. */ | ||
function mockDeleteWorkerRequest( | ||
options: { | ||
name?: string; | ||
env?: string; | ||
legacyEnv?: boolean; | ||
} = {} | ||
) { | ||
const { env, legacyEnv, name } = options; | ||
setMockResponse( | ||
// there's no special handling for environments yet | ||
"/accounts/:accountId/workers/services/:scriptName", | ||
"DELETE", | ||
async ([_url, accountId, scriptName], { method }, queryParams) => { | ||
expect(accountId).toEqual("some-account-id"); | ||
expect(method).toEqual("DELETE"); | ||
expect(scriptName).toEqual( | ||
legacyEnv && env | ||
? `${name || "test-name"}-${env}` | ||
: `${name || "test-name"}` | ||
); | ||
|
||
expect(queryParams.get("force")).toEqual("true"); | ||
|
||
return null; | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import path from "path"; | ||
import { fetchResult } from "./cfetch"; | ||
import { findWranglerToml, readConfig } from "./config"; | ||
import { confirm } from "./dialogs"; | ||
import { CI } from "./is-ci"; | ||
import isInteractive from "./is-interactive"; | ||
import { logger } from "./logger"; | ||
import * as metrics from "./metrics"; | ||
import { requireAuth } from "./user"; | ||
import { getScriptName, printWranglerBanner } from "./index"; | ||
import type { ConfigPath } from "./index"; | ||
import type { YargsOptionsToInterface } from "./yargs-types"; | ||
import type { Argv, ArgumentsCamelCase } from "yargs"; | ||
|
||
export function deleteOptions(yargs: Argv) { | ||
return yargs | ||
.option("env", { | ||
type: "string", | ||
requiresArg: true, | ||
describe: "Perform on a specific environment", | ||
alias: "e", | ||
}) | ||
.positional("script", { | ||
describe: "The path to an entry point for your worker", | ||
type: "string", | ||
requiresArg: true, | ||
}) | ||
.option("name", { | ||
describe: "Name of the worker", | ||
type: "string", | ||
requiresArg: true, | ||
}) | ||
.option("dry-run", { | ||
describe: "Don't actually delete", | ||
type: "boolean", | ||
}) | ||
.option("legacy-env", { | ||
type: "boolean", | ||
describe: "Use legacy environments", | ||
hidden: true, | ||
}); | ||
} | ||
|
||
type DeleteArgs = YargsOptionsToInterface<typeof deleteOptions>; | ||
|
||
export async function deleteHandler(args: ArgumentsCamelCase<DeleteArgs>) { | ||
await printWranglerBanner(); | ||
|
||
const configPath = | ||
(args.config as ConfigPath) || | ||
(args.script && findWranglerToml(path.dirname(args.script))); | ||
const config = readConfig(configPath, args); | ||
await metrics.sendMetricsEvent( | ||
"delete worker script", | ||
{}, | ||
{ sendMetrics: config.send_metrics } | ||
); | ||
|
||
const accountId = args.dryRun ? undefined : await requireAuth(config); | ||
|
||
const scriptName = getScriptName(args, config); | ||
|
||
if (args.dryRun) { | ||
logger.log(`--dry-run: exiting now.`); | ||
return; | ||
} | ||
|
||
let confirmed = true; | ||
if (isInteractive() || !CI.isCI()) { | ||
confirmed = await confirm( | ||
`Are you sure you want to delete ${scriptName}? This action cannot be undone.` | ||
); | ||
} | ||
|
||
if (confirmed) { | ||
await fetchResult( | ||
`/accounts/${accountId}/workers/services/${scriptName}`, | ||
{ method: "DELETE" }, | ||
new URLSearchParams({ force: "true" }) | ||
); | ||
|
||
logger.log("Successfully deleted", scriptName); | ||
} | ||
|
||
// TODO: maybe delete sites/assets kv namespace as well? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import makeCLI from "yargs"; | |
import { version as wranglerVersion } from "../package.json"; | ||
import { readConfig } from "./config"; | ||
import { d1 } from "./d1"; | ||
import { deleteHandler, deleteOptions } from "./delete"; | ||
import { | ||
buildHandler, | ||
buildOptions, | ||
|
@@ -258,6 +259,14 @@ export function createCLIParser(argv: string[]) { | |
publishHandler | ||
); | ||
|
||
// delete | ||
wrangler.command( | ||
"delete [script]", | ||
"🗑 Delete your Worker from Cloudflare.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧡 |
||
deleteOptions, | ||
deleteHandler | ||
); | ||
|
||
// tail | ||
wrangler.command( | ||
"tail [worker]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the
force
be added as a CLI option?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered it, but honestly it's the only way that it's actually useful (which is why it's added by default on the dashboard too).