From f2e34c477f772d856dd77ba8a784ba75dfd11412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arwid=20Thornstr=C3=B6m?= Date: Thu, 24 Oct 2024 11:13:19 +0200 Subject: [PATCH] added update (#23) * added update * update parameters --- .../aws/parameterstore/update/update.ts | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 src/commands/aws/parameterstore/update/update.ts diff --git a/src/commands/aws/parameterstore/update/update.ts b/src/commands/aws/parameterstore/update/update.ts new file mode 100644 index 0000000..d97e5a5 --- /dev/null +++ b/src/commands/aws/parameterstore/update/update.ts @@ -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); + } + }, +};