chore: move functions

This commit is contained in:
Fernandez Ludovic 2025-02-13 21:42:03 +01:00
parent e0ebdd245e
commit f8b3ec1cbc
2 changed files with 71 additions and 67 deletions

View file

@ -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<string> {
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(<InstallMode>mode)
return await installBinary(versionInfo, <InstallMode>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<string> {
export async function installBinary(versionInfo: VersionInfo, mode: InstallMode): Promise<string> {
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<string> {
async function goInstall(versionInfo: VersionInfo): Promise<string> {
core.info(`Installing golangci-lint ${versionInfo.TargetVersion}...`)
const startedAt = Date.now()
@ -125,7 +117,7 @@ export async function goInstall(versionInfo: VersionInfo): Promise<string> {
* @param versionInfo information about version to install.
* @returns path to installed binary of golangci-lint.
*/
export async function installBin(versionInfo: VersionInfo): Promise<string> {
async function installBin(versionInfo: VersionInfo): Promise<string> {
core.info(`Installing golangci-lint binary ${versionInfo.TargetVersion}...`)
const startedAt = Date.now()
@ -158,3 +150,33 @@ export async function installBin(versionInfo: VersionInfo): Promise<string> {
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}`
}

View file

@ -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<string> {
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<Env> {
const startedAt = Date.now()
const versionInfo = await getVersion(<InstallMode>mode)
// Prepare cache, lint and go in parallel.
await restoreCache()
return await installLint(versionInfo, <InstallMode>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<string> {
@ -140,25 +141,6 @@ async function fetchPushPatch(ctx: Context): Promise<string> {
}
}
type Env = {
binPath: string
patchPath: string
}
async function prepareEnv(): Promise<Env> {
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