Skip to content

Commit f9e4fc8

Browse files
committed
r2 bucket lifecycle set
1 parent 50c8fb5 commit f9e4fc8

File tree

2 files changed

+58
-83
lines changed

2 files changed

+58
-83
lines changed

packages/wrangler/src/r2/index.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import "./sippy";
55
import "./notification";
66
import "./domain";
77
import "./public-dev-url";
8-
import * as Lifecycle from "./lifecycle";
9-
import type { CommonYargsArgv, SubHelp } from "../yargs-types";
8+
import "./lifecycle";
109

1110
defineNamespace({
1211
command: "wrangler r2",
@@ -16,25 +15,3 @@ defineNamespace({
1615
owner: "Product: R2",
1716
},
1817
});
19-
20-
export function r2(r2Yargs: CommonYargsArgv, subHelp: SubHelp) {
21-
return r2Yargs
22-
.command(subHelp)
23-
.command("bucket", "Manage R2 buckets", (r2BucketYargs) => {
24-
r2BucketYargs.demandCommand();
25-
26-
r2BucketYargs.command(
27-
"lifecycle",
28-
"Manage lifecycle rules for an R2 bucket",
29-
(lifecycleYargs) => {
30-
return lifecycleYargs.command(
31-
"set <bucket>",
32-
"Set the lifecycle configuration for an R2 bucket from a JSON file",
33-
Lifecycle.SetOptions,
34-
Lifecycle.SetHandler
35-
);
36-
}
37-
);
38-
return r2BucketYargs;
39-
});
40-
}

packages/wrangler/src/r2/lifecycle.ts

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { readConfig } from "../config";
21
import { defineCommand, defineNamespace } from "../core";
32
import { confirm, multiselect, prompt } from "../dialogs";
43
import { UserError } from "../errors";
54
import isInteractive from "../is-interactive";
65
import { logger } from "../logger";
76
import { readFileSync } from "../parse";
8-
import { printWranglerBanner } from "../update-check";
97
import { requireAuth } from "../user";
108
import formatLabelledValues from "../utils/render-labelled-values";
119
import {
@@ -16,10 +14,6 @@ import {
1614
putLifecycleRules,
1715
tableFromLifecycleRulesResponse,
1816
} from "./helpers";
19-
import type {
20-
CommonYargsArgv,
21-
StrictYargsOptionsToInterface,
22-
} from "../yargs-types";
2317
import type { LifecycleRule } from "./helpers";
2418

2519
defineNamespace({
@@ -376,77 +370,81 @@ defineCommand({
376370
},
377371
});
378372

379-
export function SetOptions(yargs: CommonYargsArgv) {
380-
return yargs
381-
.positional("bucket", {
373+
defineCommand({
374+
command: "wrangler r2 bucket lifecycle set",
375+
metadata: {
376+
description:
377+
"Set the lifecycle configuration for an R2 bucket from a JSON file",
378+
status: "stable",
379+
owner: "Product: R2",
380+
},
381+
positionalArgs: ["bucket"],
382+
args: {
383+
bucket: {
382384
describe: "The name of the R2 bucket to set lifecycle configuration for",
383385
type: "string",
384386
demandOption: true,
385-
})
386-
.option("file", {
387+
},
388+
file: {
387389
describe: "Path to the JSON file containing lifecycle configuration",
388390
type: "string",
389391
demandOption: true,
390392
requiresArg: true,
391-
})
392-
.option("jurisdiction", {
393+
},
394+
jurisdiction: {
393395
describe: "The jurisdiction where the bucket exists",
394396
alias: "J",
395397
requiresArg: true,
396398
type: "string",
397-
})
398-
.option("force", {
399+
},
400+
force: {
399401
describe: "Skip confirmation",
400402
type: "boolean",
401403
alias: "y",
402404
default: false,
403-
});
404-
}
405-
406-
export async function SetHandler(
407-
args: StrictYargsOptionsToInterface<typeof SetOptions>
408-
) {
409-
await printWranglerBanner();
410-
const config = readConfig(args.config, args);
411-
const accountId = await requireAuth(config);
412-
413-
const { bucket, file, jurisdiction, force } = args;
414-
let lifecyclePolicy: { rules: LifecycleRule[] };
415-
try {
416-
lifecyclePolicy = JSON.parse(readFileSync(file));
417-
} catch (e) {
418-
if (e instanceof Error) {
405+
},
406+
},
407+
async handler(args, { config }) {
408+
const accountId = await requireAuth(config);
409+
410+
const { bucket, file, jurisdiction, force } = args;
411+
let lifecyclePolicy: { rules: LifecycleRule[] };
412+
try {
413+
lifecyclePolicy = JSON.parse(readFileSync(file));
414+
} catch (e) {
415+
if (e instanceof Error) {
416+
throw new UserError(
417+
`Failed to read or parse the lifecycle configuration config file: '${e.message}'`
418+
);
419+
} else {
420+
throw e;
421+
}
422+
}
423+
424+
if (!lifecyclePolicy.rules || !Array.isArray(lifecyclePolicy.rules)) {
419425
throw new UserError(
420-
`Failed to read or parse the lifecycle configuration config file: '${e.message}'`
426+
"The lifecycle configuration file must contain a 'rules' array."
421427
);
422-
} else {
423-
throw e;
424428
}
425-
}
426429

427-
if (!lifecyclePolicy.rules || !Array.isArray(lifecyclePolicy.rules)) {
428-
throw new UserError(
429-
"The lifecycle configuration file must contain a 'rules' array."
430+
if (!force) {
431+
const confirmedRemoval = await confirm(
432+
`Are you sure you want to overwrite all existing lifecycle rules for bucket '${bucket}'?`
433+
);
434+
if (!confirmedRemoval) {
435+
logger.log("Set cancelled.");
436+
return;
437+
}
438+
}
439+
logger.log(
440+
`Setting lifecycle configuration (${lifecyclePolicy.rules.length} rules) for bucket '${bucket}'...`
430441
);
431-
}
432-
433-
if (!force) {
434-
const confirmedRemoval = await confirm(
435-
`Are you sure you want to overwrite all existing lifecycle rules for bucket '${bucket}'?`
442+
await putLifecycleRules(
443+
accountId,
444+
bucket,
445+
lifecyclePolicy.rules,
446+
jurisdiction
436447
);
437-
if (!confirmedRemoval) {
438-
logger.log("Set cancelled.");
439-
return;
440-
}
441-
}
442-
logger.log(
443-
`Setting lifecycle configuration (${lifecyclePolicy.rules.length} rules) for bucket '${bucket}'...`
444-
);
445-
await putLifecycleRules(
446-
accountId,
447-
bucket,
448-
lifecyclePolicy.rules,
449-
jurisdiction
450-
);
451-
logger.log(`✨ Set lifecycle configuration for bucket '${bucket}'.`);
452-
}
448+
logger.log(`✨ Set lifecycle configuration for bucket '${bucket}'.`);
449+
},
450+
});

0 commit comments

Comments
 (0)