Skip to content

Commit

Permalink
feat: add install-mode (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Jun 11, 2023
1 parent 5be60c7 commit 185e7a2
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 32 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ jobs:
args: --timeout=3m --issues-exit-code=0 ./sample/...
only-new-issues: true

test-go-install: # make sure the action works on a clean machine without building (go-install mode)
needs: [ build ]
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
version:
- ""
- "latest"
- "v1.53.2"
- "b5093688c0d3008eaacd6066773a1a52e689252f"
runs-on: ${{ matrix.os }}
permissions:
contents: read
pull-requests: read
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
cache: false # setup-go v4 caches by default
- uses: ./
with:
version: ${{ matrix.version }}
args: --timeout=3m --issues-exit-code=0 ./sample/...
only-new-issues: true
install-mode: goinstall

test-go-mod-version:
needs: [ build ]
strategy:
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ jobs:

# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
# skip-build-cache: true

# Optional:The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
```

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

# Optional: show only new issues if it's a pull request. The default value is `false`.
# only-new-issues: true

# Optional:The mode to install golangci-lint. It can be 'binary' or 'goinstall'.
# install-mode: "goinstall"
```

You will also likely need to add the following `.gitattributes` file to ensure that line endings for windows builds are properly formatted:
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ inputs:
description: "if set to true then the action doesn't cache or restore ~/.cache/go-build."
default: false
required: false
install-mode:
description: "The mode to install golangci-lint. It can be 'binary' or 'goinstall'."
default: "binary"
required: false
runs:
using: "node16"
main: "dist/run/index.js"
Expand Down
91 changes: 80 additions & 11 deletions dist/post_run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66370,11 +66370,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.installLint = void 0;
exports.installBin = exports.goInstall = exports.installLint = exports.InstallMode = void 0;
const core = __importStar(__nccwpck_require__(2186));
const tc = __importStar(__nccwpck_require__(7784));
const child_process_1 = __nccwpck_require__(2081);
const os_1 = __importDefault(__nccwpck_require__(2037));
const path_1 = __importDefault(__nccwpck_require__(1017));
const util_1 = __nccwpck_require__(3837);
const execShellCommand = (0, util_1.promisify)(child_process_1.exec);
const downloadURL = "https://github.com/golangci/golangci-lint/releases/download";
const getAssetURL = (versionConfig) => {
let ext = "tar.gz";
Expand All @@ -66398,13 +66401,74 @@ const getAssetURL = (versionConfig) => {
const noPrefix = versionConfig.TargetVersion.slice(1);
return `${downloadURL}/${versionConfig.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`;
};
// The installLint returns path to installed binary of golangci-lint.
function installLint(versionConfig) {
var InstallMode;
(function (InstallMode) {
InstallMode["Binary"] = "binary";
InstallMode["GoInstall"] = "goinstall";
})(InstallMode = exports.InstallMode || (exports.InstallMode = {}));
const printOutput = (res) => {
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.
*/
function installLint(versionConfig, mode) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installation mode: ${mode}`);
switch (mode) {
case InstallMode.Binary:
return installBin(versionConfig);
case InstallMode.GoInstall:
return goInstall(versionConfig);
default:
return installBin(versionConfig);
}
});
}
exports.installLint = installLint;
/**
* Install golangci-lint via `go install`.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
function goInstall(versionConfig) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`);
const startedAt = Date.now();
const options = { env: Object.assign(Object.assign({}, process.env), { CGO_ENABLED: "1" }) };
const exres = yield execShellCommand(`go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, options);
printOutput(exres);
const res = yield 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;
});
}
exports.goInstall = goInstall;
/**
* Install golangci-lint via the precompiled binary.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
function installBin(versionConfig) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installing golangci-lint binary ${versionConfig.TargetVersion}...`);
const startedAt = Date.now();
const assetURL = getAssetURL(versionConfig);
core.info(`Downloading ${assetURL} ...`);
core.info(`Downloading binary ${assetURL} ...`);
const archivePath = yield tc.downloadTool(assetURL);
let extractedDir = "";
let repl = /\.tar\.gz$/;
Expand All @@ -66427,7 +66491,7 @@ function installLint(versionConfig) {
return lintPath;
});
}
exports.installLint = installLint;
exports.installBin = installBin;


/***/ }),
Expand Down Expand Up @@ -66486,8 +66550,9 @@ const writeFile = (0, util_1.promisify)(fs.writeFile);
const createTempDir = (0, util_1.promisify)(tmp_1.dir);
function prepareLint() {
return __awaiter(this, void 0, void 0, function* () {
const versionConfig = yield (0, version_1.findLintVersion)();
return yield (0, install_1.installLint)(versionConfig);
const mode = core.getInput("install-mode").toLowerCase();
const versionConfig = yield (0, version_1.findLintVersion)(mode);
return yield (0, install_1.installLint)(versionConfig, mode);
});
}
function fetchPatch() {
Expand Down Expand Up @@ -66548,11 +66613,10 @@ function prepareEnv() {
return __awaiter(this, void 0, void 0, function* () {
const startedAt = Date.now();
// Prepare cache, lint and go in parallel.
const restoreCachePromise = (0, cache_1.restoreCache)();
yield (0, cache_1.restoreCache)();
const prepareLintPromise = prepareLint();
const patchPromise = fetchPatch();
const lintPath = yield prepareLintPromise;
yield restoreCachePromise;
const patchPath = yield patchPromise;
core.info(`Prepared env in ${Date.now() - startedAt}ms`);
return { lintPath, patchPath };
Expand Down Expand Up @@ -66609,7 +66673,7 @@ function runLint(lintPath, patchPath) {
}
cmdArgs.cwd = path.resolve(workingDirectory);
}
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimRight();
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd();
core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`);
const startedAt = Date.now();
try {
Expand Down Expand Up @@ -66774,6 +66838,7 @@ const core = __importStar(__nccwpck_require__(2186));
const httpm = __importStar(__nccwpck_require__(6255));
const fs = __importStar(__nccwpck_require__(7147));
const path_1 = __importDefault(__nccwpck_require__(1017));
const install_1 = __nccwpck_require__(1649);
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/;
const modVersionRe = /github.com\/golangci\/golangci-lint\s(v.+)/;
const parseVersion = (s) => {
Expand Down Expand Up @@ -66858,9 +66923,13 @@ const getConfig = () => __awaiter(void 0, void 0, void 0, function* () {
throw new Error(`failed to get action config: ${exc.message}`);
}
});
function findLintVersion() {
function findLintVersion(mode) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Finding needed golangci-lint version...`);
if (mode == install_1.InstallMode.GoInstall) {
const v = core.getInput(`version`);
return { TargetVersion: v ? v : "latest", AssetURL: "github.com/golangci/golangci-lint" };
}
const reqLintVersion = getRequestedLintVersion();
// if the patched version is passed, just use it
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) {
Expand Down
91 changes: 80 additions & 11 deletions dist/run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66370,11 +66370,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.installLint = void 0;
exports.installBin = exports.goInstall = exports.installLint = exports.InstallMode = void 0;
const core = __importStar(__nccwpck_require__(2186));
const tc = __importStar(__nccwpck_require__(7784));
const child_process_1 = __nccwpck_require__(2081);
const os_1 = __importDefault(__nccwpck_require__(2037));
const path_1 = __importDefault(__nccwpck_require__(1017));
const util_1 = __nccwpck_require__(3837);
const execShellCommand = (0, util_1.promisify)(child_process_1.exec);
const downloadURL = "https://github.com/golangci/golangci-lint/releases/download";
const getAssetURL = (versionConfig) => {
let ext = "tar.gz";
Expand All @@ -66398,13 +66401,74 @@ const getAssetURL = (versionConfig) => {
const noPrefix = versionConfig.TargetVersion.slice(1);
return `${downloadURL}/${versionConfig.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`;
};
// The installLint returns path to installed binary of golangci-lint.
function installLint(versionConfig) {
var InstallMode;
(function (InstallMode) {
InstallMode["Binary"] = "binary";
InstallMode["GoInstall"] = "goinstall";
})(InstallMode = exports.InstallMode || (exports.InstallMode = {}));
const printOutput = (res) => {
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.
*/
function installLint(versionConfig, mode) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installation mode: ${mode}`);
switch (mode) {
case InstallMode.Binary:
return installBin(versionConfig);
case InstallMode.GoInstall:
return goInstall(versionConfig);
default:
return installBin(versionConfig);
}
});
}
exports.installLint = installLint;
/**
* Install golangci-lint via `go install`.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
function goInstall(versionConfig) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`);
const startedAt = Date.now();
const options = { env: Object.assign(Object.assign({}, process.env), { CGO_ENABLED: "1" }) };
const exres = yield execShellCommand(`go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, options);
printOutput(exres);
const res = yield 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;
});
}
exports.goInstall = goInstall;
/**
* Install golangci-lint via the precompiled binary.
*
* @param versionConfig information about version to install.
* @returns path to installed binary of golangci-lint.
*/
function installBin(versionConfig) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Installing golangci-lint binary ${versionConfig.TargetVersion}...`);
const startedAt = Date.now();
const assetURL = getAssetURL(versionConfig);
core.info(`Downloading ${assetURL} ...`);
core.info(`Downloading binary ${assetURL} ...`);
const archivePath = yield tc.downloadTool(assetURL);
let extractedDir = "";
let repl = /\.tar\.gz$/;
Expand All @@ -66427,7 +66491,7 @@ function installLint(versionConfig) {
return lintPath;
});
}
exports.installLint = installLint;
exports.installBin = installBin;


/***/ }),
Expand Down Expand Up @@ -66486,8 +66550,9 @@ const writeFile = (0, util_1.promisify)(fs.writeFile);
const createTempDir = (0, util_1.promisify)(tmp_1.dir);
function prepareLint() {
return __awaiter(this, void 0, void 0, function* () {
const versionConfig = yield (0, version_1.findLintVersion)();
return yield (0, install_1.installLint)(versionConfig);
const mode = core.getInput("install-mode").toLowerCase();
const versionConfig = yield (0, version_1.findLintVersion)(mode);
return yield (0, install_1.installLint)(versionConfig, mode);
});
}
function fetchPatch() {
Expand Down Expand Up @@ -66548,11 +66613,10 @@ function prepareEnv() {
return __awaiter(this, void 0, void 0, function* () {
const startedAt = Date.now();
// Prepare cache, lint and go in parallel.
const restoreCachePromise = (0, cache_1.restoreCache)();
yield (0, cache_1.restoreCache)();
const prepareLintPromise = prepareLint();
const patchPromise = fetchPatch();
const lintPath = yield prepareLintPromise;
yield restoreCachePromise;
const patchPath = yield patchPromise;
core.info(`Prepared env in ${Date.now() - startedAt}ms`);
return { lintPath, patchPath };
Expand Down Expand Up @@ -66609,7 +66673,7 @@ function runLint(lintPath, patchPath) {
}
cmdArgs.cwd = path.resolve(workingDirectory);
}
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimRight();
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd();
core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`);
const startedAt = Date.now();
try {
Expand Down Expand Up @@ -66774,6 +66838,7 @@ const core = __importStar(__nccwpck_require__(2186));
const httpm = __importStar(__nccwpck_require__(6255));
const fs = __importStar(__nccwpck_require__(7147));
const path_1 = __importDefault(__nccwpck_require__(1017));
const install_1 = __nccwpck_require__(1649);
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/;
const modVersionRe = /github.com\/golangci\/golangci-lint\s(v.+)/;
const parseVersion = (s) => {
Expand Down Expand Up @@ -66858,9 +66923,13 @@ const getConfig = () => __awaiter(void 0, void 0, void 0, function* () {
throw new Error(`failed to get action config: ${exc.message}`);
}
});
function findLintVersion() {
function findLintVersion(mode) {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Finding needed golangci-lint version...`);
if (mode == install_1.InstallMode.GoInstall) {
const v = core.getInput(`version`);
return { TargetVersion: v ? v : "latest", AssetURL: "github.com/golangci/golangci-lint" };
}
const reqLintVersion = getRequestedLintVersion();
// if the patched version is passed, just use it
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) {
Expand Down
Loading

0 comments on commit 185e7a2

Please sign in to comment.