added sync command (#25)

This commit is contained in:
Arwid Thornström
2025-03-25 08:55:14 +01:00
committed by GitHub
parent 49deffc8ba
commit 5a12a0d214
2 changed files with 151 additions and 0 deletions
+14
View File
@@ -6,3 +6,17 @@ You often have the address in your .pgpass
Sync the downloaded file to your localhost with 4 cores
`./pwdb sync [address] localhost -j 4 -p`
To get a production db with grants
If something is wrong with the db. Like you get errors from API2.
Then you open postgres console with `psql photowall` and run
```
REVOKE ALL ON SCHEMA public FROM public;
GRANT USAGE ON SCHEMA public TO photowall;
GRANT SELECT, UPDATE, INSERT, DELETE ON ALL TABLES IN SCHEMA public TO photowall;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO photowall;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLES TO photowall;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT USAGE, SELECT ON SEQUENCES TO photowall;
```
@@ -0,0 +1,137 @@
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
import chalk = require('chalk');
import { defaultMenuSettings } from '../../../../globals';
const { MultiSelect, Confirm } = require('enquirer');
module.exports = {
name: 'Sync Images on S3',
alias: ['si'],
description: 'Sync images between S3 buckets (a s3 si)',
hidden: false,
run: async (toolbox: GluegunMenuToolbox) => {
const { system, print } = toolbox;
// Define the folder options
const folderOptions = [
'products',
'interiors',
'interior-images',
'articles',
];
// Ask the user to select one or many folders
const prompt = new MultiSelect({
name: 'folders',
message:
'Select folder(s) to sync (use space to select, enter to confirm)',
choices: folderOptions,
result(names) {
return this.map(names);
},
});
let selectedFolders;
try {
selectedFolders = await prompt.run();
} catch (error) {
print.error('Selection cancelled');
if (toolbox.fromMenu()) {
await toolbox.menu.showMenu('aws s3', defaultMenuSettings);
}
return;
}
if (Object.keys(selectedFolders).length === 0) {
print.info('No folders selected');
if (toolbox.fromMenu()) {
await toolbox.menu.showMenu('aws s3', defaultMenuSettings);
}
return;
}
// Display selected folders
print.info('\nSelected folders:');
Object.keys(selectedFolders).forEach((folder) => {
print.info(`- ${chalk.yellow(folder)}`);
});
// Ask if the user wants to use --dryrun flag for all commands
const dryRunPrompt = new Confirm({
name: 'dryrun',
message: `Do you want to use the ${chalk.cyan(
'--dryrun'
)} flag for all commands?`,
});
let useDryRun = false;
try {
useDryRun = await dryRunPrompt.run();
} catch (error) {
print.error('Operation cancelled');
if (toolbox.fromMenu()) {
await toolbox.menu.showMenu('aws s3', defaultMenuSettings);
}
return;
}
// Process each folder with individual confirmation
let completedSyncs = 0;
for (const folder of Object.keys(selectedFolders)) {
// Create the command with or without dryrun flag
const dryRunFlag = useDryRun ? ' --dryrun' : '';
const cmd = `aws s3 sync s3://photowall-images/${folder}/ s3://photowall-images-dev/${folder}/${dryRunFlag}`;
// Show the command that will be executed
print.info(`\nCommand for ${chalk.yellow(folder)}:`);
print.info(chalk.cyan(cmd));
// Ask for confirmation to run this specific command
const confirmPrompt = new Confirm({
name: 'confirm',
message: `Do you want to run this command for ${chalk.yellow(folder)}?`,
});
let runThisCommand;
try {
runThisCommand = await confirmPrompt.run();
} catch (error) {
print.info(`Skipping ${chalk.yellow(folder)}`);
continue; // Skip to the next folder
}
if (!runThisCommand) {
print.info(`Skipping ${chalk.yellow(folder)}`);
continue; // Skip to the next folder
}
// Run the command since it was confirmed
print.info(`\nSyncing ${chalk.yellow(folder)}...`);
// Run command and capture output
try {
const output = await system.run(cmd, { trim: true });
// Display command output
print.info(chalk.green('\nCommand output:'));
if (output && output.length > 0) {
print.info(output);
} else {
print.info('No output (command completed successfully)');
}
completedSyncs++;
} catch (error) {
print.error(`Error executing command: ${error.message || error}`);
}
}
if (completedSyncs > 0) {
print.success(`\nCompleted ${completedSyncs} sync command(s)`);
} else {
print.info('\nNo sync commands were executed');
}
if (toolbox.fromMenu()) {
await toolbox.menu.showMenu('aws s3', defaultMenuSettings);
}
},
};