diff --git a/src/commands/aws/s3/clear_prints/clear_prints.ts b/src/commands/aws/s3/clear_prints/clear_prints.ts new file mode 100644 index 0000000..4867b7e --- /dev/null +++ b/src/commands/aws/s3/clear_prints/clear_prints.ts @@ -0,0 +1,70 @@ +import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; +import chalk = require('chalk'); +import { confirmMessage, defaultMenuSettings } from '../../../../globals'; +const { AutoComplete, Confirm } = require('enquirer'); +const { spawn } = require('child_process'); + +module.exports = { + name: 'clear-prints', + alias: ['cp'], + description: 'Clear prints-dev bucket on S3 (a s3 cp)', + hidden: false, + run: async (toolbox: GluegunMenuToolbox) => { + const { print, system, strings } = toolbox; + + // List all the folders on root level in bucket photowall-prints-dev + const allFiles = await system.run( + `aws s3api list-objects-v2 --bucket photowall-prints-dev --output json` + ); + + const allFilesArray = JSON.parse(allFiles); + + // Organize all the files in different folder if the folder name starts with station + const stations = []; + for (const file of allFilesArray.Contents) { + if (file.Key.startsWith('station')) { + // Get the station name + const stationName = file.Key.split('/')[0]; + if (!stations[stationName]) { + stations[stationName] = []; + } + // Then push the file into the station + stations[stationName].push(file); + } + } + + // Take all the station and create another list with the station and the number of files + const stationList = []; + for (const station in stations) { + stationList.push(`${station} (${stations[station].length} files)`); + } + + // Now show these in a enquiere prompt where the user can search and choose a folder + const prompt = new AutoComplete({ + message: 'Choose a folder', + choices: stationList, + }); + const folder = await prompt.run(); + const stationName = folder.split(' ')[0]; + console.log(`stationName`, stationName); + + // Ask the user if he wants to delete all the files in that station + const confirm = new Confirm({ + message: `Do you want to delete all the files in ${chalk.yellow( + stationName + )}?`, + }); + const deleteAll = await confirm.run(); + + if (deleteAll) { + // Delete all the files in that station + for (const file of stations[stationName]) { + await system.run(`aws s3 rm s3://photowall-prints-dev/${file.Key}`); + } + } + + if (toolbox.fromMenu()) { + await toolbox.menu.showMenu('aws s3', defaultMenuSettings); + } + }, +}; diff --git a/src/commands/aws/s3/s3.ts b/src/commands/aws/s3/s3.ts new file mode 100644 index 0000000..872036f --- /dev/null +++ b/src/commands/aws/s3/s3.ts @@ -0,0 +1,12 @@ +import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; +import { defaultMenuSettings } from '../../../globals'; + +module.exports = { + name: 's3', + alias: ['s3'], + description: 'Working with S3 on aws (a s3)', + hidden: false, + run: async (toolbox: GluegunMenuToolbox) => { + await toolbox.menu.showMenu('aws s3', defaultMenuSettings); + }, +};