added list and create for parameter store
This commit is contained in:
@@ -7,7 +7,7 @@ const { spawn } = require('child_process');
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'logs',
|
name: 'logs',
|
||||||
alias: ['l'],
|
alias: ['l'],
|
||||||
description: 'Tail a log in cloudwatch (l) (WIP)',
|
description: 'Tail a log in cloudwatch (a l) (WIP)',
|
||||||
hidden: false,
|
hidden: false,
|
||||||
run: async (toolbox: GluegunMenuToolbox) => {
|
run: async (toolbox: GluegunMenuToolbox) => {
|
||||||
const { print, system, strings } = toolbox;
|
const { print, system, strings } = toolbox;
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
@@ -1,91 +1,12 @@
|
|||||||
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
|
import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu';
|
||||||
import chalk = require('chalk');
|
import { defaultMenuSettings } from '../../../globals';
|
||||||
import { defaultMenuSettings, sleep } from '../../../globals';
|
|
||||||
const { AutoComplete } = require('enquirer');
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'parameterstore',
|
name: 'parameterstore',
|
||||||
alias: ['ps'],
|
alias: ['ps'],
|
||||||
description: 'Parameter store functions (ps)',
|
description: 'Working with parameter store on aws (a ps)',
|
||||||
hidden: false,
|
hidden: false,
|
||||||
run: async (toolbox: GluegunMenuToolbox) => {
|
run: async (toolbox: GluegunMenuToolbox) => {
|
||||||
const { system, strings, print, filesystem } = toolbox;
|
await toolbox.menu.showMenu('aws parameterstore', defaultMenuSettings);
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user