added prints bucket commands

This commit is contained in:
Arwid Thornström
2025-01-22 09:41:06 +01:00
parent 4bb789be7c
commit 49deffc8ba
4 changed files with 113 additions and 102 deletions
@@ -1,8 +1,8 @@
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
import chalk = require('chalk');
import { confirmMessage, defaultMenuSettings } from '../../../../globals';
import { defaultMenuSettings } from '../../../../globals';
import { createPrintBucketStationList } from '../../../../services/aws_s3';
const { AutoComplete, Confirm } = require('enquirer');
const { spawn } = require('child_process');
module.exports = {
name: 'clear-prints',
@@ -10,43 +10,20 @@ module.exports = {
description: 'Clear prints-dev bucket on S3 (a s3 cp)',
hidden: false,
run: async (toolbox: GluegunMenuToolbox) => {
const { print, system, strings } = toolbox;
const { system } = 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 stationListDev = await createPrintBucketStationList(
toolbox,
'photowall-prints-dev'
);
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,
choices: stationListDev.stationNames,
});
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({
@@ -58,7 +35,7 @@ module.exports = {
if (deleteAll) {
// Delete all the files in that station
for (const file of stations[stationName]) {
for (const file of stationListDev.stationFiles[stationName]) {
await system.run(`aws s3 rm s3://photowall-prints-dev/${file.Key}`);
}
}