Skip to content

Commit a2c0110

Browse files
authored
Add support for passing clippy arguments in comment-able file (#1)
* Added rudimentary arg-file support * Corrected argfile path * Added rebuilt index.js * `args` and `args-file` now mutually exclusive, added docs * Rebuilt dist * Removed unused testing code * Improved readme * Fixed typos, restored upstream code style
1 parent b5b5f21 commit a2c0110

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,35 @@ jobs:
5151
args: --all-features
5252
```
5353
54+
### With `args-file`
55+
```yaml
56+
name: Clippy check
57+
jobs:
58+
clippy_check:
59+
runs-on: ubuntu-latest
60+
steps:
61+
- uses: actions/checkout@v2
62+
- uses: actions-rs/toolchain@v1
63+
with:
64+
components: clippy
65+
override: true
66+
- uses: actions-rs/clippy-check@v1
67+
with:
68+
token: ${{ secrets.GITHUB_TOKEN }}
69+
args-file: "./clippy_args.txt"
70+
```
71+
72+
Example file-based args (`clippy_args.txt`)
73+
```sh
74+
# here's a comment
75+
--locked --all-targets --all-features --
76+
-D missing_docs
77+
-D unused_extern_crates
78+
-D warnings
79+
# another one
80+
-D clippy::complexity
81+
```
82+
5483
## Inputs
5584

5685
| Name | Required | Description | Type | Default |
@@ -60,10 +89,13 @@ jobs:
6089
| `args` | | Arguments for the `cargo clippy` command | string | |
6190
| `use-cross` | | Use [`cross`](https://github.com/rust-embedded/cross) instead of `cargo` | bool | false |
6291
| `name` | | Name of the created GitHub check. If running this action multiple times, each run must have a unique name. | string | clippy |
92+
| `args-file` | | Path to file containing line-delimited arguments for `cargo clippy` (`sh`-style comments supported) | string | |
6393

6494
For extra details about the `toolchain`, `args` and `use-cross` inputs,
6595
see [`cargo` Action](https://github.com/actions-rs/cargo#inputs) documentation.
6696

97+
**NOTE**: only specify one argument source, either `args` or `args-file`.
98+
6799
**NOTE**: if your workflow contains multiple instances of the `clippy-check` action you will need to give each invocation a unique name, using the `name` property described above.
68100
Check runs must have a unique name, and this prevents a later check run overriding a previous one within the same workflow.
69101

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ inputs:
1515
args:
1616
description: Arguments for the cargo command
1717
required: false
18+
args-file:
19+
description: Arguments defined in external file
20+
required: false
1821
use-cross:
1922
description: Use cross instead of cargo
2023
default: false

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/input.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface Input {
1313
args: string[],
1414
useCross: boolean,
1515
name: string,
16+
argsFilePath: string,
1617
}
1718

1819
export function get(): Input {
@@ -23,12 +24,14 @@ export function get(): Input {
2324
}
2425
const useCross = input.getInputBool('use-cross');
2526
const name = input.getInput('name');
27+
const argsFilePath = input.getInput('args-file')
2628

2729
return {
2830
token: input.getInput('token', {required: true}),
2931
args: args,
3032
useCross: useCross,
3133
toolchain: toolchain || undefined,
32-
name
34+
name,
35+
argsFilePath: argsFilePath
3336
}
3437
}

src/main.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {Cargo, Cross} from '@actions-rs/core';
66
import * as input from './input';
77
import {CheckRunner} from './check';
88

9+
import {readFileSync} from 'fs';
10+
911
export async function run(actionInput: input.Input): Promise<void> {
1012
const startedAt = new Date().toISOString();
1113

@@ -50,7 +52,17 @@ export async function run(actionInput: input.Input): Promise<void> {
5052
// of arguments and it will mess up the output.
5153
args.push('--message-format=json');
5254

53-
args = args.concat(actionInput.args);
55+
if (actionInput.args && actionInput.argsFilePath) {
56+
throw new Error('Only specify one argument source: `args` or `args-file`');
57+
}
58+
59+
if (actionInput.args) {
60+
args = args.concat(actionInput.args);
61+
}
62+
63+
if (actionInput.argsFilePath) {
64+
args = args.concat(parseArgsFile(actionInput.argsFilePath));
65+
}
5466

5567
let runner = new CheckRunner();
5668
let clippyExitCode: number = 0;
@@ -103,4 +115,26 @@ async function main(): Promise<void> {
103115
}
104116
}
105117

118+
/**
119+
* Parses a newline-delimited file of clippy args
120+
*
121+
* @remark sh-style comments are supported (using #)
122+
*
123+
* @param filePath - path of file that contains clippy arguments to parse
124+
* @returns parsed arguments as an array of strings
125+
*/
126+
function parseArgsFile(filePath: string): string[] {
127+
let parsedArgs: string[] = [];
128+
129+
const file = readFileSync(filePath, 'utf-8');
130+
131+
for (var line of file.split(/\r?\n/)) {
132+
if (!line.startsWith('#')) {
133+
parsedArgs = parsedArgs.concat(line.split(' '));
134+
}
135+
}
136+
137+
return parsedArgs;
138+
}
139+
106140
main();

0 commit comments

Comments
 (0)