Skip to content

Commit

Permalink
r2 bucket lifecycle set
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Nov 21, 2024
1 parent 50c8fb5 commit f9e4fc8
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 83 deletions.
25 changes: 1 addition & 24 deletions packages/wrangler/src/r2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import "./sippy";
import "./notification";
import "./domain";
import "./public-dev-url";
import * as Lifecycle from "./lifecycle";
import type { CommonYargsArgv, SubHelp } from "../yargs-types";
import "./lifecycle";

defineNamespace({
command: "wrangler r2",
Expand All @@ -16,25 +15,3 @@ defineNamespace({
owner: "Product: R2",
},
});

export function r2(r2Yargs: CommonYargsArgv, subHelp: SubHelp) {
return r2Yargs
.command(subHelp)
.command("bucket", "Manage R2 buckets", (r2BucketYargs) => {
r2BucketYargs.demandCommand();

r2BucketYargs.command(
"lifecycle",
"Manage lifecycle rules for an R2 bucket",
(lifecycleYargs) => {
return lifecycleYargs.command(
"set <bucket>",
"Set the lifecycle configuration for an R2 bucket from a JSON file",
Lifecycle.SetOptions,
Lifecycle.SetHandler
);
}
);
return r2BucketYargs;
});
}
116 changes: 57 additions & 59 deletions packages/wrangler/src/r2/lifecycle.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { readConfig } from "../config";
import { defineCommand, defineNamespace } from "../core";
import { confirm, multiselect, prompt } from "../dialogs";
import { UserError } from "../errors";
import isInteractive from "../is-interactive";
import { logger } from "../logger";
import { readFileSync } from "../parse";
import { printWranglerBanner } from "../update-check";
import { requireAuth } from "../user";
import formatLabelledValues from "../utils/render-labelled-values";
import {
Expand All @@ -16,10 +14,6 @@ import {
putLifecycleRules,
tableFromLifecycleRulesResponse,
} from "./helpers";
import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../yargs-types";
import type { LifecycleRule } from "./helpers";

defineNamespace({
Expand Down Expand Up @@ -376,77 +370,81 @@ defineCommand({
},
});

export function SetOptions(yargs: CommonYargsArgv) {
return yargs
.positional("bucket", {
defineCommand({
command: "wrangler r2 bucket lifecycle set",
metadata: {
description:
"Set the lifecycle configuration for an R2 bucket from a JSON file",
status: "stable",
owner: "Product: R2",
},
positionalArgs: ["bucket"],
args: {
bucket: {
describe: "The name of the R2 bucket to set lifecycle configuration for",
type: "string",
demandOption: true,
})
.option("file", {
},
file: {
describe: "Path to the JSON file containing lifecycle configuration",
type: "string",
demandOption: true,
requiresArg: true,
})
.option("jurisdiction", {
},
jurisdiction: {
describe: "The jurisdiction where the bucket exists",
alias: "J",
requiresArg: true,
type: "string",
})
.option("force", {
},
force: {
describe: "Skip confirmation",
type: "boolean",
alias: "y",
default: false,
});
}

export async function SetHandler(
args: StrictYargsOptionsToInterface<typeof SetOptions>
) {
await printWranglerBanner();
const config = readConfig(args.config, args);
const accountId = await requireAuth(config);

const { bucket, file, jurisdiction, force } = args;
let lifecyclePolicy: { rules: LifecycleRule[] };
try {
lifecyclePolicy = JSON.parse(readFileSync(file));
} catch (e) {
if (e instanceof Error) {
},
},
async handler(args, { config }) {
const accountId = await requireAuth(config);

const { bucket, file, jurisdiction, force } = args;
let lifecyclePolicy: { rules: LifecycleRule[] };
try {
lifecyclePolicy = JSON.parse(readFileSync(file));
} catch (e) {
if (e instanceof Error) {
throw new UserError(
`Failed to read or parse the lifecycle configuration config file: '${e.message}'`
);
} else {
throw e;
}
}

if (!lifecyclePolicy.rules || !Array.isArray(lifecyclePolicy.rules)) {
throw new UserError(
`Failed to read or parse the lifecycle configuration config file: '${e.message}'`
"The lifecycle configuration file must contain a 'rules' array."
);
} else {
throw e;
}
}

if (!lifecyclePolicy.rules || !Array.isArray(lifecyclePolicy.rules)) {
throw new UserError(
"The lifecycle configuration file must contain a 'rules' array."
if (!force) {
const confirmedRemoval = await confirm(
`Are you sure you want to overwrite all existing lifecycle rules for bucket '${bucket}'?`
);
if (!confirmedRemoval) {
logger.log("Set cancelled.");
return;
}
}
logger.log(
`Setting lifecycle configuration (${lifecyclePolicy.rules.length} rules) for bucket '${bucket}'...`
);
}

if (!force) {
const confirmedRemoval = await confirm(
`Are you sure you want to overwrite all existing lifecycle rules for bucket '${bucket}'?`
await putLifecycleRules(
accountId,
bucket,
lifecyclePolicy.rules,
jurisdiction
);
if (!confirmedRemoval) {
logger.log("Set cancelled.");
return;
}
}
logger.log(
`Setting lifecycle configuration (${lifecyclePolicy.rules.length} rules) for bucket '${bucket}'...`
);
await putLifecycleRules(
accountId,
bucket,
lifecyclePolicy.rules,
jurisdiction
);
logger.log(`✨ Set lifecycle configuration for bucket '${bucket}'.`);
}
logger.log(`✨ Set lifecycle configuration for bucket '${bucket}'.`);
},
});

0 comments on commit f9e4fc8

Please sign in to comment.