Skip to content

Commit

Permalink
Merge pull request #258 from HubSpot/add/secrets-update-command
Browse files Browse the repository at this point in the history
Add `secrets update`
  • Loading branch information
anthmatic authored Jul 15, 2020
2 parents 461b35b + e21b54f commit 8b84df9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/cms-cli/bin/hs-secrets-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env node

const { Command } = require('commander');

const { configureSecretsUpdateCommand } = require('../commands/secrets');

const program = new Command('hs secrets update');
configureSecretsUpdateCommand(program);
program.parse(process.argv);
9 changes: 9 additions & 0 deletions packages/cms-cli/bin/hscms-secrets-update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env node

const { Command } = require('commander');

const { configureSecretsUpdateCommand } = require('../commands/secrets');

const program = new Command('hscms secrets update');
configureSecretsUpdateCommand(program);
program.parse(process.argv);
42 changes: 42 additions & 0 deletions packages/cms-cli/commands/secrets.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
} = require('@hubspot/cms-lib/errorHandlers');
const {
addSecret,
updateSecret,
deleteSecret,
fetchSecrets,
} = require('@hubspot/cms-lib/api/secrets');
Expand All @@ -32,6 +33,7 @@ function configureSecretsCommand(program) {
.version(version)
.description('Manage secrets')
.command('add <name> <value>', 'add a HubSpot secret')
.command('update <name> <value>', 'update an existing HubSpot secret')
.command('delete <name>', 'delete a HubSpot secret')
.command('list', 'list all HubSpot secrets');

Expand Down Expand Up @@ -78,6 +80,45 @@ function configureSecretsAddCommand(program) {
addConfigOptions(program);
}

function configureSecretsUpdateCommand(program) {
program
.version(version)
.description('Update an existing HubSpot secret')
.arguments('<name> <value>')
.action(async (secretName, secretValue) => {
setLogLevel(program);
logDebugInfo(program);
const { config: configPath } = program;
loadConfig(configPath);
checkAndWarnGitInclusion();

if (!(validateConfig() && (await validatePortal(program)))) {
process.exit(1);
}
const portalId = getPortalId(program);

try {
await updateSecret(portalId, secretName, secretValue);
logger.log(
`The secret "${secretName}" was updated in the HubSpot portal: ${portalId}`
);
} catch (e) {
logger.error(`The secret "${secretName}" was not updated`);
logApiErrorInstance(
e,
new ApiErrorContext({
request: 'update secret',
portalId,
})
);
}
});

addLoggerOptions(program);
addPortalOptions(program);
addConfigOptions(program);
}

function configureSecretsDeleteCommand(program) {
program
.version(version)
Expand Down Expand Up @@ -157,6 +198,7 @@ function configureSecretsListCommand(program) {
module.exports = {
configureSecretsCommand,
configureSecretsAddCommand,
configureSecretsUpdateCommand,
configureSecretsDeleteCommand,
configureSecretsListCommand,
};
11 changes: 11 additions & 0 deletions packages/cms-lib/api/secrets.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ async function addSecret(portalId, key, value) {
});
}

async function updateSecret(portalId, key, value) {
return http.put(portalId, {
uri: SECRETS_API_PATH,
body: {
key,
secret: value,
},
});
}

async function deleteSecret(portalId, key) {
return http.delete(portalId, {
uri: `${SECRETS_API_PATH}/${key}`,
Expand All @@ -26,6 +36,7 @@ async function fetchSecrets(portalId) {

module.exports = {
addSecret,
updateSecret,
deleteSecret,
fetchSecrets,
};

0 comments on commit 8b84df9

Please sign in to comment.