Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in-place operations to CLI tool. #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion aeson-pretty.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ description:
> cabal install -flib-only aeson-pretty
.
the command-line tool will NOT be installed.
.
For those wishing to prettify JSON in their source repos, you can now pass
multiple paths using @find@ or @xargs@ and they will be processed in place.
No backups are made, so this is best used with a VCS.
.

extra-source-files:
README.markdown
Expand Down Expand Up @@ -69,7 +74,9 @@ executable aeson-pretty
attoparsec >= 0.10,
base == 4.*,
bytestring >= 0.9,
cmdargs >= 0.7
cmdargs >= 0.7,
directory,
filepath

ghc-options: -Wall
ghc-prof-options: -auto-all
Expand Down
38 changes: 34 additions & 4 deletions cli-tool/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,31 @@
module Main (main) where

import Prelude hiding (interact, concat, unlines, null)
import Control.Exception (bracketOnError)
import Control.Monad (forM_)
import Data.Aeson (Value(..), json', encode)
import Data.Aeson.Encode.Pretty
import Data.Attoparsec.Lazy (Result(..), parse)
import Data.ByteString.Lazy.Char8 (ByteString, interact, unlines, null)
import Data.ByteString.Lazy.Char8 (ByteString, interact, unlines, null, hPut, hGetContents)
import Data.Version (showVersion)
import Paths_aeson_pretty (version)
import System.Console.CmdArgs
import System.Directory (removeFile, renameFile)
import System.FilePath (splitFileName, replaceExtension)
import System.IO (IOMode(ReadMode), hClose, openBinaryTempFileWithDefaultPermissions, withFile)


data Options = Opts { compact :: Bool
data Options = Opts { source :: [FilePath]
, compact :: Bool
, indent :: Int
, sort :: Bool
}
deriving (Data, Typeable)

opts :: Options
opts = Opts
{ compact = False &= help "Compact output."
{ source = def &= args
, compact = False &= help "Compact output."
, indent = 4 &= help "Number of spaces per nesting-level (default 4)."
, sort = False &= help "Sort objects by key (default: undefined order)."
} &= program prog
Expand All @@ -34,6 +41,8 @@ info =
[ "Read JSON from stdin and pretty-print to stdout. The complementary "
, "compact-mode removes whitespace from the input."
, ""
, "If one or more files are specified, the files will be prettified in place. No backups are made."
, ""
, "(c) Falko Peters 2011"
, ""
, "License: BSD3, for details see the source-repository at"
Expand All @@ -50,11 +59,32 @@ main = do
, confTrailingNewline = False
}
enc = if compact then encode else encodePretty' conf
interact $ unlines . map enc . values
case source of
[] -> interact $ unlines . map enc . values
_ -> forM_ source (\path -> inplace path $ unlines . map enc . values)

values :: ByteString -> [Value]
values s = case parse json' s of
Done rest v -> v : values rest
Fail rest _ _
| null rest -> []
| otherwise -> error "invalid json"

inplace :: FilePath -> (ByteString -> ByteString) -> IO ()
inplace path interaction = do
tempPath <- bracketOnError acquire failure execute
renameFile tempPath path
where
(tempDir, tempName) = splitFileName path
tempPattern = replaceExtension tempName ".XXXXX"
acquire = openBinaryTempFileWithDefaultPermissions tempDir tempPattern
execute (tempPath, tempHandle) = do
withFile path ReadMode $ \readHandle -> do
contents <- hGetContents readHandle
hPut tempHandle $ interaction contents
hClose tempHandle
return tempPath
failure (tempPath, tempHandle) = do
hClose tempHandle
removeFile tempPath