Skip to content

Commit f8b3ec1

Browse files
committed
chore: move functions
1 parent e0ebdd2 commit f8b3ec1

File tree

2 files changed

+71
-67
lines changed

2 files changed

+71
-67
lines changed

src/install.ts

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,12 @@ import { exec, ExecOptions } from "child_process"
44
import os from "os"
55
import path from "path"
66
import { promisify } from "util"
7+
import which from "which"
78

8-
import { VersionInfo } from "./version"
9+
import { getVersion, VersionInfo } from "./version"
910

1011
const execShellCommand = promisify(exec)
1112

12-
const getAssetURL = (versionInfo: VersionInfo): string => {
13-
let ext = "tar.gz"
14-
15-
let platform = os.platform().toString()
16-
switch (platform) {
17-
case "win32":
18-
platform = "windows"
19-
ext = "zip"
20-
break
21-
}
22-
23-
let arch = os.arch()
24-
switch (arch) {
25-
case "arm64":
26-
arch = "arm64"
27-
break
28-
case "x64":
29-
arch = "amd64"
30-
break
31-
case "x32":
32-
case "ia32":
33-
arch = "386"
34-
break
35-
}
36-
37-
const noPrefix = versionInfo.TargetVersion.slice(1)
38-
39-
return `https://github.com/golangci/golangci-lint/releases/download/${versionInfo.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`
40-
}
41-
4213
export enum InstallMode {
4314
Binary = "binary",
4415
GoInstall = "goinstall",
@@ -59,14 +30,35 @@ const printOutput = (res: ExecRes): void => {
5930
}
6031
}
6132

33+
/**
34+
* Install golangci-lint.
35+
*
36+
* @returns path to installed binary of golangci-lint.
37+
*/
38+
export async function install(): Promise<string> {
39+
const mode = core.getInput("install-mode").toLowerCase()
40+
41+
if (mode === InstallMode.None) {
42+
const binPath = await which("golangci-lint", { nothrow: true })
43+
if (!binPath) {
44+
throw new Error("golangci-lint binary not found in the PATH")
45+
}
46+
return binPath
47+
}
48+
49+
const versionInfo = await getVersion(<InstallMode>mode)
50+
51+
return await installBinary(versionInfo, <InstallMode>mode)
52+
}
53+
6254
/**
6355
* Install golangci-lint.
6456
*
6557
* @param versionInfo information about version to install.
6658
* @param mode installation mode.
6759
* @returns path to installed binary of golangci-lint.
6860
*/
69-
export async function installLint(versionInfo: VersionInfo, mode: InstallMode): Promise<string> {
61+
export async function installBinary(versionInfo: VersionInfo, mode: InstallMode): Promise<string> {
7062
core.info(`Installation mode: ${mode}`)
7163

7264
switch (mode) {
@@ -85,7 +77,7 @@ export async function installLint(versionInfo: VersionInfo, mode: InstallMode):
8577
* @param versionInfo information about version to install.
8678
* @returns path to installed binary of golangci-lint.
8779
*/
88-
export async function goInstall(versionInfo: VersionInfo): Promise<string> {
80+
async function goInstall(versionInfo: VersionInfo): Promise<string> {
8981
core.info(`Installing golangci-lint ${versionInfo.TargetVersion}...`)
9082

9183
const startedAt = Date.now()
@@ -125,7 +117,7 @@ export async function goInstall(versionInfo: VersionInfo): Promise<string> {
125117
* @param versionInfo information about version to install.
126118
* @returns path to installed binary of golangci-lint.
127119
*/
128-
export async function installBin(versionInfo: VersionInfo): Promise<string> {
120+
async function installBin(versionInfo: VersionInfo): Promise<string> {
129121
core.info(`Installing golangci-lint binary ${versionInfo.TargetVersion}...`)
130122

131123
const startedAt = Date.now()
@@ -158,3 +150,33 @@ export async function installBin(versionInfo: VersionInfo): Promise<string> {
158150

159151
return binPath
160152
}
153+
154+
function getAssetURL(versionInfo: VersionInfo): string {
155+
let ext = "tar.gz"
156+
157+
let platform = os.platform().toString()
158+
switch (platform) {
159+
case "win32":
160+
platform = "windows"
161+
ext = "zip"
162+
break
163+
}
164+
165+
let arch = os.arch()
166+
switch (arch) {
167+
case "arm64":
168+
arch = "arm64"
169+
break
170+
case "x64":
171+
arch = "amd64"
172+
break
173+
case "x32":
174+
case "ia32":
175+
arch = "386"
176+
break
177+
}
178+
179+
const noPrefix = versionInfo.TargetVersion.slice(1)
180+
181+
return `https://github.com/golangci/golangci-lint/releases/download/${versionInfo.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`
182+
}

src/run.ts

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import * as fs from "fs"
66
import * as path from "path"
77
import { dir } from "tmp"
88
import { promisify } from "util"
9-
import which from "which"
109

1110
import { restoreCache, saveCache } from "./cache"
12-
import { installLint, InstallMode } from "./install"
11+
import { install } from "./install"
1312
import { alterDiffPatch } from "./utils/diffUtils"
14-
import { getVersion } from "./version"
1513

1614
const execShellCommand = promisify(exec)
1715
const writeFile = promisify(fs.writeFile)
@@ -21,20 +19,23 @@ function isOnlyNewIssues(): boolean {
2119
return core.getBooleanInput(`only-new-issues`, { required: true })
2220
}
2321

24-
async function prepareLint(): Promise<string> {
25-
const mode = core.getInput("install-mode").toLowerCase()
22+
type Env = {
23+
binPath: string
24+
patchPath: string
25+
}
2626

27-
if (mode === InstallMode.None) {
28-
const binPath = await which("golangci-lint", { nothrow: true })
29-
if (!binPath) {
30-
throw new Error("golangci-lint binary not found in the PATH")
31-
}
32-
return binPath
33-
}
27+
async function prepareEnv(): Promise<Env> {
28+
const startedAt = Date.now()
3429

35-
const versionInfo = await getVersion(<InstallMode>mode)
30+
// Prepare cache, lint and go in parallel.
31+
await restoreCache()
32+
33+
const binPath = await install()
34+
const patchPath = await fetchPatch()
35+
36+
core.info(`Prepared env in ${Date.now() - startedAt}ms`)
3637

37-
return await installLint(versionInfo, <InstallMode>mode)
38+
return { binPath, patchPath }
3839
}
3940

4041
async function fetchPatch(): Promise<string> {
@@ -140,25 +141,6 @@ async function fetchPushPatch(ctx: Context): Promise<string> {
140141
}
141142
}
142143

143-
type Env = {
144-
binPath: string
145-
patchPath: string
146-
}
147-
148-
async function prepareEnv(): Promise<Env> {
149-
const startedAt = Date.now()
150-
151-
// Prepare cache, lint and go in parallel.
152-
await restoreCache()
153-
154-
const binPath = await prepareLint()
155-
const patchPath = await fetchPatch()
156-
157-
core.info(`Prepared env in ${Date.now() - startedAt}ms`)
158-
159-
return { binPath, patchPath }
160-
}
161-
162144
type ExecRes = {
163145
stdout: string
164146
stderr: string

0 commit comments

Comments
 (0)