Skip to content

Commit

Permalink
implement versions view command
Browse files Browse the repository at this point in the history
  • Loading branch information
RamIdeas committed Mar 8, 2024
1 parent d845dbb commit 8f0da9e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/wrangler/src/metrics/send-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export type EventNames =
| "rollback deployments"
| "upload worker version"
| "deploy worker versions"
| "view worker version"
| "list worker versions";

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/wrangler/src/versions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { collectKeyValues } from "../utils/collectKeyValues";
import { versionsDeployHandler, versionsDeployOptions } from "./deploy";
import { versionsListHandler, versionsListOptions } from "./list";
import versionsUpload from "./upload";
import { versionsViewHandler, versionsViewOptions } from "./view";
import type { Config } from "../config";
import type {
CommonYargsArgv,
Expand Down Expand Up @@ -262,6 +263,12 @@ export default function registerVersionsSubcommands(
versionYargs: CommonYargsArgv
) {
versionYargs
.command(
"view <version-id>",
"View the details of a specific version of your Worker [beta]",
versionsViewOptions,
versionsViewHandler
)
.command(
"list",
"List the 10 most recent Versions of your Worker [beta]",
Expand Down
98 changes: 98 additions & 0 deletions packages/wrangler/src/versions/view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import path from "path";
import * as cli from "@cloudflare/cli";
import { fetchResult } from "../cfetch";
import { findWranglerToml, readConfig } from "../config";
import { UserError } from "../errors";
import * as metrics from "../metrics";
import { printWranglerBanner } from "../update-check";
import { requireAuth } from "../user";
import renderLabelledValues from "../utils/render-labelled-values";
import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../yargs-types";

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

export type VersionsViewArgs = StrictYargsOptionsToInterface<
typeof versionsViewOptions
>;

type UUID = string;
type VersionId = UUID;
type ApiVersion = {
id: VersionId;
number: number;
metadata: {
created_on: string;
modified_on: string;
source: "api" | string;
author_id: string;
author_email: string;
};
annotations?: Record<string, string> & {
"workers/triggered_by"?: "upload" | string;
"workers/message"?: string;
"workers/tag"?: string;
};
// other properties not typed as not used
};

export function versionsViewOptions(yargs: CommonYargsArgv) {
return yargs
.positional("version-id", {
describe: "The Worker Version ID to view",
type: "string",
requiresArg: true,
})
.option("name", {
describe: "Name of the worker",
type: "string",
requiresArg: true,
});
}

export async function versionsViewHandler(args: VersionsViewArgs) {
await printWranglerBanner();

const config = getConfig(args);
await metrics.sendMetricsEvent(
"view worker version",
{},
{
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 when deploying a worker. Either pass it as a cli arg with `--name <name>` or in your config file as `name = "<name>"`'
);
}

const version = await fetchResult<ApiVersion>(
`/accounts/${accountId}/workers/scripts/${workerName}/versions/${args.versionId}`
);

renderLabelledValues({
"Version ID:": version.id,
"Created:": new Date(version.metadata["created_on"]).toLocaleString(),
"Author:": version.metadata.author_email,
"Source:": version.metadata.source,
"Tag:": version.annotations?.["workers/tag"] ?? BLANK_INPUT,
"Message:": version.annotations?.["workers/message"] ?? BLANK_INPUT,
});
}

function getConfig(
args: Pick<VersionsViewArgs, "config" | "name" | "experimentalJsonConfig">
) {
const configPath =
args.config || (args.name && findWranglerToml(path.dirname(args.name)));
const config = readConfig(configPath, args);

return config;
}

0 comments on commit 8f0da9e

Please sign in to comment.