module Gitson.Util (module Gitson.Util) where
import Control.Monad (void, filterM)
import Data.List (isSuffixOf, isPrefixOf)
import System.FilePath
import System.Directory
import System.Process
import System.IO
entryPath :: FilePath -> FilePath -> FilePath
entryPath collection key = collection </> key <.> "json"
lockPath :: FilePath
lockPath = ".git" </> "gitson-lock"
filterFilenamesAsKeys :: [String] -> [String]
filterFilenamesAsKeys = map dropExtension . filter (isSuffixOf ".json")
filterDirs :: [FilePath] -> IO [FilePath]
filterDirs = (filterM doesDirectoryExist) . filter (not . isPrefixOf ".")
insideDirectory :: FilePath -> IO a -> IO a
insideDirectory path action = do
prevPath <- getCurrentDirectory
setCurrentDirectory path
result <- action
setCurrentDirectory prevPath
return result
lastCommitText :: IO String
lastCommitText = readProcess "git" ["log", "--max-count=1", "--pretty=format:%s"] []
devNull :: IO Handle
devNull = openFile "/dev/null" ReadWriteMode
shell :: String -> [String] -> IO ()
shell cmd args = void $ do
dnull <- devNull
(_, _, _, pid) <- createProcess (proc cmd args){std_in = UseHandle dnull, std_out = UseHandle dnull, std_err = UseHandle dnull}
waitForProcess pid