|
| 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