Skip to content

Commit

Permalink
Merge pull request #400 from crazy-max/docker-context-inspect
Browse files Browse the repository at this point in the history
docker: contextInspect func
  • Loading branch information
crazy-max committed Jul 4, 2024
2 parents 78ca5b7 + 6196743 commit d7a84a5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
13 changes: 12 additions & 1 deletion __tests__/docker/docker.test.itg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {describe, expect, test} from '@jest/globals';
import {describe, expect, it, test} from '@jest/globals';

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

Expand Down Expand Up @@ -54,3 +54,14 @@ maybe('pull', () => {
}
}, 600000);
});

maybe('contextInspect', () => {
it('inspect default context', async () => {
const contextInfo = await Docker.contextInspect();
expect(contextInfo).toBeDefined();
console.log('contextInfo', contextInfo);
expect(contextInfo?.Name).toBeDefined();
expect(contextInfo?.Endpoints).toBeDefined();
expect(Object.keys(contextInfo?.Endpoints).length).toBeGreaterThan(0);
});
});
13 changes: 13 additions & 0 deletions __tests__/docker/docker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ describe('context', () => {
});
});

describe('contextInspect', () => {
it('call docker context inspect', async () => {
const execSpy = jest.spyOn(Exec, 'getExecOutput');
await Docker.contextInspect('foo').catch(() => {
// noop
});
expect(execSpy).toHaveBeenCalledWith(`docker`, ['context', 'inspect', '--format=json', 'foo'], {
ignoreReturnCode: true,
silent: true
});
});
});

describe('printVersion', () => {
it('call docker version', async () => {
const execSpy = jest.spyOn(Exec, 'exec');
Expand Down
18 changes: 17 additions & 1 deletion src/docker/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {Cache} from '../cache';
import {Exec} from '../exec';
import {Util} from '../util';

import {ConfigFile} from '../types/docker/docker';
import {ConfigFile, ContextInfo} from '../types/docker/docker';

export class Docker {
static get configDir(): string {
Expand Down Expand Up @@ -69,6 +69,22 @@ export class Docker {
});
}

public static async contextInspect(name?: string): Promise<ContextInfo> {
const args = ['context', 'inspect', '--format=json'];
if (name) {
args.push(name);
}
return await Exec.getExecOutput(`docker`, args, {
ignoreReturnCode: true,
silent: true
}).then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr.trim());
}
return (<Array<ContextInfo>>JSON.parse(res.stdout.trim()))[0];
});
}

public static async printVersion(): Promise<void> {
await Exec.exec('docker', ['version']);
}
Expand Down
30 changes: 30 additions & 0 deletions src/types/docker/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,33 @@ export interface AuthConfig {
identitytoken?: string;
registrytoken?: string;
}

export interface ContextInfo {
Name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Metadata: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Endpoints: Record<string, EndpointInfo>;
TLSMaterial: Record<string, Array<string>>;
Storage: StorageInfo;
}

export interface EndpointInfo {
Host?: string;
SkipVerify: boolean;
TLSData?: TLSData;
}

export interface TLSData {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
CA: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Key: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Cert: any;
}

export interface StorageInfo {
MetadataPath: string;
TLSPath: string;
}

0 comments on commit d7a84a5

Please sign in to comment.