From 97e647fdd0a4ed3e6ec9201bb9880d5f6edc38be Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sat, 25 Feb 2023 12:20:06 +0100 Subject: [PATCH] docker: context method Signed-off-by: CrazyMax --- __tests__/docker.test.ts | 13 +++++++++++++ src/docker.ts | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/__tests__/docker.test.ts b/__tests__/docker.test.ts index 81e1ef07..ea266326 100644 --- a/__tests__/docker.test.ts +++ b/__tests__/docker.test.ts @@ -57,6 +57,19 @@ describe('isAvailable', () => { }); }); +describe('context', () => { + it('call docker context show', async () => { + const execSpy = jest.spyOn(Exec, 'getExecOutput'); + await Docker.context().catch(() => { + // noop + }); + expect(execSpy).toHaveBeenCalledWith(`docker`, ['context', 'show'], { + ignoreReturnCode: true, + silent: true + }); + }); +}); + describe('printVersion', () => { it('call docker version', async () => { const execSpy = jest.spyOn(Exec, 'exec'); diff --git a/src/docker.ts b/src/docker.ts index 7faa8e27..e21c5cde 100644 --- a/src/docker.ts +++ b/src/docker.ts @@ -38,6 +38,18 @@ export class Docker { }); } + public static async context(): Promise { + return await Exec.getExecOutput(`docker`, ['context', 'show'], { + ignoreReturnCode: true, + silent: true + }).then(res => { + if (res.stderr.length > 0 && res.exitCode != 0) { + throw new Error(res.stderr); + } + return res.stdout.trim(); + }); + } + public static async printVersion(): Promise { await Exec.exec('docker', ['version']); }