Skip to content

Commit 2651456

Browse files
authored
Merge pull request #2635 from dherbolt/release-1.1.patch-dherbolt
feat: allow to set the cwd in copyFromPod for use in tar
2 parents d1c2052 + f8283d4 commit 2651456

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/cp.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ export class Cp {
1616
* @param {string} containerName - The name of the container in the pod to exec the command inside.
1717
* @param {string} srcPath - The source path in the pod
1818
* @param {string} tgtPath - The target path in local
19+
* @param {string} [cwd] - The directory that is used as the parent in the pod when downloading
1920
*/
2021
public async cpFromPod(
2122
namespace: string,
2223
podName: string,
2324
containerName: string,
2425
srcPath: string,
2526
tgtPath: string,
27+
cwd?: string,
2628
): Promise<void> {
27-
const command = ['tar', 'zcf', '-', srcPath];
29+
const command = ['tar', 'zcf', '-'];
30+
if (cwd) {
31+
command.push('-C', cwd);
32+
}
33+
command.push(srcPath);
2834
const writerStream = tar.extract(tgtPath);
2935
const errStream = new WritableStreamBuffer();
3036
this.execInstance.exec(

src/cp_test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,35 @@ describe('Cp', () => {
3838
await cp.cpFromPod(namespace, pod, container, srcPath, tgtPath);
3939
verify(fakeWebSocket.connect(`${path}?${queryStr}`, null, anyFunction())).called();
4040
});
41+
42+
it('should run create tar command to a url with cwd', async () => {
43+
const kc = new KubeConfig();
44+
const fakeWebSocket: WebSocketInterface = mock(WebSocketHandler);
45+
const exec = new Exec(kc, instance(fakeWebSocket));
46+
const cp = new Cp(kc, exec);
47+
48+
const namespace = 'somenamespace';
49+
const pod = 'somepod';
50+
const container = 'container';
51+
const srcPath = '/';
52+
const tgtPath = '/';
53+
const cwd = '/abc';
54+
const cmdArray = ['tar', 'zcf', '-', '-C', cwd, srcPath];
55+
const path = `/api/v1/namespaces/${namespace}/pods/${pod}/exec`;
56+
57+
const query = {
58+
stdout: true,
59+
stderr: true,
60+
stdin: false,
61+
tty: false,
62+
command: cmdArray,
63+
container,
64+
};
65+
const queryStr = querystring.stringify(query);
66+
67+
await cp.cpFromPod(namespace, pod, container, srcPath, tgtPath, cwd);
68+
verify(fakeWebSocket.connect(`${path}?${queryStr}`, null, anyFunction())).called();
69+
});
4170
});
4271

4372
describe('cpToPod', () => {

0 commit comments

Comments
 (0)