From a7221012c8a3ed71f2f0f74561bf0ce16fb4f2d2 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 9 Jun 2023 00:02:06 +0200 Subject: [PATCH] docker: configFile method Signed-off-by: CrazyMax --- __tests__/docker/docker.test.ts | 52 +++++++++++++++ __tests__/fixtures/docker-config-auths.json | 8 +++ __tests__/fixtures/docker-config-proxies.json | 8 +++ src/docker/docker.ts | 12 ++++ src/types/docker.ts | 66 +++++++++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 __tests__/fixtures/docker-config-auths.json create mode 100644 __tests__/fixtures/docker-config-proxies.json create mode 100644 src/types/docker.ts diff --git a/__tests__/docker/docker.test.ts b/__tests__/docker/docker.test.ts index e32c64d8..66b5f292 100644 --- a/__tests__/docker/docker.test.ts +++ b/__tests__/docker/docker.test.ts @@ -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(() => { @@ -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'); diff --git a/__tests__/fixtures/docker-config-auths.json b/__tests__/fixtures/docker-config-auths.json new file mode 100644 index 00000000..5a1b9d86 --- /dev/null +++ b/__tests__/fixtures/docker-config-auths.json @@ -0,0 +1,8 @@ +{ + "auths": { + "https://index.docker.io/v1/": { + "auth": "am9lam9lOmhlbGxv", + "email": "user@example.com" + } + } +} diff --git a/__tests__/fixtures/docker-config-proxies.json b/__tests__/fixtures/docker-config-proxies.json new file mode 100644 index 00000000..72c5307b --- /dev/null +++ b/__tests__/fixtures/docker-config-proxies.json @@ -0,0 +1,8 @@ +{ + "proxies": { + "default": { + "httpProxy": "http://127.0.0.1:3128", + "httpsProxy": "http://127.0.0.1:3128" + } + } +} diff --git a/src/docker/docker.ts b/src/docker/docker.ts index 9d391f3a..4adc7562 100644 --- a/src/docker/docker.ts +++ b/src/docker/docker.ts @@ -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 JSON.parse(fs.readFileSync(f, {encoding: 'utf-8'})); + } + public static async isAvailable(): Promise { return await io .which('docker', true) diff --git a/src/types/docker.ts b/src/types/docker.ts new file mode 100644 index 00000000..66cf24d8 --- /dev/null +++ b/src/types/docker.ts @@ -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; + HttpHeaders?: Record; + psFormat?: string; + imagesFormat?: string; + networksFormat?: string; + pluginsFormat?: string; + volumesFormat?: string; + statsFormat?: string; + detachKeys?: string; + credsStore?: string; + credHelpers?: Record; + serviceInspectFormat?: string; + servicesFormat?: string; + tasksFormat?: string; + secretFormat?: string; + configFormat?: string; + nodesFormat?: string; + pruneFilters?: string[]; + proxies?: Record; + experimental?: string; + stackOrchestrator?: string; + kubernetes?: KubernetesConfig; + currentContext?: string; + cliPluginsExtraDirs?: string[]; + plugins?: Record>; + aliases?: Record; +} + +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; +}