import * as fs from 'fs' ; import { WritableStreamBuffer } from 'stream-buffers' ; import * as tar from 'tar' ; import * as tmp from 'tmp-promise' ; import { KubeConfig } from './config' ; import { Exec } from './exec' ; export class Cp { public execInstance : Exec ; public constructor ( config , execInstance ? ) { this . execInstance = execInstance || new Exec ( config ) ; } param { string } namespace param { string } podName param { string } containerName param { string } srcPath param { string } tgtPath public async cpFromPod ( namespace , podName , containerName , srcPath , tgtPath ) { const tmpFile = tmp . fileSync ( ) ; const tmpFileName = tmpFile . name ; const command = [ 'tar' , 'zcf' , '-' , srcPath ] ; const writerStream = fs . createWriteStream ( tmpFileName ) ; const errStream = new WritableStreamBuffer ( ) ; this . execInstance . exec ( namespace , podName , containerName , command , writerStream , errStream , null , false , async ( ) => { if ( errStream . size ( ) ) { throw new Error ( `Error from cpFromPod - details: \n ${ errStream . getContentsAsString ( ) }` ) ; } await tar . x ( { file : tmpFileName , cwd : tgtPath , } ) ; } ) ; } param { string } namespace param { string } podName param { string } containerName param { string } srcPath param { string } tgtPath public async cpToPod ( namespace , podName , containerName , srcPath , tgtPath ) { const tmpFile = tmp . fileSync ( ) ; const tmpFileName = tmpFile . name ; const command = [ 'tar' , 'xf' , '-' , '-C' , tgtPath ] ; await tar . c ( { file : tmpFile . name , } , [ srcPath ] ) ; const readStream = fs . createReadStream ( tmpFileName ) ; const errStream = new WritableStreamBuffer ( ) ; this . execInstance . exec ( namespace , podName , containerName , command , null , errStream , readStream , false , async ( ) => { if ( errStream . size ( ) ) { throw new Error ( `Error from cpToPod - details: \n ${ errStream . getContentsAsString ( ) }` ) ; } } ) ; } }