-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
install.ts
149 lines (122 loc) · 4.07 KB
/
install.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import * as core from "@actions/core"
import * as tc from "@actions/tool-cache"
import { exec, ExecOptions } from "child_process"
import os from "os"
import path from "path"
import { promisify } from "util"
import { VersionConfig } from "./version"
const execShellCommand = promisify(exec)
const downloadURL = "https://github.com/golangci/golangci-lint/releases/download"
const getAssetURL = (versionConfig: VersionConfig): 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 "x64":
arch = "amd64"
break
case "x32":
case "ia32":
arch = "386"
break
}
const noPrefix = versionConfig.TargetVersion.slice(1)
return `${downloadURL}/${versionConfig.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`
}
export enum InstallMode {
Binary = "binary",
GoInstall = "goinstall",
None = "none",
}
type ExecRes = {
stdout: string
stderr: string
}
const printOutput = (res: ExecRes): void => {
if (res.stdout) {
core.info(res.stdout)
}
if (res.stderr) {
core.info(res.stderr)
}
}
/**
* Install golangci-lint.
*
* @param versionConfig information about version to install.
* @param mode installation mode.
* @returns path to installed binary of golangci-lint.
*/
export async function installLint(versionConfig: VersionConfig, mode: InstallMode): Promise<string> {
core.info(`Installation mode: ${mode}`)
switch (mode) {
case InstallMode.Binary:
return installBin(versionConfig)
case InstallMode.GoInstall:
return goInstall(versionConfig)
default:
return installBin(versionConfig)
}
}
/**
* Install golangci-lint via `go install`.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
export async function goInstall(versionConfig: VersionConfig): Promise<string> {
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`)
const startedAt = Date.now()
const options: ExecOptions = { env: { ...process.env, CGO_ENABLED: "1" } }
const exres = await execShellCommand(
`go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`,
options
)
printOutput(exres)
const res = await execShellCommand(
`go install -n github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`,
options
)
printOutput(res)
// The output of `go install -n` when the binary is already installed is `touch <path_to_the_binary>`.
const lintPath = res.stderr.trimStart().trimEnd().split(` `, 2)[1]
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`)
return lintPath
}
/**
* Install golangci-lint via the precompiled binary.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
export async function installBin(versionConfig: VersionConfig): Promise<string> {
core.info(`Installing golangci-lint binary ${versionConfig.TargetVersion}...`)
const startedAt = Date.now()
const assetURL = getAssetURL(versionConfig)
core.info(`Downloading binary ${assetURL} ...`)
const archivePath = await tc.downloadTool(assetURL)
let extractedDir = ""
let repl = /\.tar\.gz$/
if (assetURL.endsWith("zip")) {
extractedDir = await tc.extractZip(archivePath, process.env.HOME)
repl = /\.zip$/
} else {
// We want to always overwrite files if the local cache already has them
const args = ["xz"]
if (process.platform.toString() != "darwin") {
args.push("--overwrite")
}
extractedDir = await tc.extractTar(archivePath, process.env.HOME, args)
}
const urlParts = assetURL.split(`/`)
const dirName = urlParts[urlParts.length - 1].replace(repl, ``)
const lintPath = path.join(extractedDir, dirName, `golangci-lint`)
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`)
return lintPath
}