Skip to content

Commit

Permalink
chore: convert deployments to createCommand (#7499)
Browse files Browse the repository at this point in the history
* chore: convert deployments to createCommand

kebab case for positional

remove print banner

chore: register namesapce

* chore: add hidden to deployments view

* remove unused imports

* Update packages/wrangler/src/versions/deployments/list.ts

* Update packages/wrangler/src/versions/deployments/list.ts

* Update packages/wrangler/src/versions/deployments/status.ts

* Update packages/wrangler/src/versions/deployments/status.ts

* Update packages/wrangler/src/versions/deployments/status.ts

* Update packages/wrangler/src/versions/deployments/view.ts

* chore: update snapshots

---------

Co-authored-by: Carmen Popoviciu <[email protected]>
  • Loading branch information
andyjessop and CarmenPopoviciu authored Dec 17, 2024
1 parent ca9410a commit a55bc36
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 284 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("deployments list", () => {
const result = runWrangler("deployments list --experimental-versions");

await expect(result).rejects.toMatchInlineSnapshot(
`[Error: You need to provide a name of your worker. Either pass it as a cli arg with \`--name <name>\` or in your config file as \`name = "<name>"\`]`
`[Error: You need to provide a name for your Worker. Either pass it as a cli arg with \`--name <name>\` or in your configuration file as \`name = "<name>"\`]`
);

expect(std.out).toMatchInlineSnapshot(`""`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("deployments list", () => {
);

await expect(result).rejects.toMatchInlineSnapshot(
`[Error: You need to provide a name of your worker. Either pass it as a cli arg with \`--name <name>\` or in your config file as \`name = "<name>"\`]`
`[Error: You need to provide a name for your Worker. Either pass it as a cli arg with \`--name <name>\` or in your configuration file as \`name = "<name>"\`]`
);

expect(std.out).toMatchInlineSnapshot(`""`);
Expand Down
107 changes: 60 additions & 47 deletions packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ import { debugLogFilepath } from "./utils/log-file";
import { vectorize } from "./vectorize/index";
import { versionsNamespace } from "./versions";
import { versionsDeployCommand } from "./versions/deploy";
import registerVersionsDeploymentsSubcommands from "./versions/deployments";
import { deploymentsNamespace } from "./versions/deployments";
import { deploymentsListCommand } from "./versions/deployments/list";
import { deploymentsStatusCommand } from "./versions/deployments/status";
import { deploymentsViewCommand } from "./versions/deployments/view";
import { versionsListCommand } from "./versions/list";
import registerVersionsRollbackCommand from "./versions/rollback";
import { versionsSecretNamespace } from "./versions/secrets";
Expand Down Expand Up @@ -477,55 +480,65 @@ export function createCLIParser(argv: string[]) {
deployHandler
);

// deployments
const deploymentsDescription =
"🚢 List and view the current and past deployments for your Worker";

if (experimentalGradualRollouts) {
registry.define([
{ command: "wrangler deployments", definition: deploymentsNamespace },
{
command: "wrangler deployments list",
definition: deploymentsListCommand,
},
{
command: "wrangler deployments status",
definition: deploymentsStatusCommand,
},
{
command: "wrangler deployments view",
definition: deploymentsViewCommand,
},
]);
registry.registerNamespace("deployments");
} else {
wrangler.command(
"deployments",
deploymentsDescription,
registerVersionsDeploymentsSubcommands
);
} else {
wrangler.command("deployments", deploymentsDescription, (yargs) =>
yargs
.option("name", {
describe: "The name of your Worker",
type: "string",
})
.command(
"list",
"Displays the 10 most recent deployments for a Worker",
async (listYargs) => listYargs,
async (listYargs) => {
const { accountId, scriptName, config } =
await commonDeploymentCMDSetup(listYargs);
await deployments(accountId, scriptName, config);
}
)
.command(
"view [deployment-id]",
"View a deployment",
async (viewYargs) =>
viewYargs.positional("deployment-id", {
describe: "The ID of the deployment you want to inspect",
type: "string",
demandOption: false,
}),
async (viewYargs) => {
const { accountId, scriptName, config } =
await commonDeploymentCMDSetup(viewYargs);

await viewDeployment(
accountId,
scriptName,
config,
viewYargs.deploymentId
);
}
)
.command(subHelp)
"🚢 List and view the current and past deployments for your Worker",
(yargs) =>
yargs
.option("name", {
describe: "The name of your Worker",
type: "string",
})
.command(
"list",
"Displays the 10 most recent deployments for a Worker",
async (listYargs) => listYargs,
async (listYargs) => {
const { accountId, scriptName, config } =
await commonDeploymentCMDSetup(listYargs);
await deployments(accountId, scriptName, config);
}
)
.command(
"view [deployment-id]",
"View a deployment",
async (viewYargs) =>
viewYargs.positional("deployment-id", {
describe: "The ID of the deployment you want to inspect",
type: "string",
demandOption: false,
}),
async (viewYargs) => {
const { accountId, scriptName, config } =
await commonDeploymentCMDSetup(viewYargs);

await viewDeployment(
accountId,
scriptName,
config,
viewYargs.deploymentId
);
}
)
.command(subHelp)
);
}

Expand Down
45 changes: 9 additions & 36 deletions packages/wrangler/src/versions/deployments/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,10 @@
import {
versionsDeploymentsListHandler,
versionsDeploymentsListOptions,
} from "./list";
import {
versionsDeploymentsStatusHandler,
versionsDeploymentsStatusOptions,
} from "./status";
import {
versionsDeploymentsViewHandler,
versionsDeploymentsViewOptions,
} from "./view";
import type { CommonYargsArgv } from "../../yargs-types";
import { createNamespace } from "../../core/create-command";

export default function registerVersionsDeploymentsSubcommands(
versionDeploymentsYargs: CommonYargsArgv
) {
versionDeploymentsYargs
.command(
"list",
"Displays the 10 most recent deployments of your Worker",
versionsDeploymentsListOptions,
versionsDeploymentsListHandler
)
.command(
"status",
"View the current state of your production",
versionsDeploymentsStatusOptions,
versionsDeploymentsStatusHandler
)
.command(
"view [deployment-id]",
false,
versionsDeploymentsViewOptions,
versionsDeploymentsViewHandler
);
}
export const deploymentsNamespace = createNamespace({
metadata: {
description:
"🚢 List and view the current and past deployments for your Worker",
owner: "Workers: Authoring and Testing",
status: "stable",
},
});
164 changes: 78 additions & 86 deletions packages/wrangler/src/versions/deployments/list.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,107 @@
import assert from "assert";
import { logRaw } from "@cloudflare/cli";
import { brandColor, gray } from "@cloudflare/cli/colors";
import { readConfig } from "../../config";
import { createCommand } from "../../core/create-command";
import { UserError } from "../../errors";
import * as metrics from "../../metrics";
import { requireAuth } from "../../user";
import formatLabelledValues from "../../utils/render-labelled-values";
import { printWranglerBanner } from "../../wrangler-banner";
import { fetchLatestDeployments, fetchVersions } from "../api";
import { getVersionSource } from "../list";
import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../../yargs-types";
import type { ApiDeployment, VersionCache } from "../types";

const BLANK_INPUT = "-"; // To be used where optional user-input is displayed and the value is nullish

export type VersionsDeloymentsListArgs = StrictYargsOptionsToInterface<
typeof versionsDeploymentsListOptions
>;

export function versionsDeploymentsListOptions(yargs: CommonYargsArgv) {
return yargs
.option("name", {
describe: "Name of the worker",
export const deploymentsListCommand = createCommand({
metadata: {
description: "Displays the 10 most recent deployments of your Worker",
owner: "Workers: Authoring and Testing",
status: "stable",
},
args: {
name: {
describe: "Name of the Worker",
type: "string",
requiresArg: true,
})
.option("json", {
},
json: {
describe: "Display output as clean JSON",
type: "boolean",
default: false,
});
}

export async function versionsDeploymentsListHandler(
args: VersionsDeloymentsListArgs
) {
if (!args.json) {
await printWranglerBanner();
}

const config = readConfig(args);
metrics.sendMetricsEvent(
"list versioned deployments",
{ json: args.json },
{
sendMetrics: config.send_metrics,
}
);

const accountId = await requireAuth(config);
const workerName = args.name ?? config.name;

if (workerName === undefined) {
throw new UserError(
'You need to provide a name of your worker. Either pass it as a cli arg with `--name <name>` or in your config file as `name = "<name>"`'
},
},
behaviour: {
printBanner: false,
},
handler: async function versionsDeploymentsListHandler(args, { config }) {
metrics.sendMetricsEvent(
"list versioned deployments",
{ json: args.json },
{
sendMetrics: config.send_metrics,
}
);
}

const deployments = (
await fetchLatestDeployments(accountId, workerName)
).sort((a, b) => a.created_on.localeCompare(b.created_on));

if (args.json) {
logRaw(JSON.stringify(deployments, null, 2));
return;
}
const accountId = await requireAuth(config);
const workerName = args.name ?? config.name;

const versionCache: VersionCache = new Map();
const versionIds = deployments.flatMap((d) =>
d.versions.map((v) => v.version_id)
);
await fetchVersions(accountId, workerName, versionCache, ...versionIds);

const formattedDeployments = deployments.map((deployment) => {
const formattedVersions = deployment.versions.map((traffic) => {
const version = versionCache.get(traffic.version_id);
assert(version);

const percentage = brandColor(`(${traffic.percentage}%)`);
const details = formatLabelledValues(
{
Created: new Date(version.metadata["created_on"]).toISOString(),
Tag: version.annotations?.["workers/tag"] || BLANK_INPUT,
Message: version.annotations?.["workers/message"] || BLANK_INPUT,
},
{
indentationCount: 4,
labelJustification: "right",
formatLabel: (label) => gray(label + ":"),
formatValue: (value) => gray(value),
}
if (workerName === undefined) {
throw new UserError(
'You need to provide a name for your Worker. Either pass it as a cli arg with `--name <name>` or in your configuration file as `name = "<name>"`'
);
}

return `${percentage} ${version.id}\n${details}`;
});
const deployments = (
await fetchLatestDeployments(accountId, workerName)
).sort((a, b) => a.created_on.localeCompare(b.created_on));

return formatLabelledValues({
// explicitly not outputting Deployment ID
Created: new Date(deployment.created_on).toISOString(),
Author: deployment.author_email,
Source: getDeploymentSource(deployment),
Message: deployment.annotations?.["workers/message"] || BLANK_INPUT,
"Version(s)": formattedVersions.join("\n\n"),
if (args.json) {
logRaw(JSON.stringify(deployments, null, 2));
return;
}

const versionCache: VersionCache = new Map();
const versionIds = deployments.flatMap((d) =>
d.versions.map((v) => v.version_id)
);
await fetchVersions(accountId, workerName, versionCache, ...versionIds);

const formattedDeployments = deployments.map((deployment) => {
const formattedVersions = deployment.versions.map((traffic) => {
const version = versionCache.get(traffic.version_id);
assert(version);

const percentage = brandColor(`(${traffic.percentage}%)`);
const details = formatLabelledValues(
{
Created: new Date(version.metadata["created_on"]).toISOString(),
Tag: version.annotations?.["workers/tag"] || BLANK_INPUT,
Message: version.annotations?.["workers/message"] || BLANK_INPUT,
},
{
indentationCount: 4,
labelJustification: "right",
formatLabel: (label) => gray(label + ":"),
formatValue: (value) => gray(value),
}
);

return `${percentage} ${version.id}\n${details}`;
});

return formatLabelledValues({
// explicitly not outputting Deployment ID
Created: new Date(deployment.created_on).toISOString(),
Author: deployment.author_email,
Source: getDeploymentSource(deployment),
Message: deployment.annotations?.["workers/message"] || BLANK_INPUT,
"Version(s)": formattedVersions.join("\n\n"),
});
});
});

logRaw(formattedDeployments.join("\n\n"));
}
logRaw(formattedDeployments.join("\n\n"));
},
});

export function getDeploymentSource(deployment: ApiDeployment) {
return getVersionSource({
Expand Down
Loading

0 comments on commit a55bc36

Please sign in to comment.