Skip to content

Commit

Permalink
Add new versions secret commands
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Jun 7, 2024
1 parent 21573f4 commit 2bc0741
Show file tree
Hide file tree
Showing 14 changed files with 717 additions and 116 deletions.
11 changes: 11 additions & 0 deletions .changeset/little-rice-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"wrangler": minor
---

feat: add `wrangler versions secret put`, `wrangler versions secret put bulk` and `wrangler versions secret list`

`wrangler versions secret put` allows for you to add/update a secret even if the latest version is not fully deployed. A new version with this secret will be created, the existing secrets and config are copied from the latest version.

`wrangler versions secret put bulk` allows you to bulk add/update multiple secrets at once, this behaves the same as `secret put` and will only make one new version.

`wrangler versions secret list` lists the secrets available to the currently deployed versions. `wrangler secret list` will list for the latest version.
30 changes: 9 additions & 21 deletions packages/wrangler/src/__tests__/secret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,9 @@ describe("wrangler secret", () => {
mockListRequest({ scriptName: "script-name" });
await runWrangler("secret list --name script-name");
expect(std.out).toMatchInlineSnapshot(`
"[
{
\\"name\\": \\"the-secret-name\\",
\\"type\\": \\"secret_text\\"
}
]"
`);
"Secret Name: the-secret-name
"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

Expand All @@ -519,13 +515,9 @@ describe("wrangler secret", () => {
"secret list --name script-name --env some-env --legacy-env"
);
expect(std.out).toMatchInlineSnapshot(`
"[
{
\\"name\\": \\"the-secret-name\\",
\\"type\\": \\"secret_text\\"
}
]"
`);
"Secret Name: the-secret-name
"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

Expand All @@ -535,13 +527,9 @@ describe("wrangler secret", () => {
"secret list --name script-name --env some-env --legacy-env false"
);
expect(std.out).toMatchInlineSnapshot(`
"[
{
\\"name\\": \\"the-secret-name\\",
\\"type\\": \\"secret_text\\"
}
]"
`);
"Secret Name: the-secret-name
"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

Expand Down
140 changes: 72 additions & 68 deletions packages/wrangler/src/__tests__/versions/versions.help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,24 @@ describe("versions --help", () => {
await expect(result).resolves.toBeUndefined();

expect(std.out).toMatchInlineSnapshot(`
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
wrangler versions secret 🤫 Generate a secret that can be referenced in a Worker
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
});

Expand All @@ -96,23 +97,24 @@ describe("versions subhelp", () => {
await setImmediate(); // wait for subhelp

expect(std.out).toMatchInlineSnapshot(`
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
wrangler versions secret 🤫 Generate a secret that can be referenced in a Worker
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});

test("shows implicit subhelp with --x-versions flag", async () => {
Expand All @@ -122,23 +124,24 @@ describe("versions subhelp", () => {
await setImmediate(); // wait for subhelp

expect(std.out).toMatchInlineSnapshot(`
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
wrangler versions secret 🤫 Generate a secret that can be referenced in a Worker
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});

test("shows implicit subhelp with --experimental-gradual-rollouts flag", async () => {
Expand All @@ -148,22 +151,23 @@ describe("versions subhelp", () => {
await setImmediate(); // wait for subhelp

expect(std.out).toMatchInlineSnapshot(`
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
"wrangler versions
List, view, upload and deploy Versions of your Worker to Cloudflare [beta]
Commands:
wrangler versions view <version-id> View the details of a specific version of your Worker [beta]
wrangler versions list List the 10 most recent Versions of your Worker [beta]
wrangler versions upload Uploads your Worker code and config as a new Version [beta]
wrangler versions deploy [version-specs..] Safely roll out new Versions of your Worker by splitting traffic between multiple Versions [beta]
wrangler versions secret 🤫 Generate a secret that can be referenced in a Worker
Flags:
-j, --experimental-json-config Experimental: Support wrangler.json [boolean]
-c, --config Path to .toml configuration file [string]
-e, --env Environment to use for operations and .env files [string]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]"
`);
});
});
8 changes: 5 additions & 3 deletions packages/wrangler/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ export function readConfig<CommandArgs>(
configPath: string | undefined,
// Include command specific args as well as the wrangler global flags
args: ReadConfigCommandArgs<CommandArgs>,
requirePagesConfig?: boolean
requirePagesConfig?: boolean,
hideWarnings?: boolean
): Config;
export function readConfig<CommandArgs>(
configPath: string | undefined,
// Include command specific args as well as the wrangler global flags
args: ReadConfigCommandArgs<CommandArgs>,
requirePagesConfig?: boolean
requirePagesConfig?: boolean,
hideWarnings?: boolean
): Config {
let rawConfig: RawConfig = {};

Expand Down Expand Up @@ -112,7 +114,7 @@ export function readConfig<CommandArgs>(
args
);

if (diagnostics.hasWarnings()) {
if (diagnostics.hasWarnings() && hideWarnings !== true) {
logger.warn(diagnostics.renderWarnings());
}
if (diagnostics.hasErrors()) {
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/config/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2162,6 +2162,7 @@ const validateUnsafeBinding: ValidatorFn = (diagnostics, field, value) => {
if (isRequiredProperty(value, "type", "string")) {
const safeBindings = [
"plain_text",
"secret_text",
"json",
"wasm_module",
"data_blob",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,42 @@ import type {
} from "./worker.js";
import type { Json } from "miniflare";

const moduleTypeMimeType: { [type in CfModuleType]: string | undefined } = {
esm: "application/javascript+module",
commonjs: "application/javascript",
"compiled-wasm": "application/wasm",
buffer: "application/octet-stream",
text: "text/plain",
python: "text/x-python",
"python-requirement": "text/x-python-requirement",
"nodejs-compat-module": undefined,
};

export function toMimeType(type: CfModuleType): string {
switch (type) {
case "esm":
return "application/javascript+module";
case "commonjs":
return "application/javascript";
case "compiled-wasm":
return "application/wasm";
case "buffer":
return "application/octet-stream";
case "text":
return "text/plain";
case "python":
return "text/x-python";
case "python-requirement":
return "text/x-python-requirement";
default:
throw new TypeError("Unsupported module: " + type);
const mimeType = moduleTypeMimeType[type];
if (mimeType === undefined) {
throw new TypeError("Unsupported module: " + type);
}

return mimeType;
}

export function fromMimeType(mimeType: string): CfModuleType {
const moduleType = Object.keys(moduleTypeMimeType).find(
(type) => moduleTypeMimeType[type as CfModuleType] === mimeType
) as CfModuleType | undefined;
if (moduleType === undefined) {
throw new TypeError("Unsupported module: " + moduleType);
}

return moduleType;
}

export type WorkerMetadataBinding =
// If you add any new binding types here, also add it to safeBindings
// under validateUnsafeBinding in config/validation.ts
| { type: "plain_text"; name: string; text: string }
| { type: "secret_text"; name: string; text: string }
| { type: "json"; name: string; json: Json }
| { type: "wasm_module"; name: string; part: string }
| { type: "text_blob"; name: string; part: string }
Expand Down Expand Up @@ -143,6 +154,7 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {
main,
sourceMaps,
bindings,
rawBindings,
migrations,
usage_model,
compatibility_date,
Expand All @@ -158,7 +170,7 @@ export function createWorkerUploadForm(worker: CfWorkerInit): FormData {

let { modules } = worker;

const metadataBindings: WorkerMetadata["bindings"] = [];
const metadataBindings: WorkerMetadataBinding[] = rawBindings ?? [];

Object.entries(bindings.vars || {})?.forEach(([key, value]) => {
if (typeof value === "string") {
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/deployment-bundle/worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Route } from "../config/environment";
import type { WorkerMetadataBinding } from "./create-worker-upload-form";
import type { Json } from "miniflare";

/**
Expand Down Expand Up @@ -314,6 +315,13 @@ export interface CfWorkerInit {
logfwdr: CfLogfwdr | undefined;
unsafe: CfUnsafe | undefined;
};
/**
* The raw bindings - this is basically never provided and it'll be the bindings above
* but if we're just taking from the api and re-putting then this is how we can do that
* without going between the different types
*/
rawBindings?: WorkerMetadataBinding[];

migrations: CfDurableObjectMigrations | undefined;
compatibility_date: string | undefined;
compatibility_flags: string[] | undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ export function createCLIParser(argv: string[]) {
"versions",
"List, view, upload and deploy Versions of your Worker to Cloudflare [beta]",
(yargs) => {
return registerVersionsSubcommands(yargs.command(subHelp));
return registerVersionsSubcommands(yargs.command(subHelp), subHelp);
}
);
}
Expand Down
Loading

0 comments on commit 2bc0741

Please sign in to comment.