Skip to content

Commit d8276a6

Browse files
committed
Add a Stdin Specification
This completes step 1 of #573 (comment).
1 parent cb8d3fa commit d8276a6

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

docs/site/reference/formatter-spec.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ The formatter's CLI must be of the form:
3030
Where:
3131

3232
- `<command>` is the name of the formatter executable.
33-
- `[options]` is any number of flags and options that the formatter accepts.
34-
- `[...<files>]` is one or more files given to the formatter for processing.
33+
- `[options]` are any number of flags and options that the formatter accepts.
34+
- `[...<files>]` are one or more files given to the formatter for processing.
3535

3636
Example:
3737

docs/site/reference/stdin-spec.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
outline: deep
3+
---
4+
5+
# Stdin Specification
6+
7+
Formatters **MAY** also implement the Stdin Specification, which allows
8+
formatting "virtual files" passed via stdin.
9+
10+
A formatter **MUST** implement the Stdin Specification if its formatting behavior
11+
can depend on the name of the file being formatted.
12+
13+
## Rules
14+
15+
In order for the formatter to comply with this spec, it **MUST** implement the
16+
vanilla [Formatter Specification](/reference/formatter-spec), and additionally
17+
satisfy the following:
18+
19+
### 1. `--stdin-filepath` flag
20+
21+
The formatter's CLI **MUST** be of the form:
22+
23+
```
24+
<command> [options] [--stdin-filepath <path>]
25+
```
26+
27+
Where:
28+
29+
- `<command>` is the name of the formatting tool.
30+
- `[options]` are any number of flags and options that the formatter accepts.
31+
- `--stdin-filepath <path>` is an optional flag that puts the formatter in
32+
"stdin mode". In stdin mode, the formatter reads file contents from stdin
33+
rather than the filesystem.
34+
- The formatter _MAY_ understand `--stdin-filepath=<path>` as well, but **MUST**
35+
understand the space separated variant.
36+
37+
Example:
38+
39+
```
40+
$ echo "{}" | nixfmt --stdin-filepath path/to/file.nix
41+
```
42+
43+
### 2. Print to stdout, do not assume file is present on filesystem
44+
45+
When in stdin mode, the formatter:
46+
47+
1. **MUST** print the formatted file to stdout.
48+
2. **MUST NOT** attempt to read the file on the filesystem. Instead, it
49+
**MUST** read from stdin.
50+
3. **MUST NOT** write to the given path on the filesytem. It _MAY_ write to
51+
temporary files elsewhere on disk, but _SHOULD_ clean them up when done.

0 commit comments

Comments
 (0)