Skip to content

Commit 185e7a2

Browse files
authored
feat: add install-mode (#768)
1 parent 5be60c7 commit 185e7a2

File tree

8 files changed

+316
-32
lines changed

8 files changed

+316
-32
lines changed

Diff for: .github/workflows/test.yml

+29
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,35 @@ jobs:
6969
args: --timeout=3m --issues-exit-code=0 ./sample/...
7070
only-new-issues: true
7171

72+
test-go-install: # make sure the action works on a clean machine without building (go-install mode)
73+
needs: [ build ]
74+
strategy:
75+
matrix:
76+
os:
77+
- ubuntu-latest
78+
- macos-latest
79+
- windows-latest
80+
version:
81+
- ""
82+
- "latest"
83+
- "v1.53.2"
84+
- "b5093688c0d3008eaacd6066773a1a52e689252f"
85+
runs-on: ${{ matrix.os }}
86+
permissions:
87+
contents: read
88+
pull-requests: read
89+
steps:
90+
- uses: actions/checkout@v3
91+
- uses: actions/setup-go@v4
92+
with:
93+
cache: false # setup-go v4 caches by default
94+
- uses: ./
95+
with:
96+
version: ${{ matrix.version }}
97+
args: --timeout=3m --issues-exit-code=0 ./sample/...
98+
only-new-issues: true
99+
install-mode: goinstall
100+
72101
test-go-mod-version:
73102
needs: [ build ]
74103
strategy:

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ jobs:
7070

7171
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
7272
# skip-build-cache: true
73+
74+
# Optional:The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
75+
# install-mode: "goinstall"
7376
```
7477

7578
We recommend running this action in a job separate from other jobs (`go test`, etc)
@@ -124,6 +127,9 @@ jobs:
124127

125128
# Optional: show only new issues if it's a pull request. The default value is `false`.
126129
# only-new-issues: true
130+
131+
# Optional:The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
132+
# install-mode: "goinstall"
127133
```
128134

129135
You will also likely need to add the following `.gitattributes` file to ensure that line endings for windows builds are properly formatted:

Diff for: action.yml

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ inputs:
3434
description: "if set to true then the action doesn't cache or restore ~/.cache/go-build."
3535
default: false
3636
required: false
37+
install-mode:
38+
description: "The mode to install golangci-lint. It can be 'binary' or 'goinstall'."
39+
default: "binary"
40+
required: false
3741
runs:
3842
using: "node16"
3943
main: "dist/run/index.js"

Diff for: dist/post_run/index.js

+80-11
Original file line numberDiff line numberDiff line change
@@ -66370,11 +66370,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6637066370
return (mod && mod.__esModule) ? mod : { "default": mod };
6637166371
};
6637266372
Object.defineProperty(exports, "__esModule", ({ value: true }));
66373-
exports.installLint = void 0;
66373+
exports.installBin = exports.goInstall = exports.installLint = exports.InstallMode = void 0;
6637466374
const core = __importStar(__nccwpck_require__(2186));
6637566375
const tc = __importStar(__nccwpck_require__(7784));
66376+
const child_process_1 = __nccwpck_require__(2081);
6637666377
const os_1 = __importDefault(__nccwpck_require__(2037));
6637766378
const path_1 = __importDefault(__nccwpck_require__(1017));
66379+
const util_1 = __nccwpck_require__(3837);
66380+
const execShellCommand = (0, util_1.promisify)(child_process_1.exec);
6637866381
const downloadURL = "https://github.com/golangci/golangci-lint/releases/download";
6637966382
const getAssetURL = (versionConfig) => {
6638066383
let ext = "tar.gz";
@@ -66398,13 +66401,74 @@ const getAssetURL = (versionConfig) => {
6639866401
const noPrefix = versionConfig.TargetVersion.slice(1);
6639966402
return `${downloadURL}/${versionConfig.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`;
6640066403
};
66401-
// The installLint returns path to installed binary of golangci-lint.
66402-
function installLint(versionConfig) {
66404+
var InstallMode;
66405+
(function (InstallMode) {
66406+
InstallMode["Binary"] = "binary";
66407+
InstallMode["GoInstall"] = "goinstall";
66408+
})(InstallMode = exports.InstallMode || (exports.InstallMode = {}));
66409+
const printOutput = (res) => {
66410+
if (res.stdout) {
66411+
core.info(res.stdout);
66412+
}
66413+
if (res.stderr) {
66414+
core.info(res.stderr);
66415+
}
66416+
};
66417+
/**
66418+
* Install golangci-lint.
66419+
*
66420+
* @param versionConfig information about version to install.
66421+
* @param mode installation mode.
66422+
* @returns path to installed binary of golangci-lint.
66423+
*/
66424+
function installLint(versionConfig, mode) {
66425+
return __awaiter(this, void 0, void 0, function* () {
66426+
core.info(`Installation mode: ${mode}`);
66427+
switch (mode) {
66428+
case InstallMode.Binary:
66429+
return installBin(versionConfig);
66430+
case InstallMode.GoInstall:
66431+
return goInstall(versionConfig);
66432+
default:
66433+
return installBin(versionConfig);
66434+
}
66435+
});
66436+
}
66437+
exports.installLint = installLint;
66438+
/**
66439+
* Install golangci-lint via `go install`.
66440+
*
66441+
* @param versionConfig information about version to install.
66442+
* @returns path to installed binary of golangci-lint.
66443+
*/
66444+
function goInstall(versionConfig) {
6640366445
return __awaiter(this, void 0, void 0, function* () {
6640466446
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`);
6640566447
const startedAt = Date.now();
66448+
const options = { env: Object.assign(Object.assign({}, process.env), { CGO_ENABLED: "1" }) };
66449+
const exres = yield execShellCommand(`go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, options);
66450+
printOutput(exres);
66451+
const res = yield execShellCommand(`go install -n github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, options);
66452+
printOutput(res);
66453+
// The output of `go install -n` when the binary is already installed is `touch <path_to_the_binary>`.
66454+
const lintPath = res.stderr.trimStart().trimEnd().split(` `, 2)[1];
66455+
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`);
66456+
return lintPath;
66457+
});
66458+
}
66459+
exports.goInstall = goInstall;
66460+
/**
66461+
* Install golangci-lint via the precompiled binary.
66462+
*
66463+
* @param versionConfig information about version to install.
66464+
* @returns path to installed binary of golangci-lint.
66465+
*/
66466+
function installBin(versionConfig) {
66467+
return __awaiter(this, void 0, void 0, function* () {
66468+
core.info(`Installing golangci-lint binary ${versionConfig.TargetVersion}...`);
66469+
const startedAt = Date.now();
6640666470
const assetURL = getAssetURL(versionConfig);
66407-
core.info(`Downloading ${assetURL} ...`);
66471+
core.info(`Downloading binary ${assetURL} ...`);
6640866472
const archivePath = yield tc.downloadTool(assetURL);
6640966473
let extractedDir = "";
6641066474
let repl = /\.tar\.gz$/;
@@ -66427,7 +66491,7 @@ function installLint(versionConfig) {
6642766491
return lintPath;
6642866492
});
6642966493
}
66430-
exports.installLint = installLint;
66494+
exports.installBin = installBin;
6643166495

6643266496

6643366497
/***/ }),
@@ -66486,8 +66550,9 @@ const writeFile = (0, util_1.promisify)(fs.writeFile);
6648666550
const createTempDir = (0, util_1.promisify)(tmp_1.dir);
6648766551
function prepareLint() {
6648866552
return __awaiter(this, void 0, void 0, function* () {
66489-
const versionConfig = yield (0, version_1.findLintVersion)();
66490-
return yield (0, install_1.installLint)(versionConfig);
66553+
const mode = core.getInput("install-mode").toLowerCase();
66554+
const versionConfig = yield (0, version_1.findLintVersion)(mode);
66555+
return yield (0, install_1.installLint)(versionConfig, mode);
6649166556
});
6649266557
}
6649366558
function fetchPatch() {
@@ -66548,11 +66613,10 @@ function prepareEnv() {
6654866613
return __awaiter(this, void 0, void 0, function* () {
6654966614
const startedAt = Date.now();
6655066615
// Prepare cache, lint and go in parallel.
66551-
const restoreCachePromise = (0, cache_1.restoreCache)();
66616+
yield (0, cache_1.restoreCache)();
6655266617
const prepareLintPromise = prepareLint();
6655366618
const patchPromise = fetchPatch();
6655466619
const lintPath = yield prepareLintPromise;
66555-
yield restoreCachePromise;
6655666620
const patchPath = yield patchPromise;
6655766621
core.info(`Prepared env in ${Date.now() - startedAt}ms`);
6655866622
return { lintPath, patchPath };
@@ -66609,7 +66673,7 @@ function runLint(lintPath, patchPath) {
6660966673
}
6661066674
cmdArgs.cwd = path.resolve(workingDirectory);
6661166675
}
66612-
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimRight();
66676+
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd();
6661366677
core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`);
6661466678
const startedAt = Date.now();
6661566679
try {
@@ -66774,6 +66838,7 @@ const core = __importStar(__nccwpck_require__(2186));
6677466838
const httpm = __importStar(__nccwpck_require__(6255));
6677566839
const fs = __importStar(__nccwpck_require__(7147));
6677666840
const path_1 = __importDefault(__nccwpck_require__(1017));
66841+
const install_1 = __nccwpck_require__(1649);
6677766842
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/;
6677866843
const modVersionRe = /github.com\/golangci\/golangci-lint\s(v.+)/;
6677966844
const parseVersion = (s) => {
@@ -66858,9 +66923,13 @@ const getConfig = () => __awaiter(void 0, void 0, void 0, function* () {
6685866923
throw new Error(`failed to get action config: ${exc.message}`);
6685966924
}
6686066925
});
66861-
function findLintVersion() {
66926+
function findLintVersion(mode) {
6686266927
return __awaiter(this, void 0, void 0, function* () {
6686366928
core.info(`Finding needed golangci-lint version...`);
66929+
if (mode == install_1.InstallMode.GoInstall) {
66930+
const v = core.getInput(`version`);
66931+
return { TargetVersion: v ? v : "latest", AssetURL: "github.com/golangci/golangci-lint" };
66932+
}
6686466933
const reqLintVersion = getRequestedLintVersion();
6686566934
// if the patched version is passed, just use it
6686666935
if ((reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.major) !== null && (reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.minor) != null && (reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.patch) !== null) {

Diff for: dist/run/index.js

+80-11
Original file line numberDiff line numberDiff line change
@@ -66370,11 +66370,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6637066370
return (mod && mod.__esModule) ? mod : { "default": mod };
6637166371
};
6637266372
Object.defineProperty(exports, "__esModule", ({ value: true }));
66373-
exports.installLint = void 0;
66373+
exports.installBin = exports.goInstall = exports.installLint = exports.InstallMode = void 0;
6637466374
const core = __importStar(__nccwpck_require__(2186));
6637566375
const tc = __importStar(__nccwpck_require__(7784));
66376+
const child_process_1 = __nccwpck_require__(2081);
6637666377
const os_1 = __importDefault(__nccwpck_require__(2037));
6637766378
const path_1 = __importDefault(__nccwpck_require__(1017));
66379+
const util_1 = __nccwpck_require__(3837);
66380+
const execShellCommand = (0, util_1.promisify)(child_process_1.exec);
6637866381
const downloadURL = "https://github.com/golangci/golangci-lint/releases/download";
6637966382
const getAssetURL = (versionConfig) => {
6638066383
let ext = "tar.gz";
@@ -66398,13 +66401,74 @@ const getAssetURL = (versionConfig) => {
6639866401
const noPrefix = versionConfig.TargetVersion.slice(1);
6639966402
return `${downloadURL}/${versionConfig.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`;
6640066403
};
66401-
// The installLint returns path to installed binary of golangci-lint.
66402-
function installLint(versionConfig) {
66404+
var InstallMode;
66405+
(function (InstallMode) {
66406+
InstallMode["Binary"] = "binary";
66407+
InstallMode["GoInstall"] = "goinstall";
66408+
})(InstallMode = exports.InstallMode || (exports.InstallMode = {}));
66409+
const printOutput = (res) => {
66410+
if (res.stdout) {
66411+
core.info(res.stdout);
66412+
}
66413+
if (res.stderr) {
66414+
core.info(res.stderr);
66415+
}
66416+
};
66417+
/**
66418+
* Install golangci-lint.
66419+
*
66420+
* @param versionConfig information about version to install.
66421+
* @param mode installation mode.
66422+
* @returns path to installed binary of golangci-lint.
66423+
*/
66424+
function installLint(versionConfig, mode) {
66425+
return __awaiter(this, void 0, void 0, function* () {
66426+
core.info(`Installation mode: ${mode}`);
66427+
switch (mode) {
66428+
case InstallMode.Binary:
66429+
return installBin(versionConfig);
66430+
case InstallMode.GoInstall:
66431+
return goInstall(versionConfig);
66432+
default:
66433+
return installBin(versionConfig);
66434+
}
66435+
});
66436+
}
66437+
exports.installLint = installLint;
66438+
/**
66439+
* Install golangci-lint via `go install`.
66440+
*
66441+
* @param versionConfig information about version to install.
66442+
* @returns path to installed binary of golangci-lint.
66443+
*/
66444+
function goInstall(versionConfig) {
6640366445
return __awaiter(this, void 0, void 0, function* () {
6640466446
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`);
6640566447
const startedAt = Date.now();
66448+
const options = { env: Object.assign(Object.assign({}, process.env), { CGO_ENABLED: "1" }) };
66449+
const exres = yield execShellCommand(`go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, options);
66450+
printOutput(exres);
66451+
const res = yield execShellCommand(`go install -n github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, options);
66452+
printOutput(res);
66453+
// The output of `go install -n` when the binary is already installed is `touch <path_to_the_binary>`.
66454+
const lintPath = res.stderr.trimStart().trimEnd().split(` `, 2)[1];
66455+
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`);
66456+
return lintPath;
66457+
});
66458+
}
66459+
exports.goInstall = goInstall;
66460+
/**
66461+
* Install golangci-lint via the precompiled binary.
66462+
*
66463+
* @param versionConfig information about version to install.
66464+
* @returns path to installed binary of golangci-lint.
66465+
*/
66466+
function installBin(versionConfig) {
66467+
return __awaiter(this, void 0, void 0, function* () {
66468+
core.info(`Installing golangci-lint binary ${versionConfig.TargetVersion}...`);
66469+
const startedAt = Date.now();
6640666470
const assetURL = getAssetURL(versionConfig);
66407-
core.info(`Downloading ${assetURL} ...`);
66471+
core.info(`Downloading binary ${assetURL} ...`);
6640866472
const archivePath = yield tc.downloadTool(assetURL);
6640966473
let extractedDir = "";
6641066474
let repl = /\.tar\.gz$/;
@@ -66427,7 +66491,7 @@ function installLint(versionConfig) {
6642766491
return lintPath;
6642866492
});
6642966493
}
66430-
exports.installLint = installLint;
66494+
exports.installBin = installBin;
6643166495

6643266496

6643366497
/***/ }),
@@ -66486,8 +66550,9 @@ const writeFile = (0, util_1.promisify)(fs.writeFile);
6648666550
const createTempDir = (0, util_1.promisify)(tmp_1.dir);
6648766551
function prepareLint() {
6648866552
return __awaiter(this, void 0, void 0, function* () {
66489-
const versionConfig = yield (0, version_1.findLintVersion)();
66490-
return yield (0, install_1.installLint)(versionConfig);
66553+
const mode = core.getInput("install-mode").toLowerCase();
66554+
const versionConfig = yield (0, version_1.findLintVersion)(mode);
66555+
return yield (0, install_1.installLint)(versionConfig, mode);
6649166556
});
6649266557
}
6649366558
function fetchPatch() {
@@ -66548,11 +66613,10 @@ function prepareEnv() {
6654866613
return __awaiter(this, void 0, void 0, function* () {
6654966614
const startedAt = Date.now();
6655066615
// Prepare cache, lint and go in parallel.
66551-
const restoreCachePromise = (0, cache_1.restoreCache)();
66616+
yield (0, cache_1.restoreCache)();
6655266617
const prepareLintPromise = prepareLint();
6655366618
const patchPromise = fetchPatch();
6655466619
const lintPath = yield prepareLintPromise;
66555-
yield restoreCachePromise;
6655666620
const patchPath = yield patchPromise;
6655766621
core.info(`Prepared env in ${Date.now() - startedAt}ms`);
6655866622
return { lintPath, patchPath };
@@ -66609,7 +66673,7 @@ function runLint(lintPath, patchPath) {
6660966673
}
6661066674
cmdArgs.cwd = path.resolve(workingDirectory);
6661166675
}
66612-
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimRight();
66676+
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd();
6661366677
core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`);
6661466678
const startedAt = Date.now();
6661566679
try {
@@ -66774,6 +66838,7 @@ const core = __importStar(__nccwpck_require__(2186));
6677466838
const httpm = __importStar(__nccwpck_require__(6255));
6677566839
const fs = __importStar(__nccwpck_require__(7147));
6677666840
const path_1 = __importDefault(__nccwpck_require__(1017));
66841+
const install_1 = __nccwpck_require__(1649);
6677766842
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/;
6677866843
const modVersionRe = /github.com\/golangci\/golangci-lint\s(v.+)/;
6677966844
const parseVersion = (s) => {
@@ -66858,9 +66923,13 @@ const getConfig = () => __awaiter(void 0, void 0, void 0, function* () {
6685866923
throw new Error(`failed to get action config: ${exc.message}`);
6685966924
}
6686066925
});
66861-
function findLintVersion() {
66926+
function findLintVersion(mode) {
6686266927
return __awaiter(this, void 0, void 0, function* () {
6686366928
core.info(`Finding needed golangci-lint version...`);
66929+
if (mode == install_1.InstallMode.GoInstall) {
66930+
const v = core.getInput(`version`);
66931+
return { TargetVersion: v ? v : "latest", AssetURL: "github.com/golangci/golangci-lint" };
66932+
}
6686466933
const reqLintVersion = getRequestedLintVersion();
6686566934
// if the patched version is passed, just use it
6686666935
if ((reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.major) !== null && (reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.minor) != null && (reqLintVersion === null || reqLintVersion === void 0 ? void 0 : reqLintVersion.patch) !== null) {

0 commit comments

Comments
 (0)