module Main (main) where import Converge import Control.Monad (unless) import Options.Applicative import System.Exit (ExitCode (..)) import System.IO (hPutStrLn, stderr) data CliOptions = CliOptions { cliRepoPath :: !FilePath , cliDebounce :: !Int , cliRemote :: !Text , cliBranch :: !Text } cliParser :: Parser CliOptions cliParser = CliOptions <$> strOption ( long "repo" <> short 'r' <> metavar "PATH" <> value "." <> showDefault <> help "Path to the git repository to watch (default: cwd)" ) <*> option auto ( long "debounce" <> short 'd' <> metavar "MS" <> value 5000 <> showDefault <> help "Debounce window in milliseconds" ) <*> strOption ( long "remote" <> metavar "NAME" <> value "origin" <> showDefault <> help "Remote name to pull from" ) <*> strOption ( long "branch" <> short 'b' <> metavar "NAME" <> value "main" <> showDefault <> help "Branch to pull" ) main :: IO () main = do cli <- execParser opts let options = defaultOptions { optRepoPath = cliRepoPath cli , optDebounceMs = cliDebounce cli , optRemote = cliRemote cli , optBranch = cliBranch cli } hPutStrLn stderr $ "Converge watching " <> optRepoPath options <> " (debounce: " <> show (optDebounceMs options) <> "ms)" withWatcher options $ \_event -> do (commitCode, _, commitErr) <- gitCommitAll (optRepoPath options) unless (commitCode == ExitSuccess) $ hPutStrLn stderr ("git commit failed: " <> show commitErr) (pullCode, _, pullErr) <- gitPull options unless (pullCode == ExitSuccess) $ hPutStrLn stderr ("git pull failed: " <> show pullErr) conflicted <- hasConflicts (optRepoPath options) when conflicted $ notifyConflict options where opts = info (cliParser <**> helper) ( fullDesc <> progDesc "Auto-sync a git repo by watching, committing, and pulling" <> header "converge - keep a git repo in sync" )