Skip to content

Commit d7a84a5

Browse files
authored
Merge pull request #400 from crazy-max/docker-context-inspect
docker: contextInspect func
2 parents 78ca5b7 + 6196743 commit d7a84a5

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

__tests__/docker/docker.test.itg.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {describe, expect, test} from '@jest/globals';
17+
import {describe, expect, it, test} from '@jest/globals';
1818

1919
import {Docker} from '../../src/docker/docker';
2020

@@ -54,3 +54,14 @@ maybe('pull', () => {
5454
}
5555
}, 600000);
5656
});
57+
58+
maybe('contextInspect', () => {
59+
it('inspect default context', async () => {
60+
const contextInfo = await Docker.contextInspect();
61+
expect(contextInfo).toBeDefined();
62+
console.log('contextInfo', contextInfo);
63+
expect(contextInfo?.Name).toBeDefined();
64+
expect(contextInfo?.Endpoints).toBeDefined();
65+
expect(Object.keys(contextInfo?.Endpoints).length).toBeGreaterThan(0);
66+
});
67+
});

__tests__/docker/docker.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ describe('context', () => {
118118
});
119119
});
120120

121+
describe('contextInspect', () => {
122+
it('call docker context inspect', async () => {
123+
const execSpy = jest.spyOn(Exec, 'getExecOutput');
124+
await Docker.contextInspect('foo').catch(() => {
125+
// noop
126+
});
127+
expect(execSpy).toHaveBeenCalledWith(`docker`, ['context', 'inspect', '--format=json', 'foo'], {
128+
ignoreReturnCode: true,
129+
silent: true
130+
});
131+
});
132+
});
133+
121134
describe('printVersion', () => {
122135
it('call docker version', async () => {
123136
const execSpy = jest.spyOn(Exec, 'exec');

src/docker/docker.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {Cache} from '../cache';
2525
import {Exec} from '../exec';
2626
import {Util} from '../util';
2727

28-
import {ConfigFile} from '../types/docker/docker';
28+
import {ConfigFile, ContextInfo} from '../types/docker/docker';
2929

3030
export class Docker {
3131
static get configDir(): string {
@@ -69,6 +69,22 @@ export class Docker {
6969
});
7070
}
7171

72+
public static async contextInspect(name?: string): Promise<ContextInfo> {
73+
const args = ['context', 'inspect', '--format=json'];
74+
if (name) {
75+
args.push(name);
76+
}
77+
return await Exec.getExecOutput(`docker`, args, {
78+
ignoreReturnCode: true,
79+
silent: true
80+
}).then(res => {
81+
if (res.stderr.length > 0 && res.exitCode != 0) {
82+
throw new Error(res.stderr.trim());
83+
}
84+
return (<Array<ContextInfo>>JSON.parse(res.stdout.trim()))[0];
85+
});
86+
}
87+
7288
public static async printVersion(): Promise<void> {
7389
await Exec.exec('docker', ['version']);
7490
}

src/types/docker/docker.ts

+30
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,33 @@ export interface AuthConfig {
6464
identitytoken?: string;
6565
registrytoken?: string;
6666
}
67+
68+
export interface ContextInfo {
69+
Name: string;
70+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
71+
Metadata: any;
72+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
73+
Endpoints: Record<string, EndpointInfo>;
74+
TLSMaterial: Record<string, Array<string>>;
75+
Storage: StorageInfo;
76+
}
77+
78+
export interface EndpointInfo {
79+
Host?: string;
80+
SkipVerify: boolean;
81+
TLSData?: TLSData;
82+
}
83+
84+
export interface TLSData {
85+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
86+
CA: any;
87+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
88+
Key: any;
89+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
90+
Cert: any;
91+
}
92+
93+
export interface StorageInfo {
94+
MetadataPath: string;
95+
TLSPath: string;
96+
}

0 commit comments

Comments
 (0)