Skip to content

Commit 20d5541

Browse files
authored
Add working-directory support (#18)
Add working-directory support Fixes #15
1 parent 85a3a6a commit 20d5541

File tree

5 files changed

+45
-12
lines changed

5 files changed

+45
-12
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ jobs:
2323
- uses: ./
2424
with:
2525
version: v1.26
26-
args: --issues-exit-code=0 ./sample/...
26+
args: --issues-exit-code=0 ./...
27+
working-directory: sample

action.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ inputs:
1010
description: 'golangci-lint command line arguments'
1111
default: ''
1212
required: false
13-
github-token:
14-
description: 'GitHub token with scope `repo.public_repo`. Used for fetching a list of releases of golangci-lint.'
15-
required: true
13+
working-directory:
14+
description: 'golangci-lint working directory, default is project root'
15+
required: false
1616

1717
runs:
1818
using: 'node12'
1919
main: 'dist/run/index.js'
2020
post: 'dist/post_run/index.js'
2121
branding:
2222
icon: 'shield'
23-
color: 'yellow'
23+
color: 'yellow'

dist/post_run/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,6 +2352,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
23522352
Object.defineProperty(exports, "__esModule", { value: true });
23532353
const core = __importStar(__webpack_require__(470));
23542354
const child_process_1 = __webpack_require__(129);
2355+
const fs = __importStar(__webpack_require__(747));
2356+
const path = __importStar(__webpack_require__(622));
23552357
const util_1 = __webpack_require__(669);
23562358
const cache_1 = __webpack_require__(722);
23572359
const install_1 = __webpack_require__(655);
@@ -2396,11 +2398,19 @@ function runLint(lintPath) {
23962398
if (args.includes(`-out-format`)) {
23972399
throw new Error(`please, don't change out-format for golangci-lint: it can be broken in a future`);
23982400
}
2401+
const workingDirectory = core.getInput(`working-directory`);
2402+
const cmdArgs = {};
2403+
if (workingDirectory != ``) {
2404+
if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) {
2405+
throw new Error(`working-directory (${workingDirectory}) was not a path`);
2406+
}
2407+
cmdArgs.cwd = path.resolve(workingDirectory);
2408+
}
23992409
const cmd = `${lintPath} run --out-format=github-actions ${args}`.trimRight();
2400-
core.info(`Running [${cmd}] ...`);
2410+
core.info(`Running [${cmd}] in [${cmdArgs.cwd}] ...`);
24012411
const startedAt = Date.now();
24022412
try {
2403-
const res = yield execShellCommand(cmd);
2413+
const res = yield execShellCommand(cmd, cmdArgs);
24042414
printOutput(res);
24052415
core.info(`golangci-lint found no issues`);
24062416
}

dist/run/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,8 @@ var __importStar = (this && this.__importStar) || function (mod) {
23642364
Object.defineProperty(exports, "__esModule", { value: true });
23652365
const core = __importStar(__webpack_require__(470));
23662366
const child_process_1 = __webpack_require__(129);
2367+
const fs = __importStar(__webpack_require__(747));
2368+
const path = __importStar(__webpack_require__(622));
23672369
const util_1 = __webpack_require__(669);
23682370
const cache_1 = __webpack_require__(722);
23692371
const install_1 = __webpack_require__(655);
@@ -2408,11 +2410,19 @@ function runLint(lintPath) {
24082410
if (args.includes(`-out-format`)) {
24092411
throw new Error(`please, don't change out-format for golangci-lint: it can be broken in a future`);
24102412
}
2413+
const workingDirectory = core.getInput(`working-directory`);
2414+
const cmdArgs = {};
2415+
if (workingDirectory != ``) {
2416+
if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) {
2417+
throw new Error(`working-directory (${workingDirectory}) was not a path`);
2418+
}
2419+
cmdArgs.cwd = path.resolve(workingDirectory);
2420+
}
24112421
const cmd = `${lintPath} run --out-format=github-actions ${args}`.trimRight();
2412-
core.info(`Running [${cmd}] ...`);
2422+
core.info(`Running [${cmd}] in [${cmdArgs.cwd}] ...`);
24132423
const startedAt = Date.now();
24142424
try {
2415-
const res = yield execShellCommand(cmd);
2425+
const res = yield execShellCommand(cmd, cmdArgs);
24162426
printOutput(res);
24172427
core.info(`golangci-lint found no issues`);
24182428
}

src/run.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as core from "@actions/core"
2-
import { exec } from "child_process"
2+
import { exec, ExecOptions } from "child_process"
3+
import * as fs from "fs"
4+
import * as path from "path"
35
import { promisify } from "util"
46

57
import { restoreCache, saveCache } from "./cache"
@@ -55,11 +57,21 @@ async function runLint(lintPath: string): Promise<void> {
5557
throw new Error(`please, don't change out-format for golangci-lint: it can be broken in a future`)
5658
}
5759

60+
const workingDirectory = core.getInput(`working-directory`)
61+
const cmdArgs: ExecOptions = {}
62+
if (workingDirectory != ``) {
63+
if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) {
64+
throw new Error(`working-directory (${workingDirectory}) was not a path`)
65+
}
66+
67+
cmdArgs.cwd = path.resolve(workingDirectory)
68+
}
69+
5870
const cmd = `${lintPath} run --out-format=github-actions ${args}`.trimRight()
59-
core.info(`Running [${cmd}] ...`)
71+
core.info(`Running [${cmd}] in [${cmdArgs.cwd}] ...`)
6072
const startedAt = Date.now()
6173
try {
62-
const res = await execShellCommand(cmd)
74+
const res = await execShellCommand(cmd, cmdArgs)
6375
printOutput(res)
6476
core.info(`golangci-lint found no issues`)
6577
} catch (exc) {

0 commit comments

Comments
 (0)