steadybit
Version:
Command-line interface to interact with the Steadybit API
117 lines • 5.91 kB
JavaScript
;
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2022 Steadybit GmbH
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeExperiments = executeExperiments;
const confirm_1 = require("../prompt/confirm");
const files_1 = require("./files");
const api = __importStar(require("./api"));
const rxjs_1 = require("rxjs");
const errors_1 = require("../errors");
function executeExperiments(options) {
return __awaiter(this, void 0, void 0, function* () {
if (!options.yes) {
const confirmation = yield (0, confirm_1.confirm)('Are you sure you want to run the experiment?', {
defaultYes: false,
defaultWhenNonInteractive: true,
});
if (!confirmation) {
process.exit(0);
}
}
if (!options.file && !options.key) {
throw (0, errors_1.abortExecution)('Either --key or --file must be specified.');
}
else if (options.file) {
const files = yield (0, files_1.resolveExperimentFiles)(options.file, options.recursive);
if (files.length > 1 && options.key) {
throw (0, errors_1.abortExecution)('If --key is specified, at most one --file can be specified.');
}
for (const file of files) {
const experiment = yield (0, files_1.loadExperiment)(file);
let key = options.key || experiment.key;
let result;
if (key) {
yield api.updateExperiment(key, experiment);
result = yield api.executeExperiment(key, !!options.yes);
}
else {
const upsertResult = yield api.upsertAndExecuteExperiment(experiment);
key = upsertResult.key;
result = upsertResult;
if (!experiment.key) {
yield (0, files_1.writeYamlFile)(file, Object.assign({ key }, experiment));
}
}
console.log('Executing experiment:', key);
console.log('Experiment run API:', result.location);
console.log('Experiment run UI:', result.uiLocation);
/* eslint-disable @typescript-eslint/no-unused-expressions */
options.wait && result.location && (yield waitFor(result.location));
}
}
else if (options.key) {
const result = yield api.executeExperiment(options.key, !!options.yes);
console.log('Experiment run API:', result.location);
console.log('Experiment run UI:', result.uiLocation);
console.log('Executing experiment:', options.key);
/* eslint-disable @typescript-eslint/no-unused-expressions */
options.wait && result.location && (yield waitFor(result.location));
}
});
}
function waitFor(location) {
return __awaiter(this, void 0, void 0, function* () {
const executionResult = yield (0, rxjs_1.firstValueFrom)((0, rxjs_1.interval)(5000)
.pipe((0, rxjs_1.switchMap)(() => (0, rxjs_1.from)(api.getExperimentExecutionUsingUrl(location !== null && location !== void 0 ? location : ''))))
.pipe((0, rxjs_1.tap)(e => console.log('Current run state:', e.state.toLowerCase())))
.pipe((0, rxjs_1.filter)(e => e.state === 'FAILED' || e.state === 'ERRORED' || e.state === 'CANCELED' || e.state === 'COMPLETED')));
if (executionResult && executionResult.state !== 'COMPLETED') {
console.error(`Experiment ${executionResult.key} (#${executionResult.id}) ${executionResult.state.toLowerCase()}${executionResult.reason ? `, reason: ${executionResult.reason}` : ''}`);
process.exit(1);
}
});
}
//# sourceMappingURL=exec.js.map