packages feed

spy-0.14: src/Main.hs

module Main where

import           Spy.Watcher
import           System.Console.CmdArgs
import           System.Directory       (canonicalizePath)
import           System.Environment     (getArgs, withArgs)
import           System.FilePath        (addTrailingPathSeparator)

version :: String
version = "spy v0.14, (C) Stefan Saasen"

watch :: Spy
watch = Watch
    {hidden     = False             &= name "i" &= name "hidden" &= typ  "BOOL" &= help "Set to true if hidden files/directories should be included"
    ,dir        = "."               &= argPos 0  &= typ "FILE/DIR"
    ,format     = Just plainFormat  &= name "f"  &= help "Specify the output format ('json', 'plain', 'color')"
    ,glob       = Nothing           &= args      &= typ "GLOB"
    }
    &= help "Watch a directory (or file) for file changes"
    &= auto

run :: Spy
run = Run
    {command    = def           &= argPos 0     &= typ "CMD"
    ,dir        = "."           &= argPos 1     &= typ "FILE/DIR"
    ,glob       = Nothing       &= args         &= typ "GLOB"
    ,notifyOnly = False         &= name "n"     &= name "notify-only"                    &= typ "BOOL" &= help "Don't pass the path of the modified file to the command"
    ,hidden     = False         &= explicit     &= name "i"             &= name "hidden" &= typ "BOOL" &= help "Set to true if hidden files/directories should be included"
    }
    &= help "Run a command whenever a file changes"


mode :: Mode (CmdArgs Spy)
mode = cmdArgsMode $ modes [watch,run]
        &= help "spy watches for file changes on the file system"
        &= program "spy" &= summary version


main :: IO ()
main = do
    options <- getArgs
    config <- (if null options then withArgs ["--help"] else id) $ cmdArgsRun mode
    canonicalizedDir <- canonicalizePath $ dir config
    putStrLn "Press Ctrl-D to stop"
    spy (config { dir = addTrailingPathSeparator canonicalizedDir }) $
        putStrLn "No eyes on the target"