Skip to content

Commit 5671029

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

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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** satisfy the following:
16+
17+
### 1. `--stdin` flag
18+
19+
The formatter's CLI must be of the form:
20+
21+
```
22+
<command> [options] [--stdin] [...<files>]
23+
```
24+
25+
Where:
26+
27+
- `<command>` is the name of the formatting tool.
28+
- `[options]` are any number of flags and options that the formatter accepts.
29+
- `--stdin` is an optional flag that puts the formatter in "stdin mode". In
30+
stdin mode, the formatter reads file contents from stdin rather than the
31+
filesystem.
32+
- `[...<files>]` are one or more files given to the formatter for processing. If
33+
`--stdin` is specified, then exactly 1 must be present, and is treated as a
34+
filename.
35+
36+
Example:
37+
38+
```
39+
$ echo "{}" | nixfmt --stdin path/to/file.nix
40+
```
41+
42+
!!! note
43+
44+
This CLI **MUST** be implemented in a way that is compatibile with the
45+
vanilla [Formatter Specification](#1-files-passed-as-arguments).
46+
47+
### 2. Print to stdout, do not assume file is present on filesystem
48+
49+
When in stdin mode, the formatter:
50+
51+
1. **MUST** print the formatted file to stdout.
52+
2. **MUST NOT** attempt to read the file on the filesystem. Instead, it
53+
**MUST** read from stdin.
54+
3. **MUST NOT** write to the given path on the filesytem. It _MAY_ write to
55+
temporary files elsewhere on disk, but _SHOULD_ clean them up when done.
56+
57+
### 3. Exit nonzero on error
58+
59+
When something goes wrong when formatting (for example, the source code is
60+
syntactically invalid), the formatter **MUST** exit nonzero.
61+
62+
The formatter _SHOULD_ print useful information about what went wrong to
63+
stderr.

0 commit comments

Comments
 (0)