Skip to content

Commit

Permalink
Merge pull request #345 from HubSpot/add/secrets-prompt
Browse files Browse the repository at this point in the history
Prompt for secret values
  • Loading branch information
anthmatic authored Oct 12, 2020
2 parents 6af8af4 + 11791c3 commit b54a571
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
11 changes: 5 additions & 6 deletions packages/cms-cli/commands/secrets/addSecret.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ const {
getPortalId,
} = require('../../lib/commonOpts');
const { logDebugInfo } = require('../../lib/debugInfo');
const { secretValuePrompt } = require('../../lib/secretPrompt');

exports.command = 'add <name> <value>';
exports.command = 'add <name>';
exports.describe = 'Add a HubSpot secret';

exports.handler = async options => {
const { config: configPath, name: secretName, value: secretValue } = options;
const { config: configPath, name: secretName } = options;

setLogLevel(options);
logDebugInfo(options);
Expand All @@ -40,6 +41,8 @@ exports.handler = async options => {
trackCommandUsage('secrets-add', {}, portalId);

try {
const { secretValue } = await secretValuePrompt();

await addSecret(portalId, secretName, secretValue);
logger.log(
`The secret "${secretName}" was added to the HubSpot portal: ${portalId}`
Expand All @@ -65,9 +68,5 @@ exports.builder = yargs => {
describe: 'Name of the secret',
type: 'string',
});
yargs.positional('value', {
describe: 'The secret to be stored such as an API key',
type: 'string',
});
return yargs;
};
11 changes: 5 additions & 6 deletions packages/cms-cli/commands/secrets/updateSecret.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ const {
getPortalId,
} = require('../../lib/commonOpts');
const { logDebugInfo } = require('../../lib/debugInfo');
const { secretValuePrompt } = require('../../lib/secretPrompt');

exports.command = 'update <name> <value>';
exports.command = 'update <name>';
exports.describe = 'Update an existing HubSpot secret';

exports.handler = async options => {
const { name: secretName, value: secretValue, config: configPath } = options;
const { name: secretName, config: configPath } = options;

setLogLevel(options);
logDebugInfo(options);
Expand All @@ -40,6 +41,8 @@ exports.handler = async options => {
trackCommandUsage('secrets-update', {}, portalId);

try {
const { secretValue } = await secretValuePrompt();

await updateSecret(portalId, secretName, secretValue);
logger.log(
`The secret "${secretName}" was updated in the HubSpot portal: ${portalId}`
Expand All @@ -65,9 +68,5 @@ exports.builder = yargs => {
describe: 'Name of the secret to be updated',
type: 'string',
});
yargs.positional('value', {
describe: 'The secret to be stored',
type: 'string',
});
return yargs;
};
22 changes: 22 additions & 0 deletions packages/cms-cli/lib/secretPrompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const inquirer = require('inquirer');

const SECRET_VALUE_PROMPT = {
name: 'secretValue',
type: 'password',
mask: '*',
message: 'Enter a value for your secret',
validate(val) {
if (typeof val !== 'string') {
return 'You entered an invalid value. Please try again.';
}
return true;
},
};

function secretValuePrompt() {
const prompt = inquirer.createPromptModule();
return prompt([SECRET_VALUE_PROMPT]);
}
module.exports = {
secretValuePrompt,
};

0 comments on commit b54a571

Please sign in to comment.