Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker: configFile method #114

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions __tests__/docker/docker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,30 @@
*/

import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';
import * as fs from 'fs';
import path from 'path';
import * as io from '@actions/io';
import osm = require('os');
import * as rimraf from 'rimraf';

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

import {ConfigFile} from '../../src/types/docker';

const fixturesDir = path.join(__dirname, '..', 'fixtures');

// prettier-ignore
const tmpDir = path.join(process.env.TEMP || '/tmp', 'docker-jest');

beforeEach(() => {
jest.clearAllMocks();
});

afterEach(function () {
rimraf.sync(tmpDir);
});

describe('configDir', () => {
const originalEnv = process.env;
beforeEach(() => {
Expand All @@ -48,6 +61,45 @@ describe('configDir', () => {
});
});

describe('configFile', () => {
const originalEnv = process.env;
beforeEach(() => {
jest.resetModules();
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
process.env = {
...originalEnv,
DOCKER_CONFIG: tmpDir
};
});
afterEach(() => {
process.env = originalEnv;
});
it('auths', async () => {
fs.copyFileSync(path.join(fixturesDir, 'docker-config-auths.json'), path.join(tmpDir, 'config.json'));
expect(Docker.configFile()).toEqual({
auths: {
'https://index.docker.io/v1/': {
auth: 'am9lam9lOmhlbGxv',
email: 'user@example.com'
}
}
} as unknown as ConfigFile);
});
it('proxies', async () => {
fs.copyFileSync(path.join(fixturesDir, 'docker-config-proxies.json'), path.join(tmpDir, 'config.json'));
expect(Docker.configFile()).toEqual({
proxies: {
default: {
httpProxy: 'http://127.0.0.1:3128',
httpsProxy: 'http://127.0.0.1:3128'
}
}
} as unknown as ConfigFile);
});
});

describe('isAvailable', () => {
it('cli', async () => {
const ioWhichSpy = jest.spyOn(io, 'which');
Expand Down
8 changes: 8 additions & 0 deletions __tests__/fixtures/docker-config-auths.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"auths": {
"https://index.docker.io/v1/": {
"auth": "am9lam9lOmhlbGxv",
"email": "user@example.com"
}
}
}
8 changes: 8 additions & 0 deletions __tests__/fixtures/docker-config-proxies.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"proxies": {
"default": {
"httpProxy": "http://127.0.0.1:3128",
"httpsProxy": "http://127.0.0.1:3128"
}
}
}
12 changes: 12 additions & 0 deletions src/docker/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,29 @@
* limitations under the License.
*/

import fs from 'fs';
import os from 'os';
import path from 'path';
import * as core from '@actions/core';
import * as io from '@actions/io';

import {Exec} from '../exec';

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

export class Docker {
static get configDir(): string {
return process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
}

public static configFile(): ConfigFile | undefined {
const f = path.join(Docker.configDir, 'config.json');
if (!fs.existsSync(f)) {
return undefined;
}
return <ConfigFile>JSON.parse(fs.readFileSync(f, {encoding: 'utf-8'}));
}

public static async isAvailable(): Promise<boolean> {
return await io
.which('docker', true)
Expand Down
66 changes: 66 additions & 0 deletions src/types/docker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// https://github.com/docker/cli/blob/master/cli/config/configfile/file.go
export interface ConfigFile {
auths: Record<string, AuthConfig>;
HttpHeaders?: Record<string, string>;
psFormat?: string;
imagesFormat?: string;
networksFormat?: string;
pluginsFormat?: string;
volumesFormat?: string;
statsFormat?: string;
detachKeys?: string;
credsStore?: string;
credHelpers?: Record<string, string>;
serviceInspectFormat?: string;
servicesFormat?: string;
tasksFormat?: string;
secretFormat?: string;
configFormat?: string;
nodesFormat?: string;
pruneFilters?: string[];
proxies?: Record<string, ProxyConfig>;
experimental?: string;
stackOrchestrator?: string;
kubernetes?: KubernetesConfig;
currentContext?: string;
cliPluginsExtraDirs?: string[];
plugins?: Record<string, Record<string, string>>;
aliases?: Record<string, string>;
}

export interface ProxyConfig {
httpProxy?: string;
httpsProxy?: string;
noProxy?: string;
ftpProxy?: string;
}

export interface KubernetesConfig {
allNamespaces?: string;
}

export interface AuthConfig {
username?: string;
password?: string;
auth?: string;
email?: string;
serveraddress?: string;
identitytoken?: string;
registrytoken?: string;
}