Created
May 23, 2019 09:53
-
-
Save madjar/35311de77a70a3a833fca2b252c0e438 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
-- stack --resolver lts-13.15 script | |
{-# LANGUAGE OverloadedStrings, NoImplicitPrelude #-} | |
import Language.Haskell.Interpreter | |
import Options.Applicative.Simple | |
import Protolude hiding (to) | |
import Prelude (String) | |
import Control.Lens hiding (set) | |
import Data.Aeson | |
import Data.Aeson.Lens | |
main = do | |
((file, lensArg, edit), ()) <- | |
simpleOptions | |
"json-edit" | |
"Edit a json file based on lenses" | |
"" | |
((,,) <$> strOption (short 'f' <> metavar "FILE") <*> strArgument (metavar "LENS") <*> switch (short 'e' <> long "--edit")) | |
empty | |
fileContent <- readFile file | |
if edit | |
then return () | |
else do | |
lens <- fmap runTraversal (interpretLens lensArg) | |
liftIO . print $ (fileContent ^.. lens) | |
interpretLens :: String -> IO (ReifiedTraversal' Text Text) | |
interpretLens s = do | |
result <- runInterpreter $ do | |
setImports ["Data.Aeson", "Data.Aeson.Lens", "Control.Lens", "Control.Lens.Internal.Prism", "Data.Text", "Prelude"] | |
interpret ("Traversal "<> (parens s)) infer | |
case result of | |
Right prism -> return prism | |
Left e@(WontCompile errors) -> traverse_ (putStrLn . errMsg) errors >> exitFailure | |
Left e -> print e >> exitFailure | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a (failed) attempt at making a tool that allows you to zoom into part of a json file (or any lens for that matter) with your editor.