From 9edc91debc6587fc3d87d13becfd1230efe3f34d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arwid=20Thornstro=CC=88m?= Date: Wed, 2 Oct 2024 11:01:07 +0200 Subject: [PATCH] added list and create for parameter store --- src/commands/aws/logs/logs.ts | 2 +- .../aws/parameterstore/create/create.ts | 91 +++++++++++++++++++ src/commands/aws/parameterstore/list/list.ts | 91 +++++++++++++++++++ .../aws/parameterstore/parameterstore.ts | 85 +---------------- 4 files changed, 186 insertions(+), 83 deletions(-) create mode 100644 src/commands/aws/parameterstore/create/create.ts create mode 100644 src/commands/aws/parameterstore/list/list.ts diff --git a/src/commands/aws/logs/logs.ts b/src/commands/aws/logs/logs.ts index 8418c45..6a9c3da 100644 --- a/src/commands/aws/logs/logs.ts +++ b/src/commands/aws/logs/logs.ts @@ -7,7 +7,7 @@ const { spawn } = require('child_process'); module.exports = { name: 'logs', alias: ['l'], - description: 'Tail a log in cloudwatch (l) (WIP)', + description: 'Tail a log in cloudwatch (a l) (WIP)', hidden: false, run: async (toolbox: GluegunMenuToolbox) => { const { print, system, strings } = toolbox; diff --git a/src/commands/aws/parameterstore/create/create.ts b/src/commands/aws/parameterstore/create/create.ts new file mode 100644 index 0000000..d8b0c14 --- /dev/null +++ b/src/commands/aws/parameterstore/create/create.ts @@ -0,0 +1,91 @@ +import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; +import chalk = require('chalk'); +import { defaultMenuSettings, sleep } from '../../../../globals'; + +module.exports = { + name: 'create', + alias: ['c'], + description: 'Create a new item in parameter store (a ps c)', + hidden: false, + run: async (toolbox: GluegunMenuToolbox) => { + const { prompt, system, strings, print } = toolbox; + + const parameterName = await prompt.ask({ + type: 'input', + name: 'name', + message: 'Name:', + validate: (value) => { + if (!value) return 'Parameter name is required'; + return true; + }, + }); + + // Check if the parameter already exists + try { + const checkResult = JSON.parse( + strings.trim( + await system.run( + `aws ssm get-parameter --name "${parameterName.name}" --with-decryption` + ) + ) + ); + + if (checkResult.Parameter) { + print.error( + chalk.red(`Parameter "${parameterName.name}" already exists.`) + ); + return; + } + } catch (error) { + // If the parameter doesn't exist, AWS CLI will throw an error, which we can ignore + if (!error.message.includes('ParameterNotFound')) { + print.error( + chalk.red( + `An error occurred while checking the parameter: ${error.message}` + ) + ); + return; + } + } + + const parameterValue = await prompt.ask({ + type: 'input', + name: 'value', + message: 'Value:', + validate: (value) => { + if (!value) return 'Parameter value is required'; + return true; + }, + }); + + if (!parameterValue.value.trim()) { + print.error(chalk.red('Parameter value cannot be empty.')); + return; + } + + const confirmCreate = await prompt.confirm( + `Create [${chalk.yellow(parameterName.name)}] with value [${chalk.yellow( + parameterValue.value + )}]?` + ); + + if (confirmCreate) { + try { + const createResult = await system.run( + `aws ssm put-parameter --name "${parameterName.name}" --value "${parameterValue.value}" --type String` + ); + print.success( + chalk.green(`Parameter "${parameterName.name}" created successfully.`) + ); + } catch (error) { + print.error(chalk.red(`Failed to create parameter: ${error.message}`)); + } + } else { + print.info('Parameter creation cancelled.'); + } + + if (toolbox.fromMenu()) { + await toolbox.menu.showMenu('aws parameterstore', defaultMenuSettings); + } + }, +}; diff --git a/src/commands/aws/parameterstore/list/list.ts b/src/commands/aws/parameterstore/list/list.ts new file mode 100644 index 0000000..212afed --- /dev/null +++ b/src/commands/aws/parameterstore/list/list.ts @@ -0,0 +1,91 @@ +import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; +import chalk = require('chalk'); +import { defaultMenuSettings, sleep } from '../../../../globals'; +const { AutoComplete } = require('enquirer'); + +module.exports = { + name: 'list', + alias: ['l'], + description: 'List all parameters in parameter store (a ps l)', + hidden: false, + run: async (toolbox: GluegunMenuToolbox) => { + const { system, strings, print, filesystem } = toolbox; + + // Check for cache in /tmp folder + const cache = filesystem.read('/tmp/.pwcli_parametercache'); + const cacheValues = !cache ? [] : JSON.parse(cache); + const valueNames = [...cacheValues]; + + let nextToken = ''; + if (valueNames.length === 0) { + const spinner = print.spin( + `Downloading parameter names (${chalk.red('0')})` + ); + while (nextToken !== null) { + const values = JSON.parse( + strings.trim( + await system.run( + `aws ssm describe-parameters --output json --max-items 50 ${ + nextToken !== '' ? `--starting-token ${nextToken}` : '' + }` + ) + ) + ); + + await sleep(500); + valueNames.push( + ...values.Parameters.map(({ Name }) => { + return Name; + }) + ); + nextToken = values.NextToken || null; + spinner.text = `Downloading parameter names (${chalk.red( + valueNames.length + )})`; + } + + spinner.text = 'Saving parameter names to /tmp/.pwcli_parametercache'; + + filesystem.file('/tmp/.pwcli_parametercache', { + mode: '600', + content: JSON.stringify(valueNames), + }); + await sleep(2000); + + spinner.stop(); + } + + let parameterName = ''; + while (parameterName !== 'exit') { + const prompt = new AutoComplete({ + name: 'parameterNames', + message: 'Choose a parameter (esc to exit)', + limit: 30, + initial: 10, + choices: valueNames, + }); + + try { + const parameterName = await prompt.run(); + const parameterValue = JSON.parse( + strings.trim( + await system.run( + `aws ssm get-parameter --name ${parameterName} --with-decryption` + ) + ) + ); + print.success( + `${chalk.green(parameterName)} value is [${chalk.yellow( + parameterValue.Parameter.Value + )}]` + ); + } catch (e) { + parameterName = 'exit'; + } + } + + if (toolbox.fromMenu()) { + await toolbox.menu.showMenu('aws parameterstore', defaultMenuSettings); + } + }, +}; diff --git a/src/commands/aws/parameterstore/parameterstore.ts b/src/commands/aws/parameterstore/parameterstore.ts index d0ff3b3..19f20d5 100644 --- a/src/commands/aws/parameterstore/parameterstore.ts +++ b/src/commands/aws/parameterstore/parameterstore.ts @@ -1,91 +1,12 @@ import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; -import chalk = require('chalk'); -import { defaultMenuSettings, sleep } from '../../../globals'; -const { AutoComplete } = require('enquirer'); +import { defaultMenuSettings } from '../../../globals'; module.exports = { name: 'parameterstore', alias: ['ps'], - description: 'Parameter store functions (ps)', + description: 'Working with parameter store on aws (a ps)', hidden: false, run: async (toolbox: GluegunMenuToolbox) => { - const { system, strings, print, filesystem } = toolbox; - - // Check for cache in /tmp folder - const cache = filesystem.read('/tmp/.pwcli_parametercache'); - const cacheValues = !cache ? [] : JSON.parse(cache); - const valueNames = [...cacheValues]; - - let nextToken = ''; - if (valueNames.length === 0) { - const spinner = print.spin( - `Downloading parameter names (${chalk.red('0')})` - ); - while (nextToken !== null) { - const values = JSON.parse( - strings.trim( - await system.run( - `aws ssm describe-parameters --output json --max-items 50 ${ - nextToken !== '' ? `--starting-token ${nextToken}` : '' - }` - ) - ) - ); - - await sleep(500); - valueNames.push( - ...values.Parameters.map(({ Name }) => { - return Name; - }) - ); - nextToken = values.NextToken || null; - spinner.text = `Downloading parameter names (${chalk.red( - valueNames.length - )})`; - } - - spinner.text = 'Saving parameter names to /tmp/.pwcli_parametercache'; - - filesystem.file('/tmp/.pwcli_parametercache', { - mode: '600', - content: JSON.stringify(valueNames), - }); - await sleep(2000); - - spinner.stop(); - } - - let parameterName = ''; - while (parameterName !== 'exit') { - const prompt = new AutoComplete({ - name: 'parameterNames', - message: 'Choose a parameter (esc to exit)', - limit: 30, - initial: 10, - choices: valueNames, - }); - - try { - const parameterName = await prompt.run(); - const parameterValue = JSON.parse( - strings.trim( - await system.run( - `aws ssm get-parameter --name ${parameterName} --with-decryption` - ) - ) - ); - print.success( - `${chalk.green(parameterName)} value is [${chalk.yellow( - parameterValue.Parameter.Value - )}]` - ); - } catch (e) { - parameterName = 'exit'; - } - } - - if (toolbox.fromMenu()) { - await toolbox.menu.showMenu('aws', defaultMenuSettings); - } + await toolbox.menu.showMenu('aws parameterstore', defaultMenuSettings); }, };