@cto.ai/ops
Version:
💻 CTO.ai - The CLI built for Teams 🚀
87 lines (86 loc) • 3.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const base_1 = tslib_1.__importStar(require("./../base"));
const CustomErrors_1 = require("./../errors/CustomErrors");
const time_1 = require("./../utils/time");
const Table = require('cli-table3');
class Status extends base_1.default {
constructor() {
super(...arguments);
this.getErrorMessage = async (config) => {
const services = await this.services.api.find(`/private/teams/${config.team.name}/serviceinfo`, {
headers: {
Authorization: this.accessToken,
},
});
if (services.data.length === 0) {
return 'It looks like you have not created any services yet. \nCheck out our documentation to get started: https://cto.ai/docs';
}
const startedService = services.data.find(service => service.last_run_status === 'started');
return startedService ? '' : 'No services are currently running.';
};
this.getRunningOps = async (config) => {
try {
const services = await this.services.api.find(`/private/teams/${config.team.name}/service`, {
headers: {
Authorization: this.accessToken,
},
});
return services.data;
}
catch (err) {
this.debug('%0', err);
this.log(this.ux.colors.whiteBright(`❗ Sorry, we encountered an error trying to list the service status`));
throw new CustomErrors_1.APIError(err);
}
};
this.showServices = (runningServices, config) => {
this.log(this.ux.colors.whiteBright(`Active services for ${this.ux.colors.callOutCyan(config.team.name)}':\n`));
var table = new Table({
head: ['Service', 'Run ID', 'Created'],
style: {
padding: 0,
border: [],
},
});
const sortedServices = runningServices.sort((a, b) => {
return new Date(b.createdAt) - new Date(a.createdAt);
});
sortedServices.forEach(s => {
let t = {};
t[this.ux.url(`${s.name}:${s.version}`, s.url)] = [
this.ux.colors.whiteBright(s.runId),
this.ux.colors.whiteBright(`${(0, time_1.timeSince)(new Date(s.createdAt))} ago`),
];
table.push(t);
});
console.log(table.toString());
};
}
async run() {
const config = await this.isLoggedIn();
try {
this.parse(Status);
const serviceErrorMessage = await this.getErrorMessage(config);
if (serviceErrorMessage) {
this.log(this.ux.colors.whiteBright(`${serviceErrorMessage}`));
return;
}
const runningServices = await this.getRunningOps(config);
this.showServices(runningServices, config);
}
catch (err) {
this.debug('%O', err);
this.config.runHook('error', {
err,
accessToken: config.tokens.accessToken,
});
}
}
}
exports.default = Status;
Status.description = 'See the status of currently running cloud services';
Status.flags = {
help: base_1.flags.help({ char: 'h' }),
};