Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 15 additions & 68 deletions src/commands/env/var/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,55 +52,6 @@ export default class ConfigSet extends Command {
}, {});
}

parseErrors(errorObject: any, targetCompute: string) {
// may have create a cleaner way to parse the errors
// to fit the sfdx json error object, but this currently works

const errObj = errorObject.stack;
const isInvalidEnvironment = errObj.includes("Error: Couldn't");
const hasNoInput = errObj.includes('Error: Usage:');
const isInvalidInput = !hasNoInput;

if (isInvalidEnvironment) {
const errorStackStartIndex = errObj.indexOf('at');
const errStack = errObj.substr(errorStackStartIndex);
errorObject.message = `Couldn't find that app <${targetCompute}>`;
errorObject.stack = errStack;
this.error(errObj);
}

if (isInvalidInput) {
const errorIndex = errObj.indexOf(':') + 1;
const keyValueIndex = errObj.indexOf('value') + 5;

// eslint-disable-next-line no-control-regex
const errMessage = errObj
.substr(errorIndex, keyValueIndex)
// eslint-disable-next-line no-control-regex
.replace(/\u001b\[.*?m/g, '')
.replace('\n', '')
.replace(' ', '');
// eslint-disable-next-line no-control-regex
const errStack = errObj
.substr(keyValueIndex)
// eslint-disable-next-line no-control-regex
.replace(/\u001b\[.*?m\r?\n|\r/g, '')
.replace(' ', '');

errorObject.message = errMessage;
errorObject.stack = errStack;
this.error(errObj);
}

if (hasNoInput) {
const errorStackStartIndex = errObj.indexOf('at');
const errStack = errObj.substr(errorStackStartIndex);
errorObject.message = 'Must specify KEY and VALUE to set.';
errorObject.stack = errStack;
this.error(errObj);
}
}

async run() {
const { flags, argv } = await this.parse(ConfigSet);
this.postParseHook(flags);
Expand All @@ -122,32 +73,28 @@ export default class ConfigSet extends Command {

const appName = await resolveAppNameForEnvironment(targetCompute);

if (flags.json) {
try {
const configPairs = this.parseKeyValuePairs(argv);
const configPairs = this.parseKeyValuePairs(argv);

await this.client.patch(`/apps/${appName}/config-vars`, {
data: configPairs,
});

return [];
} catch (err: any) {
this.parseErrors(err, targetCompute);
}
} else {
const configPairs = this.parseKeyValuePairs(argv);

cli.action.start(
`Setting ${Object.keys(configPairs)
.map((key) => herokuColor.configVar(key))
.join(', ')} and restarting ${herokuColor.app(targetCompute)}`
);
cli.action.start(
`Setting ${Object.keys(configPairs)
.map((key) => herokuColor.configVar(key))
.join(', ')} and restarting ${herokuColor.app(targetCompute)}`
);

try {
await this.client.patch(`/apps/${appName}/config-vars`, {
data: configPairs,
});

cli.action.stop();

return [];
} catch (error: any) {
cli.action.stop('failed');
if (error.data?.message?.includes("Couldn't find that app")) {
this.error(new Error(`Could not find environment <${appName}>`));
}
this.error(error);
}
}
}
3 changes: 3 additions & 0 deletions test/commands/env/var/unset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ describe('sf env:var:unset', () => {
test
.stdout()
.stderr()
// Adding retries here because there is some kind of race condition that causes fancy-test to not
// fully capture the value of stderr when running in CI (╯°□°)╯︵ ┻━┻
.retries(2)
.nock('https://api.heroku.com', (api) =>
api
.patch('/apps/my-environment/config-vars', {
Expand Down