From f8b3ec1cbcf38e365ab3c8f8ac135c27f5ec4b9e Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 13 Feb 2025 21:42:03 +0100 Subject: [PATCH] chore: move functions --- src/install.ts | 90 +++++++++++++++++++++++++++++++------------------- src/run.ts | 48 +++++++++------------------ 2 files changed, 71 insertions(+), 67 deletions(-) diff --git a/src/install.ts b/src/install.ts index 75cb75a..960fa8d 100644 --- a/src/install.ts +++ b/src/install.ts @@ -4,41 +4,12 @@ import { exec, ExecOptions } from "child_process" import os from "os" import path from "path" import { promisify } from "util" +import which from "which" -import { VersionInfo } from "./version" +import { getVersion, VersionInfo } from "./version" const execShellCommand = promisify(exec) -const getAssetURL = (versionInfo: VersionInfo): string => { - let ext = "tar.gz" - - let platform = os.platform().toString() - switch (platform) { - case "win32": - platform = "windows" - ext = "zip" - break - } - - let arch = os.arch() - switch (arch) { - case "arm64": - arch = "arm64" - break - case "x64": - arch = "amd64" - break - case "x32": - case "ia32": - arch = "386" - break - } - - const noPrefix = versionInfo.TargetVersion.slice(1) - - return `https://github.com/golangci/golangci-lint/releases/download/${versionInfo.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}` -} - export enum InstallMode { Binary = "binary", GoInstall = "goinstall", @@ -59,6 +30,27 @@ const printOutput = (res: ExecRes): void => { } } +/** + * Install golangci-lint. + * + * @returns path to installed binary of golangci-lint. + */ +export async function install(): Promise { + const mode = core.getInput("install-mode").toLowerCase() + + if (mode === InstallMode.None) { + const binPath = await which("golangci-lint", { nothrow: true }) + if (!binPath) { + throw new Error("golangci-lint binary not found in the PATH") + } + return binPath + } + + const versionInfo = await getVersion(mode) + + return await installBinary(versionInfo, mode) +} + /** * Install golangci-lint. * @@ -66,7 +58,7 @@ const printOutput = (res: ExecRes): void => { * @param mode installation mode. * @returns path to installed binary of golangci-lint. */ -export async function installLint(versionInfo: VersionInfo, mode: InstallMode): Promise { +export async function installBinary(versionInfo: VersionInfo, mode: InstallMode): Promise { core.info(`Installation mode: ${mode}`) switch (mode) { @@ -85,7 +77,7 @@ export async function installLint(versionInfo: VersionInfo, mode: InstallMode): * @param versionInfo information about version to install. * @returns path to installed binary of golangci-lint. */ -export async function goInstall(versionInfo: VersionInfo): Promise { +async function goInstall(versionInfo: VersionInfo): Promise { core.info(`Installing golangci-lint ${versionInfo.TargetVersion}...`) const startedAt = Date.now() @@ -125,7 +117,7 @@ export async function goInstall(versionInfo: VersionInfo): Promise { * @param versionInfo information about version to install. * @returns path to installed binary of golangci-lint. */ -export async function installBin(versionInfo: VersionInfo): Promise { +async function installBin(versionInfo: VersionInfo): Promise { core.info(`Installing golangci-lint binary ${versionInfo.TargetVersion}...`) const startedAt = Date.now() @@ -158,3 +150,33 @@ export async function installBin(versionInfo: VersionInfo): Promise { return binPath } + +function getAssetURL(versionInfo: VersionInfo): string { + let ext = "tar.gz" + + let platform = os.platform().toString() + switch (platform) { + case "win32": + platform = "windows" + ext = "zip" + break + } + + let arch = os.arch() + switch (arch) { + case "arm64": + arch = "arm64" + break + case "x64": + arch = "amd64" + break + case "x32": + case "ia32": + arch = "386" + break + } + + const noPrefix = versionInfo.TargetVersion.slice(1) + + return `https://github.com/golangci/golangci-lint/releases/download/${versionInfo.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}` +} diff --git a/src/run.ts b/src/run.ts index da83f1e..9e5fa51 100644 --- a/src/run.ts +++ b/src/run.ts @@ -6,12 +6,10 @@ import * as fs from "fs" import * as path from "path" import { dir } from "tmp" import { promisify } from "util" -import which from "which" import { restoreCache, saveCache } from "./cache" -import { installLint, InstallMode } from "./install" +import { install } from "./install" import { alterDiffPatch } from "./utils/diffUtils" -import { getVersion } from "./version" const execShellCommand = promisify(exec) const writeFile = promisify(fs.writeFile) @@ -21,20 +19,23 @@ function isOnlyNewIssues(): boolean { return core.getBooleanInput(`only-new-issues`, { required: true }) } -async function prepareLint(): Promise { - const mode = core.getInput("install-mode").toLowerCase() +type Env = { + binPath: string + patchPath: string +} - if (mode === InstallMode.None) { - const binPath = await which("golangci-lint", { nothrow: true }) - if (!binPath) { - throw new Error("golangci-lint binary not found in the PATH") - } - return binPath - } +async function prepareEnv(): Promise { + const startedAt = Date.now() - const versionInfo = await getVersion(mode) + // Prepare cache, lint and go in parallel. + await restoreCache() - return await installLint(versionInfo, mode) + const binPath = await install() + const patchPath = await fetchPatch() + + core.info(`Prepared env in ${Date.now() - startedAt}ms`) + + return { binPath, patchPath } } async function fetchPatch(): Promise { @@ -140,25 +141,6 @@ async function fetchPushPatch(ctx: Context): Promise { } } -type Env = { - binPath: string - patchPath: string -} - -async function prepareEnv(): Promise { - const startedAt = Date.now() - - // Prepare cache, lint and go in parallel. - await restoreCache() - - const binPath = await prepareLint() - const patchPath = await fetchPatch() - - core.info(`Prepared env in ${Date.now() - startedAt}ms`) - - return { binPath, patchPath } -} - type ExecRes = { stdout: string stderr: string