From 20d5541dabea6b62fc848972c80e978deac95aec Mon Sep 17 00:00:00 2001 From: Stephanie Hobbs Date: Thu, 21 May 2020 12:36:02 +0100 Subject: [PATCH] Add working-directory support (#18) Add working-directory support Fixes #15 --- .github/workflows/test.yml | 3 ++- action.yml | 8 ++++---- dist/post_run/index.js | 14 ++++++++++++-- dist/run/index.js | 14 ++++++++++++-- src/run.ts | 18 +++++++++++++++--- 5 files changed, 45 insertions(+), 12 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b3443a1..c856d76 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,4 +23,5 @@ jobs: - uses: ./ with: version: v1.26 - args: --issues-exit-code=0 ./sample/... \ No newline at end of file + args: --issues-exit-code=0 ./... + working-directory: sample diff --git a/action.yml b/action.yml index 47b94d9..2f05153 100644 --- a/action.yml +++ b/action.yml @@ -10,9 +10,9 @@ inputs: description: 'golangci-lint command line arguments' default: '' required: false - github-token: - description: 'GitHub token with scope `repo.public_repo`. Used for fetching a list of releases of golangci-lint.' - required: true + working-directory: + description: 'golangci-lint working directory, default is project root' + required: false runs: using: 'node12' @@ -20,4 +20,4 @@ runs: post: 'dist/post_run/index.js' branding: icon: 'shield' - color: 'yellow' \ No newline at end of file + color: 'yellow' diff --git a/dist/post_run/index.js b/dist/post_run/index.js index 5b636d5..f77fea1 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -2352,6 +2352,8 @@ var __importStar = (this && this.__importStar) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(__webpack_require__(470)); const child_process_1 = __webpack_require__(129); +const fs = __importStar(__webpack_require__(747)); +const path = __importStar(__webpack_require__(622)); const util_1 = __webpack_require__(669); const cache_1 = __webpack_require__(722); const install_1 = __webpack_require__(655); @@ -2396,11 +2398,19 @@ function runLint(lintPath) { if (args.includes(`-out-format`)) { throw new Error(`please, don't change out-format for golangci-lint: it can be broken in a future`); } + const workingDirectory = core.getInput(`working-directory`); + const cmdArgs = {}; + if (workingDirectory != ``) { + if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) { + throw new Error(`working-directory (${workingDirectory}) was not a path`); + } + cmdArgs.cwd = path.resolve(workingDirectory); + } const cmd = `${lintPath} run --out-format=github-actions ${args}`.trimRight(); - core.info(`Running [${cmd}] ...`); + core.info(`Running [${cmd}] in [${cmdArgs.cwd}] ...`); const startedAt = Date.now(); try { - const res = yield execShellCommand(cmd); + const res = yield execShellCommand(cmd, cmdArgs); printOutput(res); core.info(`golangci-lint found no issues`); } diff --git a/dist/run/index.js b/dist/run/index.js index ae3aaeb..1dcb0c4 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -2364,6 +2364,8 @@ var __importStar = (this && this.__importStar) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(__webpack_require__(470)); const child_process_1 = __webpack_require__(129); +const fs = __importStar(__webpack_require__(747)); +const path = __importStar(__webpack_require__(622)); const util_1 = __webpack_require__(669); const cache_1 = __webpack_require__(722); const install_1 = __webpack_require__(655); @@ -2408,11 +2410,19 @@ function runLint(lintPath) { if (args.includes(`-out-format`)) { throw new Error(`please, don't change out-format for golangci-lint: it can be broken in a future`); } + const workingDirectory = core.getInput(`working-directory`); + const cmdArgs = {}; + if (workingDirectory != ``) { + if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) { + throw new Error(`working-directory (${workingDirectory}) was not a path`); + } + cmdArgs.cwd = path.resolve(workingDirectory); + } const cmd = `${lintPath} run --out-format=github-actions ${args}`.trimRight(); - core.info(`Running [${cmd}] ...`); + core.info(`Running [${cmd}] in [${cmdArgs.cwd}] ...`); const startedAt = Date.now(); try { - const res = yield execShellCommand(cmd); + const res = yield execShellCommand(cmd, cmdArgs); printOutput(res); core.info(`golangci-lint found no issues`); } diff --git a/src/run.ts b/src/run.ts index 4656a5a..ae08a99 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,5 +1,7 @@ import * as core from "@actions/core" -import { exec } from "child_process" +import { exec, ExecOptions } from "child_process" +import * as fs from "fs" +import * as path from "path" import { promisify } from "util" import { restoreCache, saveCache } from "./cache" @@ -55,11 +57,21 @@ async function runLint(lintPath: string): Promise { throw new Error(`please, don't change out-format for golangci-lint: it can be broken in a future`) } + const workingDirectory = core.getInput(`working-directory`) + const cmdArgs: ExecOptions = {} + if (workingDirectory != ``) { + if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) { + throw new Error(`working-directory (${workingDirectory}) was not a path`) + } + + cmdArgs.cwd = path.resolve(workingDirectory) + } + const cmd = `${lintPath} run --out-format=github-actions ${args}`.trimRight() - core.info(`Running [${cmd}] ...`) + core.info(`Running [${cmd}] in [${cmdArgs.cwd}] ...`) const startedAt = Date.now() try { - const res = await execShellCommand(cmd) + const res = await execShellCommand(cmd, cmdArgs) printOutput(res) core.info(`golangci-lint found no issues`) } catch (exc) {