Skip to content

Commit a722101

Browse files
committed
docker: configFile method
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
1 parent 6fc5565 commit a722101

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

__tests__/docker/docker.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,30 @@
1515
*/
1616

1717
import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';
18+
import * as fs from 'fs';
1819
import path from 'path';
1920
import * as io from '@actions/io';
2021
import osm = require('os');
22+
import * as rimraf from 'rimraf';
2123

2224
import {Docker} from '../../src/docker/docker';
2325
import {Exec} from '../../src/exec';
2426

27+
import {ConfigFile} from '../../src/types/docker';
28+
29+
const fixturesDir = path.join(__dirname, '..', 'fixtures');
30+
31+
// prettier-ignore
32+
const tmpDir = path.join(process.env.TEMP || '/tmp', 'docker-jest');
33+
2534
beforeEach(() => {
2635
jest.clearAllMocks();
2736
});
2837

38+
afterEach(function () {
39+
rimraf.sync(tmpDir);
40+
});
41+
2942
describe('configDir', () => {
3043
const originalEnv = process.env;
3144
beforeEach(() => {
@@ -48,6 +61,45 @@ describe('configDir', () => {
4861
});
4962
});
5063

64+
describe('configFile', () => {
65+
const originalEnv = process.env;
66+
beforeEach(() => {
67+
jest.resetModules();
68+
if (!fs.existsSync(tmpDir)) {
69+
fs.mkdirSync(tmpDir, {recursive: true});
70+
}
71+
process.env = {
72+
...originalEnv,
73+
DOCKER_CONFIG: tmpDir
74+
};
75+
});
76+
afterEach(() => {
77+
process.env = originalEnv;
78+
});
79+
it('auths', async () => {
80+
fs.copyFileSync(path.join(fixturesDir, 'docker-config-auths.json'), path.join(tmpDir, 'config.json'));
81+
expect(Docker.configFile()).toEqual({
82+
auths: {
83+
'https://index.docker.io/v1/': {
84+
auth: 'am9lam9lOmhlbGxv',
85+
email: 'user@example.com'
86+
}
87+
}
88+
} as unknown as ConfigFile);
89+
});
90+
it('proxies', async () => {
91+
fs.copyFileSync(path.join(fixturesDir, 'docker-config-proxies.json'), path.join(tmpDir, 'config.json'));
92+
expect(Docker.configFile()).toEqual({
93+
proxies: {
94+
default: {
95+
httpProxy: 'http://127.0.0.1:3128',
96+
httpsProxy: 'http://127.0.0.1:3128'
97+
}
98+
}
99+
} as unknown as ConfigFile);
100+
});
101+
});
102+
51103
describe('isAvailable', () => {
52104
it('cli', async () => {
53105
const ioWhichSpy = jest.spyOn(io, 'which');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"auths": {
3+
"https://index.docker.io/v1/": {
4+
"auth": "am9lam9lOmhlbGxv",
5+
"email": "user@example.com"
6+
}
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"proxies": {
3+
"default": {
4+
"httpProxy": "http://127.0.0.1:3128",
5+
"httpsProxy": "http://127.0.0.1:3128"
6+
}
7+
}
8+
}

src/docker/docker.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,29 @@
1414
* limitations under the License.
1515
*/
1616

17+
import fs from 'fs';
1718
import os from 'os';
1819
import path from 'path';
1920
import * as core from '@actions/core';
2021
import * as io from '@actions/io';
22+
2123
import {Exec} from '../exec';
2224

25+
import {ConfigFile} from '../types/docker';
26+
2327
export class Docker {
2428
static get configDir(): string {
2529
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
2630
}
2731

32+
public static configFile(): ConfigFile | undefined {
33+
const f = path.join(Docker.configDir, 'config.json');
34+
if (!fs.existsSync(f)) {
35+
return undefined;
36+
}
37+
return <ConfigFile>JSON.parse(fs.readFileSync(f, {encoding: 'utf-8'}));
38+
}
39+
2840
public static async isAvailable(): Promise<boolean> {
2941
return await io
3042
.which('docker', true)

src/types/docker.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2023 actions-toolkit authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// https://github.com/docker/cli/blob/master/cli/config/configfile/file.go
18+
export interface ConfigFile {
19+
auths: Record<string, AuthConfig>;
20+
HttpHeaders?: Record<string, string>;
21+
psFormat?: string;
22+
imagesFormat?: string;
23+
networksFormat?: string;
24+
pluginsFormat?: string;
25+
volumesFormat?: string;
26+
statsFormat?: string;
27+
detachKeys?: string;
28+
credsStore?: string;
29+
credHelpers?: Record<string, string>;
30+
serviceInspectFormat?: string;
31+
servicesFormat?: string;
32+
tasksFormat?: string;
33+
secretFormat?: string;
34+
configFormat?: string;
35+
nodesFormat?: string;
36+
pruneFilters?: string[];
37+
proxies?: Record<string, ProxyConfig>;
38+
experimental?: string;
39+
stackOrchestrator?: string;
40+
kubernetes?: KubernetesConfig;
41+
currentContext?: string;
42+
cliPluginsExtraDirs?: string[];
43+
plugins?: Record<string, Record<string, string>>;
44+
aliases?: Record<string, string>;
45+
}
46+
47+
export interface ProxyConfig {
48+
httpProxy?: string;
49+
httpsProxy?: string;
50+
noProxy?: string;
51+
ftpProxy?: string;
52+
}
53+
54+
export interface KubernetesConfig {
55+
allNamespaces?: string;
56+
}
57+
58+
export interface AuthConfig {
59+
username?: string;
60+
password?: string;
61+
auth?: string;
62+
email?: string;
63+
serveraddress?: string;
64+
identitytoken?: string;
65+
registrytoken?: string;
66+
}

0 commit comments

Comments
 (0)