added update (#23)

* added update

* update parameters
This commit is contained in:
Arwid Thornström
2024-10-24 11:13:19 +02:00
committed by GitHub
parent e1a204e73f
commit f2e34c477f
@@ -0,0 +1,94 @@
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
import chalk = require('chalk');
import { defaultMenuSettings, sleep } from '../../../../globals';
import * as fs from 'fs';
const { AutoComplete } = require('enquirer');
module.exports = {
name: 'update',
alias: ['u'],
description: 'Update an existing item in parameter store (a ps u)',
hidden: false,
run: async (toolbox: GluegunMenuToolbox) => {
const { print, prompt, system, filesystem } = toolbox;
// Read the .pwcli_parametercache file
const cacheFilePath = '/tmp/.pwcli_parametercache';
let parameters = [];
if (fs.existsSync(cacheFilePath)) {
const cacheContent = await filesystem.read(cacheFilePath);
parameters = JSON.parse(cacheContent);
} else {
print.error(
chalk.red(
'Cache file not found. Please run "a ps l" first to populate the cache.'
)
);
return;
}
const autoComplete = new AutoComplete({
name: 'parameterNames',
message: 'Choose a parameter',
limit: 30,
initial: 10,
choices: parameters,
});
const parameterName = await autoComplete.run();
print.info(parameterName);
const parameterValue = await prompt.ask({
type: 'input',
name: 'value',
message: `New value for [${chalk.yellow(parameterName)}]:`,
});
print.info(
`Parameter: ${chalk.yellow(parameterName)}, New Value: ${chalk.green(
parameterValue.value
)}`
);
const secureParameter = await prompt.confirm(
'Should this parameter be secure?'
);
let parameterType = 'String';
if (secureParameter) {
parameterType = 'SecureString';
}
const confirmUpdate = await prompt.confirm(
`Update [${chalk.yellow(parameterName)}] with value [${chalk.green(
parameterValue.value
)}] as ${chalk.red(parameterType)}?`
);
if (confirmUpdate) {
try {
const updateResult = await system.run(
`aws ssm put-parameter --name "${parameterName}" --value "${parameterValue.value}" --type ${parameterType} --overwrite`
);
print.info(updateResult);
print.success(
chalk.green(`Parameter "${parameterName}" updated successfully.`)
);
} catch (error) {
print.error(
chalk.red(
`An error occurred while updating the parameter: ${error.message}`
)
);
return;
}
} else {
print.info('Parameter update cancelled.');
}
if (toolbox.fromMenu()) {
await toolbox.menu.showMenu('aws parameterstore', defaultMenuSettings);
}
},
};