Skip to content

Commit

Permalink
Merge pull request #6 from liquid-labs/work-liquid-labs/s3-empty-buck…
Browse files Browse the repository at this point in the history
…et/5

Add quiet option
  • Loading branch information
zanerock authored Mar 1, 2024
2 parents d317f0e + b2d7fde commit 20f9a51
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 46 deletions.
125 changes: 89 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"homepage": "https://github.com/liquid-labs/s3-empty-bucket#readme",
"dependencies": {
"@aws-sdk/client-s3": "^3.525.0",
"@aws-sdk/credential-providers": "^3.525.0"
"@aws-sdk/credential-providers": "^3.525.0",
"command-line-args": "^5.2.1"
},
"devDependencies": {
"@liquid-labs/sdlc-resource-babel-and-rollup": "^1.0.0-alpha.8",
Expand Down
19 changes: 15 additions & 4 deletions src/cli/s3-empty-bucket.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { S3Client } from '@aws-sdk/client-s3'

import commandLineArgs from 'command-line-args'

import { getCredentials } from './lib/get-credentials'
import { emptyBucket } from '../lib/s3-empty-bucket'

const cliSpec = {
mainCommand : 's3-empty-bucket',
mainOptions : [
{ name : 'bucketName', defaultOption : true, description : 'The name of the bucket to empty.' },
{ name : 'profile', alias : 'p', description : 'The SSO profile to use.' },
{ name : 'quiet', alias : 'q', type : Boolean, description : 'Suppresses output.' }
]
}

const s3EmptyBucket = () => {
const bucketName = process.argv[2]
const ssoProfile = process.argv[3]
const options = commandLineArgs(cliSpec.mainOptions)
const { bucketName, profile, quiet } = options

const credentials = getCredentials({ ssoProfile })
const credentials = getCredentials({ ssoProfile : profile })
const s3Client = new S3Client({ credentials })

emptyBucket({ bucketName, s3Client })
emptyBucket({ bucketName, s3Client, verbose : !quiet })
}

export { s3EmptyBucket }
17 changes: 12 additions & 5 deletions src/lib/s3-empty-bucket.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { DeleteObjectsCommand, ListObjectsCommand } from '@aws-sdk/client-s3'

const emptyBucket = async ({ bucketName, s3Client }) => {
const emptyBucket = async ({ bucketName, s3Client, verbose }) => {
const objects = []
let marker, isTruncated
process.stdout.write('Cataloging files...\n')

maybeSay('Cataloging files...\n', verbose)
do {
const listObjectsCommand = new ListObjectsCommand({ Bucket : bucketName, Marker : marker })
const listObjectsResult = await s3Client.send(listObjectsCommand)
Expand All @@ -15,11 +16,11 @@ const emptyBucket = async ({ bucketName, s3Client }) => {
} while (isTruncated === true)

if (objects.length === 0) {
process.stdout.write('Bucket already empty.\n')
maybeSay('Bucket already empty.\n', verbose)
return
}

process.stdout.write(`Deleting ${objects.length} files...\n`)
maybeSay(`Deleting ${objects.length} files...\n`, verbose)

const input = {
Bucket : bucketName,
Expand All @@ -32,7 +33,13 @@ const emptyBucket = async ({ bucketName, s3Client }) => {
const deleteObjectsCommand = new DeleteObjectsCommand(input)
await s3Client.send(deleteObjectsCommand)

process.stdout.write('Done!\n')
maybeSay('Done!\n', verbose)
}

const maybeSay = (message, verbose) => {
if (verbose === true) {
process.stdout.write(message)
}
}

export { emptyBucket }

0 comments on commit 20f9a51

Please sign in to comment.