diff --git a/README.md b/README.md index 4875d0d4..65469909 100644 --- a/README.md +++ b/README.md @@ -291,9 +291,9 @@ to return back to the unmerged state. ## Usage -* `nixfmt < input.nix` – reads Nix code from `stdin`, formats it, and outputs to `stdout` +* `echo "{a=1;}" | nixfmt --stdin-filepath input.nix` – reads Nix code from `stdin`, formats it, and outputs to `stdout` (the filepath (`input.nix`) is only used for error messages) * `nixfmt file.nix` – format the file in place -## Acknowledgements +## Acknowledgments `nixfmt` was originally developed by [Serokell](https://github.com/serokell) and later donated to become an official Nix project with the acceptance of [RFC 166](https://github.com/NixOS/rfcs/pull/166). diff --git a/main/Main.hs b/main/Main.hs index ae0dd302..5521fa75 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -15,7 +15,6 @@ import Data.ByteString.Char8 (unpack) import Data.Either (lefts) import Data.FileEmbed import Data.List (intersperse, isSuffixOf) -import Data.Maybe (fromMaybe) import Data.Text (Text) import qualified Data.Text.IO as TextIO (getContents, hPutStr, putStr) import Data.Version (showVersion) @@ -57,7 +56,7 @@ data Nixfmt = Nixfmt strict :: Bool, verify :: Bool, ast :: Bool, - filename :: Maybe FilePath, + stdin_filepath :: Maybe FilePath, ir :: Bool } deriving (Show, Data, Typeable) @@ -89,11 +88,11 @@ options = False &= help "Pretty print the internal AST, only for debugging", - filename = + stdin_filepath = Nothing &= help - "The filename to display when the file input is given through stdin.\n\ - \Useful for tools like editors and autoformatters that wish to use Nixfmt without providing it direct file access, while still providing context to where the file is.", + "Format the contents of stdin. When specified, no positional arguments are allowed.\n\ + \The filepath is is only used for error messages.", ir = False &= help @@ -153,8 +152,8 @@ checkTarget format Target{tDoRead, tPath} = do | formatted == contents -> Right () | otherwise -> Left $ tPath ++ ": not formatted" -stdioTarget :: Maybe FilePath -> Target -stdioTarget filename = Target TextIO.getContents (fromMaybe "" filename) (const TextIO.putStr) +stdioTarget :: FilePath -> Target +stdioTarget filepath = Target TextIO.getContents filepath (const TextIO.putStr) fileTarget :: FilePath -> Target fileTarget path = Target (readFileUtf8 path) path atomicWriteFile @@ -169,8 +168,8 @@ checkFileTarget :: FilePath -> Target checkFileTarget path = Target (readFileUtf8 path) path (const $ const $ pure ()) toTargets :: Nixfmt -> IO [Target] -toTargets Nixfmt{files = [], filename} = pure [stdioTarget filename] -toTargets Nixfmt{files = ["-"], filename} = pure [stdioTarget filename] +toTargets Nixfmt{files = [], stdin_filepath = Just filepath} = pure [stdioTarget filepath] +toTargets Nixfmt{files = _ : _, stdin_filepath = Just _} = error "Cannot specify both positional files and --stdin-filepath" toTargets Nixfmt{check = False, files = paths} = map fileTarget <$> collectAllNixFiles paths toTargets Nixfmt{check = True, files = paths} = map checkFileTarget <$> collectAllNixFiles paths diff --git a/test/test.sh b/test/test.sh index 6525947d..91429f16 100755 --- a/test/test.sh +++ b/test/test.sh @@ -1,5 +1,10 @@ #!/usr/bin/env bash +# We invoke `nixmft` in a way that makes shellcheck think we're clobbering the +# file we're formatting as we're reading it. That's not actually happening +# here. +# shellcheck disable=SC2094 + set -euo pipefail # Simple test runner for nixfmt. @@ -35,7 +40,7 @@ verifyCorrect() { shift echo "Checking $file (${*:-no options}) …" - if ! out=$(nixfmt --strict --verify "$@" < "$file"); then + if ! out=$(nixfmt --strict --verify --stdin-filepath "$file" "$@" < "$file"); then echo "[ERROR] failed nixfmt verification" exit 1 fi @@ -60,7 +65,7 @@ verifyCorrect test/correct-indent-4.nix --indent=4 # Verify "invalid" for file in test/invalid/*.nix; do - if nixfmt < "$file" > /dev/null 2>&1; then + if nixfmt --stdin-filepath "test-$file" < "$file" > /dev/null 2>&1; then echo "[ERROR] $file should have failed nixfmt" exit 1 else @@ -71,7 +76,7 @@ done # Verify "diff" for file in test/diff/**/in.nix; do echo "Checking $file …" - out="$(nixfmt --verify < "$file")" + out="$(nixfmt --verify --stdin-filepath "$file" < "$file")" outfile="$(dirname "$file")/out.nix" if diff --color=always --unified "$outfile" <(echo "$out"); then @@ -85,7 +90,7 @@ for file in test/diff/**/in.nix; do fi echo "Checking $file with --strict …" - out="$(nixfmt --strict --verify < "$file")" + out="$(nixfmt --strict --verify --stdin-filepath "$file" < "$file")" outfile="$(dirname "$file")/out-pure.nix" if diff --color=always --unified "$outfile" <(echo "$out"); then