Skip to content

Commit

Permalink
feat(cli): implement --stdin-filepath flag (#1712)
Browse files Browse the repository at this point in the history
* feat(cli): implement --stdin-filepath flag

* chore: missing path in tsconfig.json
  • Loading branch information
P0lip authored Jul 6, 2021
1 parent efa5c50 commit 45b15a2
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 21 deletions.
38 changes: 19 additions & 19 deletions docs/guides/2-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ spectral lint ./reference/**/*.oas*.{json,yml,yaml}
Other options include:

```text
--version Show version number [boolean]
--help Show help [boolean]
-e, --encoding text encoding to use
[string] [choices: "utf8", "ascii", "utf-8", "utf16le", "ucs2", "ucs-2", "base64", "latin1"] [default: "utf8"]
--format, -f formatter to use for outputting results
[string] [choices: "json", "stylish", "junit", "html", "text", "teamcity", "pretty"] [default: "stylish"]
--output, -o output to a file instead of stdout [string]
--resolver path to custom json-ref-resolver instance [string]
--ruleset, -r path/URL to a ruleset file [string]
--fail-severity, -F results of this level or above will trigger a failure exit code
[string] [choices: "error", "warn", "info", "hint"] [default: "error"]
--display-only-failures, -D only output results equal to or greater than --fail-severity
[boolean] [default: false]
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
--verbose, -v increase verbosity [boolean]
--quiet, -q no logging - output only [boolean]
--version Show version number [boolean]
--help Show help [boolean]
-e, --encoding text encoding to use
[string] [choices: "utf8", "ascii", "utf-8", "utf16le", "ucs2", "ucs-2", "base64", "latin1"] [default: "utf8"]
-f, --format formatter to use for outputting results
[string] [choices: "json", "stylish", "junit", "html", "text", "teamcity", "pretty"] [default: "stylish"]
-o, --output output to a file instead of stdout [string]
--stdin-filepath path to a file to pretend that stdin comes from [string]
--resolver path to custom json-ref-resolver instance [string]
-r, --ruleset path/URL to a ruleset file [string]
-F, --fail-severity results of this level or above will trigger a failure exit code
[string] [choices: "error", "warn", "info", "hint"] [default: "error"]
-D, --display-only-failures only output results equal to or greater than --fail-severity [boolean] [default: false]
--ignore-unknown-format do not warn about unmatched formats [boolean] [default: false]
--fail-on-unmatched-globs fail on unmatched glob patterns [boolean] [default: false]
-v, --verbose increase verbosity [boolean]
-q, --quiet no logging - output only [boolean]
```

The Spectral CLI supports loading documents as YAML or JSON, and validation of OpenAPI v2/v3 documents via our built-in ruleset.
Expand All @@ -57,7 +57,7 @@ Spectral has a few different error severities: `error`, `warn`, `info` and `hint

The default behavior can be modified with the `--fail-severity=` option. Setting fail severity to `--fail-severity=info` would return a failure status code of 1 for any info results or higher. Using `--fail-severity=warn` will cause a failure status code for errors or warnings.

Changing the fail severity will not effect output. To change what results Spectral CLI prints to the screen, add the `--display-only-failures` switch (or just `-D` for short). This will strip out any results which are below the specified fail severity.
Changing the fail severity will not affect output. To change what results Spectral CLI prints to the screen, add the `--display-only-failures` switch (or just `-D` for short). This will strip out any results which are below the specified fail severity.

## Proxying

Expand All @@ -83,4 +83,4 @@ module.exports = new Resolver({
});
```

You can learn more about `$ref` resolving in the [JS section](./3-javascript.md#using-custom-resolver).
You can learn more about `$ref` resolving in the [JS section](./3-javascript.md#using-a-custom-resolver).
6 changes: 6 additions & 0 deletions packages/cli/src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ const lintCommand: CommandModule = {
description: 'output to a file instead of stdout',
type: 'string',
},
'stdin-filepath': {
description: 'path to a file to pretend that stdin comes from',
type: 'string',
},
resolver: {
description: 'path to custom json-ref-resolver instance',
type: 'string',
Expand Down Expand Up @@ -115,6 +119,7 @@ const lintCommand: CommandModule = {
failSeverity,
displayOnlyFailures,
ruleset,
stdinFilepath,
format,
output,
encoding,
Expand All @@ -134,6 +139,7 @@ const lintCommand: CommandModule = {
ignoreUnknownFormat,
failOnUnmatchedGlobs,
ruleset,
stdinFilepath,
...pick<Partial<ILintConfig>, keyof ILintConfig>(config, ['verbose', 'quiet', 'resolver']),
})
.then(results => {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface ILintConfig {
output?: string;
resolver?: string;
ruleset?: string;
stdinFilepath?: string;
ignoreUnknownFormat: boolean;
failOnUnmatchedGlobs: boolean;
verbose?: boolean;
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/services/linter/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function lint(documents: Array<number | string>, flags: ILintConfig
console.info(`Linting ${targetUri}`);
}

const document = await createDocument(targetUri, { encoding: flags.encoding });
const document = await createDocument(targetUri, { encoding: flags.encoding }, flags.stdinFilepath ?? '<STDIN>');

results.push(
...(await spectral.run(document, {
Expand All @@ -53,10 +53,11 @@ export async function lint(documents: Array<number | string>, flags: ILintConfig
const createDocument = async (
identifier: string | number,
opts: IFileReadOptions,
source: string,
): Promise<Document<unknown, Parsers.YamlParserResult<unknown>>> => {
if (typeof identifier === 'string') {
return new Document(await readParsable(identifier, opts), Parsers.Yaml, identifier);
}

return new Document(await readFileDescriptor(identifier, opts), Parsers.Yaml, '<STDIN>');
return new Document(await readFileDescriptor(identifier, opts), Parsers.Yaml, source);
};
1 change: 1 addition & 0 deletions test-harness/scenarios/help-no-document.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Options:
-e, --encoding text encoding to use [string] [choices: "utf8", "ascii", "utf-8", "utf16le", "ucs2", "ucs-2", "base64", "latin1"] [default: "utf8"]
-f, --format formatter to use for outputting results [string] [choices: "json", "stylish", "junit", "html", "text", "teamcity", "pretty"] [default: "stylish"]
-o, --output output to a file instead of stdout [string]
--stdin-filepath path to a file to pretend that stdin comes from [string]
--resolver path to custom json-ref-resolver instance [string]
-r, --ruleset path/URL to a ruleset file [string]
-F, --fail-severity results of this level or above will trigger a failure exit code [string] [choices: "error", "warn", "info", "hint"] [default: "error"]
Expand Down
46 changes: 46 additions & 0 deletions test-harness/scenarios/stdin-filepath.scenario
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
====test====
Respects stdin filepath
====asset:ruleset.json====
{
"rules": {
"title-matches-stoplight": {
"message": "Info must contain Stoplight",
"given": "$.title",
"then": {
"function": "pattern",
"functionOptions": {
"match": "Stoplight"
}
}
},
"description-matches-stoplight": {
"message": "Info must contain Stoplight",
"given": "$.description",
"then": {
"function": "pattern",
"functionOptions": {
"match": "Stoplight"
}
}
}
},
"overrides": [
{
"files": ["**/*.yaml"],
"rules": {
"title-matches-stoplight": "error"
}
}
]
}
====asset:document.yaml====
description: Test
title: Oops
====command====
cat {asset:document.yaml} | {bin} lint --ruleset {asset:ruleset.json} --stdin-filepath={asset:document.yaml}
====stdout====
{asset:document.yaml}
1:14 warning description-matches-stoplight Info must contain Stoplight description
2:8 error title-matches-stoplight Info must contain Stoplight title

✖ 2 problems (1 error, 1 warning, 0 infos, 0 hints)
1 change: 1 addition & 0 deletions test-harness/scenarios/strict-options.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Options:
-e, --encoding text encoding to use [string] [choices: "utf8", "ascii", "utf-8", "utf16le", "ucs2", "ucs-2", "base64", "latin1"] [default: "utf8"]
-f, --format formatter to use for outputting results [string] [choices: "json", "stylish", "junit", "html", "text", "teamcity", "pretty"] [default: "stylish"]
-o, --output output to a file instead of stdout [string]
--stdin-filepath path to a file to pretend that stdin comes from [string]
--resolver path to custom json-ref-resolver instance [string]
-r, --ruleset path/URL to a ruleset file [string]
-F, --fail-severity results of this level or above will trigger a failure exit code [string] [choices: "error", "warn", "info", "hint"] [default: "error"]
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@stoplight/spectral-parsers": ["packages/parsers/src/index.ts"],
"@stoplight/spectral-ref-resolver": ["packages/ref-resolver/src/index.ts"],
"@stoplight/spectral-runtime": ["packages/runtime/src/index.ts"],
"@stoplight/spectral-ruleset-migrator": ["packages/ruleset-migrator/src/index.ts"],
"@stoplight/spectral-rulesets": ["packages/rulesets/src/index.ts"]
},
"moduleResolution": "node",
Expand Down

0 comments on commit 45b15a2

Please sign in to comment.