@controlplane/cli
Version:
Control Plane Corporation CLI
232 lines • 8.62 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.Manifest = exports.Remove = exports.Deploy = exports.StackCmd = void 0;
const YAML = require("js-yaml");
const convert_1 = require("../stack/convert");
const command_1 = require("../cli/command");
const functions_1 = require("../util/functions");
const io_1 = require("../util/io");
const image_1 = require("./image");
const generic_1 = require("./generic");
const objects_1 = require("../util/objects");
const options_1 = require("./options");
const MANIFEST_CMD_NAME = 'manifest';
const COMPOSE_FILE_NAMES = ['compose.yaml', 'compose.yml', 'docker-compose.yaml', 'docker-compose.yml'];
class StackCmd extends command_1.Command {
constructor() {
super(...arguments);
this.command = 'stack';
this.describe = 'manage docker-compose files';
this.aliases = ['compose'];
}
builder(yargs) {
return yargs
.demandCommand()
.version(false)
.help()
.command(new Deploy().toYargs())
.command(new Remove().toYargs())
.command(new Manifest().toYargs());
}
handle() { }
}
exports.StackCmd = StackCmd;
function withOptionalFileOptions(yargs) {
return yargs.options({
directory: {
alias: 'dir',
description: 'Path to parent folder of docker-compose file',
requiresArg: true,
},
'compose-file': {
description: 'Name of the docker-compose file if alternative naming was used',
requiresArg: true,
},
});
}
function withEnabledBuildOption(yargs) {
return yargs.options({
build: {
description: 'Build images',
boolean: true,
default: true,
},
});
}
function withDisabledBuildOption(yargs) {
return yargs.options({
build: {
description: 'Build images',
boolean: true,
default: false,
},
});
}
class Deploy extends generic_1.Apply {
constructor() {
super(...arguments);
this.command = 'deploy';
this.describe = 'deploy from a docker-compose file';
this.aliases = ['up'];
}
builder(yargs) {
return (0, functions_1.pipe)(
//
withOptionalFileOptions, options_1.withGvcOptions, withEnabledBuildOption)(yargs);
}
async handle(args) {
// Get file path
const path = args.directory === undefined || args.directory === '.' ? process.cwd() : args.directory;
let bodyAsString;
try {
// Finding docker compose file names
let fileNames = [];
if (args['compose-file']) {
fileNames.push(args['compose-file']);
}
else {
fileNames.push(...COMPOSE_FILE_NAMES);
}
bodyAsString = await (0, io_1.findAndReadFileInDir)(path, fileNames);
}
catch (e) {
this.session.abort({ message: e.message });
}
// Getting session configuration
const registry = args.build
? (0, image_1.configureDockerAuth)((await this.session.discovery).endpoints.registry, this.session.context.org)
: '';
try {
const cplnObject = await (0, convert_1.default)(bodyAsString, path, this.session.context, registry, this.session.profile, args.build);
const sortedObjects = (0, objects_1.sortByKindPriority)(cplnObject);
const failures = [];
for (const resource of sortedObjects) {
const res = await this.applyItem(resource);
if (res !== true) {
failures.push(res);
}
}
if (failures.length > 0) {
let options = '';
const relativeArgs = ['directory', 'compose-file', 'gvc'];
for (const [arg, value] of Object.entries(args)) {
if (relativeArgs.includes(arg) && value) {
options += `--${arg} ${value} `;
}
}
this.session.errFormat(failures);
this.session.abort({
message: `Suggestion: Output the manifest instead and troubleshoot further.\nRun:\n cpln ${args._[0]} ${MANIFEST_CMD_NAME} ${options}`,
});
}
}
catch (e) {
this.session.abort({ message: e.message });
}
}
}
exports.Deploy = Deploy;
class Remove extends generic_1.DeleteFromFiles {
constructor() {
super(...arguments);
this.command = 'rm';
this.describe = 'Delete objects from a docker-compose file';
this.aliases = ['down'];
}
builder(yargs) {
return (0, functions_1.pipe)(withOptionalFileOptions, options_1.withGvcOptions)(yargs);
}
async handle(args) {
// Get file path
const path = args.directory === undefined || args.directory === '.' ? process.cwd() : args.directory;
let bodyAsString;
try {
// Finding docker-compose file names
let fileNames = [];
if (args['compose-file']) {
fileNames.push(args['compose-file']);
}
else {
fileNames.push(...COMPOSE_FILE_NAMES);
}
bodyAsString = await (0, io_1.findAndReadFileInDir)(path, fileNames);
}
catch (e) {
this.session.abort({ message: e.message });
}
// Getting session configuration
const registry = (0, image_1.configureDockerAuth)((await this.session.discovery).endpoints.registry, this.session.context.org);
try {
const cplnObjects = await (0, convert_1.default)(bodyAsString, path, this.session.context, registry, this.session.profile, false);
const sortedObjects = (0, objects_1.sortByKindPriority)(cplnObjects, true);
const failures = [];
for (const resource of sortedObjects) {
const res = await this.deleteItem(resource);
if (res !== true) {
failures.push(res);
}
}
if (failures.length > 0) {
this.session.abort({ error: failures });
}
}
catch (e) {
this.session.abort({ message: e.message });
}
}
}
exports.Remove = Remove;
class Manifest extends command_1.Command {
constructor() {
super(...arguments);
this.command = MANIFEST_CMD_NAME;
this.describe = 'Generate a CPLN apply file from a docker-compose file';
}
builder(yargs) {
return (0, functions_1.pipe)(withOptionalFileOptions, options_1.withGvcOptions, options_1.withFormatOptions, withDisabledBuildOption)(yargs);
}
async handle(args) {
// Get file path
const path = args.directory === undefined || args.directory === '.' ? process.cwd() : args.directory;
let bodyAsString;
try {
// Finding docker-compose file names
let fileNames = [];
if (args['compose-file']) {
fileNames.push(args['compose-file']);
}
else {
fileNames.push(...COMPOSE_FILE_NAMES);
}
bodyAsString = await (0, io_1.findAndReadFileInDir)(path, fileNames);
}
catch (e) {
this.session.abort({ message: e.message });
}
// Getting session configuration
const registry = args.build
? (0, image_1.configureDockerAuth)((await this.session.discovery).endpoints.registry, this.session.context.org)
: '';
try {
let output;
const cplnObject = await (0, convert_1.default)(bodyAsString, path, this.session.context, registry, this.session.profile, args.build);
const sortedObjects = (0, objects_1.sortByKindPriority)(cplnObject);
switch (this.session.format.output) {
case 'json':
case 'json-slim':
output = JSON.stringify(sortedObjects);
break;
default:
const asYamlObjs = sortedObjects.map((obj) => YAML.dump(obj, {}));
output = asYamlObjs.join('---\n');
break;
}
this.session.out(output);
}
catch (e) {
this.session.abort({ message: e.message });
}
}
}
exports.Manifest = Manifest;
//# sourceMappingURL=stack.js.map
;