alwaysai
Version:
The alwaysAI command-line interface (CLI)
75 lines • 3.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const chalk_1 = require("chalk");
const t = require("io-ts");
const config_nodejs_1 = require("@alwaysai/config-nodejs");
const alwayscli_1 = require("@alwaysai/alwayscli");
const docker_spawner_1 = require("../spawner/docker-spawner");
const target_protocol_1 = require("./target-protocol");
const ssh_docker_spawner_1 = require("../spawner/ssh-docker-spawner");
exports.TARGET_CONFIG_FILE_NAME = 'alwaysai.target.json';
const sshDockerTarget = t.type({
protocol: t.literal(target_protocol_1.TargetProtocol['ssh+docker:']),
hostname: t.string,
path: t.string,
}, 'SshDockerTarget');
const dockerTarget = t.type({
protocol: t.literal(target_protocol_1.TargetProtocol['docker:']),
});
const targetConfigCodec = t.taggedUnion('protocol', [dockerTarget, sshDockerTarget]);
const DID_YOU_RUN_APP_CONFIGURE = 'Did you run "alwaysai app configure"?';
const ENOENT = {
message: `${exports.TARGET_CONFIG_FILE_NAME} not found. ${DID_YOU_RUN_APP_CONFIGURE}`,
code: 'TERSE',
};
exports.targetConfigFile = TargetConfigFile();
function TargetConfigFile(dir = process.cwd()) {
const filePath = path_1.join(dir, exports.TARGET_CONFIG_FILE_NAME);
const configFile = config_nodejs_1.ConfigFile({ path: filePath, codec: targetConfigCodec, ENOENT });
return Object.assign({}, configFile, { readSpawner,
describe });
function describe() {
const config = configFile.readIfExists();
if (!config) {
return `Target configuration file "${exports.TARGET_CONFIG_FILE_NAME}" not found`;
}
const docker = chalk_1.default.bold('docker');
switch (config.protocol) {
case 'docker:': {
return `Target: ${docker} container on this host`;
}
case 'ssh+docker:': {
const hostname = chalk_1.default.bold(config.hostname);
const path = chalk_1.default.bold(config.path);
return `Target: ${docker} container on ${hostname}, path ${path}`;
}
default:
throw new Error('Unexpected protocol');
}
}
function readSpawner() {
const config = configFile.read();
switch (config.protocol) {
case 'ssh+docker:': {
if (!config.hostname) {
throw new alwayscli_1.TerseError(`"hostname" is required for protocol "${config.protocol}". ${DID_YOU_RUN_APP_CONFIGURE}`);
}
if (!config.path) {
throw new alwayscli_1.TerseError(`"path" is required for protocol "${config.protocol}". ${DID_YOU_RUN_APP_CONFIGURE}`);
}
const spawner = ssh_docker_spawner_1.SshDockerSpawner({
path: config.path,
hostname: config.hostname,
});
return spawner;
}
case 'docker:': {
return docker_spawner_1.DockerSpawner();
}
default:
throw new alwayscli_1.TerseError('Unsupported protocol');
}
}
}
//# sourceMappingURL=target-config-file.js.map