"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const nodeUtil = require("util");
const core_1 = require("@oclif/core");
const child_process_1 = require("child_process");
const command_base_tenantless_1 = require("../command-base-tenantless");
const execAsync = nodeUtil.promisify(child_process_1.exec); // antiquated nastiness
class Stop extends command_base_tenantless_1.CommandBaseTenantless {
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                var _a, _b, _c;
                try {
                    this.bootstrap();
                    const { args, flags } = yield this.parse(Stop); // this needs to happen first so help can override
                    // this._cfg = this._cfgRepo.readProjectConfig();
                    // if (!this._cfg) throw new Error(`Could not find a project config.`);
                    let containerName = this._projCfg.name; // TODO: This needs to be looking at the collection:  this._cfg.images
                    let exists = yield execAsync(`docker ps -f name=${containerName} -q`);
                    if (exists.stdout) {
                        let cmd = `docker stop ${containerName}`;
                        if (flags.remove) {
                            cmd = `docker rm -f ${containerName}`;
                        }
                        this._ui.startSpinner(`Stopping ${this._styler.hilite(containerName)}`);
                        let proc = (0, child_process_1.exec)(cmd);
                        (_a = proc.stdout) === null || _a === void 0 ? void 0 : _a.addListener("data", chunk => process.stdout.write(chunk));
                        (_b = proc.stderr) === null || _b === void 0 ? void 0 : _b.addListener("data", chunk => process.stderr.write(chunk));
                        (_c = proc.stdin) === null || _c === void 0 ? void 0 : _c.addListener("data", chunk => process.stdin.write(chunk));
                        this._ui.stopSpinner("");
                    }
                    else {
                        this._ui.userMessage(`${containerName} already stopped.`);
                    }
                    resolve();
                }
                catch (err) {
                    reject(err);
                }
            }));
        });
    }
}
exports.default = Stop;
Stop.description = 'Stops the running container for this project';
Stop.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    remove: core_1.Flags.boolean({ char: 'r', description: 'Deletes the container after stopping it' })
};
Stop.args = {};
//# sourceMappingURL=stop.js.map