@@ -84,15 +84,20 @@ export const r2BucketLockAddCommand = createCommand({
8484 type : "string" ,
8585 requiresArg : true ,
8686 } ,
87- "lock -days" : {
88- describe : "Number of days after which objects expire " ,
87+ "retention -days" : {
88+ describe : "Number of days which objects will be retained for " ,
8989 type : "number" ,
90- conflicts : "lock -date",
90+ conflicts : [ "retention -date", "retention-indefinite" ] ,
9191 } ,
92- "lock -date" : {
93- describe : "Date after which objects expire (YYYY-MM-DD)" ,
92+ "retention -date" : {
93+ describe : "Date after which objects will be retained until (YYYY-MM-DD)" ,
9494 type : "string" ,
95- conflicts : "lock-days" ,
95+ conflicts : [ "retention-days" , "retention-indefinite" ] ,
96+ } ,
97+ "retention-indefinite" : {
98+ describe : "Retain objects indefinitely" ,
99+ type : "boolean" ,
100+ conflicts : [ "retention-date" , "retention-days" ] ,
96101 } ,
97102 jurisdiction : {
98103 describe : "The jurisdiction where the bucket exists" ,
@@ -108,7 +113,16 @@ export const r2BucketLockAddCommand = createCommand({
108113 } ,
109114 } ,
110115 async handler (
111- { bucket, lockDays, lockDate, jurisdiction, force, id, prefix } ,
116+ {
117+ bucket,
118+ retentionDays,
119+ retentionDate,
120+ retentionIndefinite,
121+ jurisdiction,
122+ force,
123+ id,
124+ prefix,
125+ } ,
112126 { config }
113127 ) {
114128 const accountId = await requireAuth ( config ) ;
@@ -138,7 +152,8 @@ export const r2BucketLockAddCommand = createCommand({
138152 if ( prefix === "" ) {
139153 const confirmedAdd = await confirm (
140154 `Are you sure you want to add lock rule '${ id } ' to bucket '${ bucket } ' without a prefix? ` +
141- `The lock rule will apply to all objects in your bucket.`
155+ `The lock rule will apply to all objects in your bucket.` ,
156+ { defaultValue : false }
142157 ) ;
143158 if ( ! confirmedAdd ) {
144159 logger . log ( "Add cancelled." ) ;
@@ -151,57 +166,72 @@ export const r2BucketLockAddCommand = createCommand({
151166 newRule . prefix = prefix ;
152167 }
153168
154- if ( lockDays === undefined && lockDate === undefined && ! force ) {
155- const confirmIndefinite = await confirm (
156- `Are you sure you want to add lock rule '${ id } ' to bucket '${ bucket } ' without an expiration? ` +
169+ if (
170+ retentionDays === undefined &&
171+ retentionDate === undefined &&
172+ retentionIndefinite === undefined &&
173+ ! force
174+ ) {
175+ retentionIndefinite = await confirm (
176+ `Are you sure you want to add lock rule '${ id } ' to bucket '${ bucket } ' without retention? ` +
157177 `The lock rule will apply to all matching objects indefinitely.` ,
158- { defaultValue : true }
178+ { defaultValue : false }
159179 ) ;
160- if ( confirmIndefinite !== true ) {
180+ if ( retentionIndefinite !== true ) {
161181 logger . log ( "Add cancelled." ) ;
162182 return ;
163183 }
164184 }
165185
166- if ( lockDays !== undefined ) {
167- if ( ! isNaN ( lockDays ) ) {
168- if ( lockDays > 0 ) {
169- const conditionDaysValue = Number ( lockDays ) * 86400 ; // Convert days to seconds
186+ if ( retentionDays !== undefined ) {
187+ if ( ! isNaN ( retentionDays ) ) {
188+ if ( retentionDays > 0 ) {
189+ const conditionDaysValue = Number ( retentionDays ) * 86400 ; // Convert days to seconds
170190 newRule . condition = {
171191 type : "Age" ,
172192 maxAgeSeconds : conditionDaysValue ,
173193 } ;
174194 } else {
175- throw new UserError ( `Days must be a positive number: ${ lockDays } ` , {
176- telemetryMessage : "Lock days not a positive number." ,
177- } ) ;
195+ throw new UserError (
196+ `Days must be a positive number: ${ retentionDays } ` ,
197+ {
198+ telemetryMessage : "Retention days not a positive number." ,
199+ }
200+ ) ;
178201 }
179202 } else {
180203 throw new UserError ( `Days must be a number.` , {
181- telemetryMessage : "Lock days not a positive number." ,
204+ telemetryMessage : "Retention days not a positive number." ,
182205 } ) ;
183206 }
184- } else if ( lockDate !== undefined ) {
185- if ( isValidDate ( lockDate ) ) {
186- const date = new Date ( `${ lockDate } T00:00:00.000Z` ) ;
207+ } else if ( retentionDate !== undefined ) {
208+ if ( isValidDate ( retentionDate ) ) {
209+ const date = new Date ( `${ retentionDate } T00:00:00.000Z` ) ;
187210 const conditionDateValue = date . toISOString ( ) ;
188211 newRule . condition = {
189212 type : "Date" ,
190213 date : conditionDateValue ,
191214 } ;
192215 } else {
193216 throw new UserError (
194- `Date must be a valid date in the YYYY-MM-DD format: ${ String ( lockDate ) } ` ,
217+ `Date must be a valid date in the YYYY-MM-DD format: ${ String ( retentionDate ) } ` ,
195218 {
196219 telemetryMessage :
197- "Lock date not a valid date in the YYYY-MM-DD format." ,
220+ "Retention date not a valid date in the YYYY-MM-DD format." ,
198221 }
199222 ) ;
200223 }
201- } else {
224+ } else if (
225+ retentionIndefinite !== undefined &&
226+ retentionIndefinite === true
227+ ) {
202228 newRule . condition = {
203229 type : "Indefinite" ,
204230 } ;
231+ } else {
232+ throw new UserError ( `Retention must be specified.` , {
233+ telemetryMessage : "Lock retention not specified." ,
234+ } ) ;
205235 }
206236 rules . push ( newRule ) ;
207237 logger . log ( `Adding lock rule '${ id } ' to bucket '${ bucket } '...` ) ;
0 commit comments