Skip to content

Instantly share code, notes, and snippets.

@sinelaw
Created January 13, 2015 11:16
Show Gist options
  • Select an option

  • Save sinelaw/f3d153f6cf9e08314020 to your computer and use it in GitHub Desktop.

Select an option

Save sinelaw/f3d153f6cf9e08314020 to your computer and use it in GitHub Desktop.
Generates a json file describing the directory structure
module Main where
import System.Directory (getDirectoryContents)
import Control.Monad (forM_)
import Data.Functor ((<$>))
import System.Posix.Files (getSymbolicLinkStatus, isDirectory)
withIsLast :: [a] -> [(a, Bool)]
withIsLast [] = []
withIsLast [x] = [(x, True)]
withIsLast (x:xs) = (x, False) : withIsLast xs
printFile :: String -> String -> Bool -> IO ()
printFile top d isLast = do
let path = top ++ "/" ++ d
quotedPath = "\"" ++ d ++ "\""
s <- getSymbolicLinkStatus path
putStr $ quotedPath ++ ": "
if isDirectory s
then do putStrLn "{"
traverseDir path
putStr "}"
else putStr "null"
putStrLn $ if isLast then "" else ","
traverseDir :: FilePath -> IO ()
traverseDir top = do
ds <- filter (\n -> n /= "." && n /= "..") <$> getDirectoryContents top
forM_ (withIsLast ds) $ \(d, isLast) -> printFile top d isLast
main :: IO ()
main = do
putStrLn "{"
traverseDir "."
putStrLn "}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment