"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Cp = void 0;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const stream_buffers_1 = require("stream-buffers");
const tar = tslib_1.__importStar(require("tar"));
const exec_1 = require("./exec");
const util_1 = require("./util");
class Cp {
    constructor(config, execInstance) {
        this.execInstance = execInstance || new exec_1.Exec(config);
    }
    /**
     * @param {string} namespace - The namespace of the pod to exec the command inside.
     * @param {string} podName - The name of the pod to exec the command inside.
     * @param {string} containerName - The name of the container in the pod to exec the command inside.
     * @param {string} srcPath - The source path in the pod
     * @param {string} tgtPath - The target path in local
     * @param {string} [cwd] - The directory that is used as the parent in the pod when downloading
     */
    async cpFromPod(namespace, podName, containerName, srcPath, tgtPath, cwd) {
        const tmpFileName = await (0, util_1.generateTmpFileName)();
        const command = ['tar', 'zcf', '-'];
        if (cwd) {
            command.push('-C', cwd);
        }
        command.push(srcPath);
        const writerStream = fs.createWriteStream(tmpFileName);
        const errStream = new stream_buffers_1.WritableStreamBuffer();
        this.execInstance.exec(namespace, podName, containerName, command, writerStream, errStream, null, false, async ({ status }) => {
            writerStream.close();
            if (status === 'Failure' || errStream.size()) {
                throw new Error(`Error from cpFromPod - details: \n ${errStream.getContentsAsString()}`);
            }
            await tar.x({
                file: tmpFileName,
                cwd: tgtPath,
            });
        });
    }
    /**
     * @param {string} namespace - The namespace of the pod to exec the command inside.
     * @param {string} podName - The name of the pod to exec the command inside.
     * @param {string} containerName - The name of the container in the pod to exec the command inside.
     * @param {string} srcPath - The source path in local
     * @param {string} tgtPath - The target path in the pod
     * @param {string} [cwd] - The directory that is used as the parent in the host when uploading
     */
    async cpToPod(namespace, podName, containerName, srcPath, tgtPath, cwd) {
        const tmpFileName = await (0, util_1.generateTmpFileName)();
        const command = ['tar', 'xf', '-', '-C', tgtPath];
        await tar.c({ file: tmpFileName, cwd }, [srcPath]);
        const readStream = fs.createReadStream(tmpFileName);
        const errStream = new stream_buffers_1.WritableStreamBuffer();
        this.execInstance.exec(namespace, podName, containerName, command, null, errStream, readStream, false, async ({ status }) => {
            if (status === 'Failure' || errStream.size()) {
                throw new Error(`Error from cpToPod - details: \n ${errStream.getContentsAsString()}`);
            }
        });
    }
}
exports.Cp = Cp;
//# sourceMappingURL=cp.js.map