From f7cac3b071c16fa184e35510e88ad7f2d80a4d2f Mon Sep 17 00:00:00 2001
From: CrazyMax <crazy-max@users.noreply.github.com>
Date: Sun, 16 Aug 2020 02:37:47 +0200
Subject: [PATCH] Refactor Docker config

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
---
 .github/workflows/ci.yml |  4 ++--
 dist/index.js            | 37 ++++++++++++++++++++++---------------
 src/buildx.ts            | 24 +++---------------------
 src/docker.ts            | 24 ++++++++++++++++++++++++
 4 files changed, 51 insertions(+), 38 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index b83f492..be898a9 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -22,7 +22,7 @@ jobs:
         name: Checkout
         uses: actions/checkout@v2.3.1
       -
-        name: Build
+        name: Build and push
         uses: ./
         with:
           context: ./test
@@ -68,7 +68,7 @@ jobs:
           context: ./test
           file: ./test/Dockerfile-${{ matrix.dockerfile }}
           builder: ${{ steps.buildx.outputs.builder }}
-          platforms: linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/386,linux/ppc64le,linux/s390x
+          platforms: linux/amd64,linux/arm64,linux/386
           #push: true
           tags: |
             localhost:5000/name/app:latest
diff --git a/dist/index.js b/dist/index.js
index d0beef2..23583e1 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1114,7 +1114,7 @@ run();
 /***/ }),
 
 /***/ 231:
-/***/ (function(__unusedmodule, exports) {
+/***/ (function(__unusedmodule, exports, __webpack_require__) {
 
 "use strict";
 
@@ -1127,8 +1127,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
         step((generator = generator.apply(thisArg, _arguments || [])).next());
     });
 };
+var __importDefault = (this && this.__importDefault) || function (mod) {
+    return (mod && mod.__esModule) ? mod : { "default": mod };
+};
 Object.defineProperty(exports, "__esModule", { value: true });
-exports.parseImage = void 0;
+exports.parseImage = exports.config = void 0;
+const path_1 = __importDefault(__webpack_require__(622));
+const os_1 = __importDefault(__webpack_require__(87));
+const fs_1 = __importDefault(__webpack_require__(747));
+function config() {
+    return __awaiter(this, void 0, void 0, function* () {
+        const dockerHome = process.env.DOCKER_CONFIG || path_1.default.join(os_1.default.homedir(), '.docker');
+        const file = path_1.default.join(dockerHome, 'config.json');
+        if (!fs_1.default.existsSync(file)) {
+            return;
+        }
+        return JSON.parse(fs_1.default.readFileSync(file, { encoding: 'utf-8' }));
+    });
+}
+exports.config = config;
 exports.parseImage = (image) => __awaiter(void 0, void 0, void 0, function* () {
     const match = image.match(/^(?:([^\/]+)\/)?(?:([^\/]+)\/)?([^@:\/]+)(?:[@:](.+))?$/);
     if (!match) {
@@ -1895,14 +1912,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
         step((generator = generator.apply(thisArg, _arguments || [])).next());
     });
 };
-var __importDefault = (this && this.__importDefault) || function (mod) {
-    return (mod && mod.__esModule) ? mod : { "default": mod };
-};
 Object.defineProperty(exports, "__esModule", { value: true });
 exports.use = exports.isInstalled = exports.isAvailable = void 0;
-const fs_1 = __importDefault(__webpack_require__(747));
-const path_1 = __importDefault(__webpack_require__(622));
-const os_1 = __importDefault(__webpack_require__(87));
+const docker = __importStar(__webpack_require__(231));
 const exec = __importStar(__webpack_require__(807));
 function isAvailable() {
     return __awaiter(this, void 0, void 0, function* () {
@@ -1918,13 +1930,8 @@ exports.isAvailable = isAvailable;
 function isInstalled() {
     var _a;
     return __awaiter(this, void 0, void 0, function* () {
-        const dockerHome = process.env.DOCKER_CONFIG || path_1.default.join(os_1.default.homedir(), '.docker');
-        const dockerCfgFile = path_1.default.join(dockerHome, 'config.json');
-        if (!fs_1.default.existsSync(dockerCfgFile)) {
-            return false;
-        }
-        const dockerCfg = JSON.parse(fs_1.default.readFileSync(dockerCfgFile, { encoding: 'utf-8' }));
-        return ((_a = dockerCfg.aliases) === null || _a === void 0 ? void 0 : _a.builder) == 'buildx';
+        const dockerCfg = yield docker.config();
+        return ((_a = dockerCfg === null || dockerCfg === void 0 ? void 0 : dockerCfg.aliases) === null || _a === void 0 ? void 0 : _a.builder) == 'buildx';
     });
 }
 exports.isInstalled = isInstalled;
diff --git a/src/buildx.ts b/src/buildx.ts
index 1790f55..88629b6 100644
--- a/src/buildx.ts
+++ b/src/buildx.ts
@@ -1,17 +1,6 @@
-import fs from 'fs';
-import path from 'path';
-import os from 'os';
+import * as docker from './docker';
 import * as exec from './exec';
 
-interface DockerConfig {
-  credsStore?: string;
-  experimental?: string;
-  stackOrchestrator?: string;
-  aliases?: {
-    builder?: string;
-  };
-}
-
 export async function isAvailable(): Promise<Boolean> {
   return await exec.exec(`docker`, ['buildx'], true).then(res => {
     if (res.stderr != '' && !res.success) {
@@ -22,15 +11,8 @@ export async function isAvailable(): Promise<Boolean> {
 }
 
 export async function isInstalled(): Promise<Boolean> {
-  const dockerHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
-
-  const dockerCfgFile: string = path.join(dockerHome, 'config.json');
-  if (!fs.existsSync(dockerCfgFile)) {
-    return false;
-  }
-
-  const dockerCfg: DockerConfig = JSON.parse(fs.readFileSync(dockerCfgFile, {encoding: 'utf-8'}));
-  return dockerCfg.aliases?.builder == 'buildx';
+  const dockerCfg = await docker.config();
+  return dockerCfg?.aliases?.builder == 'buildx';
 }
 
 export async function use(builder: string): Promise<void> {
diff --git a/src/docker.ts b/src/docker.ts
index 64f747b..873a1b7 100644
--- a/src/docker.ts
+++ b/src/docker.ts
@@ -1,3 +1,16 @@
+import path from 'path';
+import os from 'os';
+import fs from 'fs';
+
+export interface Config {
+  credsStore?: string;
+  experimental?: string;
+  stackOrchestrator?: string;
+  aliases?: {
+    builder?: string;
+  };
+}
+
 export interface Image {
   registry?: string;
   namespace?: string;
@@ -5,6 +18,17 @@ export interface Image {
   tag?: string;
 }
 
+export async function config(): Promise<Config | undefined> {
+  const dockerHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
+
+  const file: string = path.join(dockerHome, 'config.json');
+  if (!fs.existsSync(file)) {
+    return;
+  }
+
+  return JSON.parse(fs.readFileSync(file, {encoding: 'utf-8'})) as Config;
+}
+
 export const parseImage = async (image: string): Promise<Image | undefined> => {
   const match = image.match(/^(?:([^\/]+)\/)?(?:([^\/]+)\/)?([^@:\/]+)(?:[@:](.+))?$/);
   if (!match) {