growthbook
Version:
The GrowthBook command-line interface (CLI) for working with the GrowthBook A/B testing, feature flagging, and experimentation platform
87 lines (86 loc) • 3.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Fs = require("node:fs");
const core_1 = require("@oclif/core");
const cli_1 = require("../../utils/cli");
const constants_1 = require("../../utils/constants");
const config_1 = require("../../utils/config");
const saved_groups_repository_1 = require("../../repositories/saved-groups.repository");
const http_1 = require("../../utils/http");
class SavedgroupsUpdate extends core_1.Command {
async run() {
const { args: { input, }, flags: { profile, apiBaseUrl, filePath, output, id, }, } = await this.parse(SavedgroupsUpdate);
const profileUsed = profile || constants_1.DEFAULT_GROWTHBOOK_PROFILE;
const { apiKey, apiBaseUrl: configApiBaseUrl } = (0, config_1.getGrowthBookProfileConfigAndThrowForCommand)(profileUsed, this);
const baseUrlUsed = apiBaseUrl || configApiBaseUrl || constants_1.DEFAULT_GROWTHBOOK_BASE_URL;
// Read payload from standard in
let payload = input;
// If no input from standard in, try the filePath (if provided)
if (!input && filePath) {
payload = Fs.readFileSync(filePath, 'utf-8');
}
// If no payload from file, prompt for input
if (!payload) {
payload = await core_1.ux.prompt('Paste the saved group payload', {
required: true,
});
}
let parsedPayload = null;
try {
parsedPayload = JSON.parse(payload);
}
catch {
this.error('Unable to parse payload');
}
core_1.ux.action.start('Posting parsed payload');
this.logJson(parsedPayload);
const repo = new saved_groups_repository_1.SavedGroupsRepository({
apiKey,
apiBaseUrl: baseUrlUsed,
});
try {
// Update the saved group
const updatedRecord = await repo.updateSavedGroup(id, parsedPayload);
this.logJson(updatedRecord);
core_1.ux.action.stop(`${cli_1.Icons.checkmark} Successfully updated saved group ${updatedRecord.id}`);
// If provided an output path, write to file
if (output) {
const outputContents = JSON.stringify(updatedRecord, null, 2);
core_1.ux.action.start('Writing updated saved group to file');
try {
Fs.writeFileSync(output, outputContents);
core_1.ux.action.stop(`${cli_1.Icons.checkmark} Output updated saved group to ${output}`);
}
catch (error) {
this.error(`${error}`);
core_1.ux.action.stop(`${cli_1.Icons.xSymbol} Failed to write saved group output to file path ${output}`);
}
}
}
catch (error) {
this.error((0, http_1.errorStringFromResponse)(error));
core_1.ux.action.stop(`${cli_1.Icons.xSymbol} Failed to update saved group`);
}
}
}
exports.default = SavedgroupsUpdate;
SavedgroupsUpdate.description = 'Update an existing saved group.';
SavedgroupsUpdate.examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> --filePath input.json',
];
SavedgroupsUpdate.flags = {
...cli_1.baseGrowthBookCliFlags,
...cli_1.fileInputOutputCliFlags,
id: core_1.Flags.string({
char: 'i',
description: 'Saved group ID to update',
required: true,
}),
};
SavedgroupsUpdate.args = {
input: core_1.Args.string({
description: 'JSON payload of the update payload. Docs: https://docs.growthbook.io/api/#tag/saved-groups/operation/updateSavedGroup',
required: false,
}),
};