diff --git a/src/cp.ts b/src/cp.ts index 702498f194..7e676bfb0b 100644 --- a/src/cp.ts +++ b/src/cp.ts @@ -16,6 +16,7 @@ export class Cp { * @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 */ public async cpFromPod( namespace: string, @@ -23,8 +24,13 @@ export class Cp { containerName: string, srcPath: string, tgtPath: string, + cwd?: string, ): Promise { - const command = ['tar', 'zcf', '-', srcPath]; + const command = ['tar', 'zcf', '-']; + if (cwd) { + command.push('-C', cwd); + } + command.push(srcPath); const writerStream = tar.extract(tgtPath); const errStream = new WritableStreamBuffer(); this.execInstance.exec( diff --git a/src/cp_test.ts b/src/cp_test.ts index 7cfcafa7d4..0782106cba 100644 --- a/src/cp_test.ts +++ b/src/cp_test.ts @@ -38,6 +38,35 @@ describe('Cp', () => { await cp.cpFromPod(namespace, pod, container, srcPath, tgtPath); verify(fakeWebSocket.connect(`${path}?${queryStr}`, null, anyFunction())).called(); }); + + it('should run create tar command to a url with cwd', async () => { + const kc = new KubeConfig(); + const fakeWebSocket: WebSocketInterface = mock(WebSocketHandler); + const exec = new Exec(kc, instance(fakeWebSocket)); + const cp = new Cp(kc, exec); + + const namespace = 'somenamespace'; + const pod = 'somepod'; + const container = 'container'; + const srcPath = '/'; + const tgtPath = '/'; + const cwd = '/abc'; + const cmdArray = ['tar', 'zcf', '-', '-C', cwd, srcPath]; + const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`; + + const query = { + stdout: true, + stderr: true, + stdin: false, + tty: false, + command: cmdArray, + container, + }; + const queryStr = querystring.stringify(query); + + await cp.cpFromPod(namespace, pod, container, srcPath, tgtPath, cwd); + verify(fakeWebSocket.connect(`${path}?${queryStr}`, null, anyFunction())).called(); + }); }); describe('cpToPod', () => {