Commit 42efe40693c0c330e168339ede78ff9eb329832a Parent: 69129dca29a746f494ac2c5e02d11609e552ef7c Author: mcol <mcol@posteo.net> Date: 2021-11-25 22:14:35 +0000 Committer: mcol <mcol@posteo.net> Committed: 2021-11-25 22:14:35 +0000 Add -q quiet option to suppress messages
test/test.sh Modified
@@ -34,7 +34,7 @@ ) rm -rf "$RESULT" -stack run -- -c test/config.dhall +stack run -- -c test/config.dhall -q for test in "${TESTS[@]}" do
src/Templates.hs Modified
@@ -43,6 +43,7 @@ , envOutputDirectory :: Path Abs Dir , envRepoPaths :: [Path Abs Dir] , envHost :: Text + , envQuiet :: Bool , envForce :: Bool } @@ -55,8 +56,8 @@ This creates the runtime environment, collecting the config and loading template data from the template directory. -} -loadEnv :: Bool -> Config -> IO Env -loadEnv force config = do +loadEnv :: Bool -> Bool -> Config -> IO Env +loadEnv quiet force config = do -- First ensure that the output directory exists output <- parseAbsDir <=< canonicalizePath . outputDirectory $ config ensureDir output @@ -87,6 +88,7 @@ , envOutputDirectory = output , envRepoPaths = repoPaths' , envHost = host config + , envQuiet = quiet , envForce = force } where
src/Repositories.hs Modified
@@ -10,7 +10,7 @@ ) where import Conduit (runConduit, sinkList, (.|)) -import Control.Monad (when, (<=<)) +import Control.Monad (unless, when, (<=<)) import Control.Monad.IO.Class (liftIO) import Control.Monad.Trans.Reader (ReaderT) import Data.Either (fromRight) @@ -71,7 +71,7 @@ resolveReference "HEAD" >>= \case Nothing -> do - liftIO . putStrLn $ "gitserve: " <> show name <> ": Failed to resolve HEAD." + liftIO . unless (envQuiet env) . putStrLn $ "gitserve: " <> show name <> ": Failed to resolve HEAD." return repo Just commitID -> do let gitHead = Tagged commitID @@ -90,10 +90,11 @@ liftIO . ensureDir $ output </> fileDir -- Run the generator -- + let quiet = envQuiet env let force = envForce env mapM_ (genRepo output scope) $ envRepoTemplates env - mapM_ (genTarget output scope force $ envCommitTemplate env) commits - mapM_ (genTarget output scope True $ envFileTemplate env) tree -- TODO: detect file changes + mapM_ (genTarget output scope quiet force $ envCommitTemplate env) commits + mapM_ (genTarget output scope quiet True $ envFileTemplate env) tree -- TODO: detect file changes -- Return the repo with the head so the index page can use it. -- return $ repo{repositoryHead = Just . head $ commits} @@ -241,15 +242,16 @@ Path Abs Dir -> HashMap.HashMap Text (GVal RunRepo) -> Bool -> + Bool -> Maybe Template -> a -> ReaderT LgRepo IO () -genTarget _ _ _ Nothing _ = return () -genTarget output scope force (Just template) target = do +genTarget _ _ _ _ Nothing _ = return () +genTarget output scope quiet force (Just template) target = do outFile <- liftIO . dest $ target let output' = output </> outFile exists <- liftIO . doesFileExist $ output' when (force || not exists) $ do - liftIO . putStrLn $ "Writing " <> toFilePath output' + liftIO . unless quiet . putStrLn $ "Writing " <> toFilePath output' let scope' = scope <> HashMap.fromList [(pack . category $ target, toGVal target)] generate output' scope' template
src/Main.hs Modified
@@ -18,9 +18,10 @@ Command line options -} data Options = Options - { config :: FilePath - , force :: Bool - , printVersion :: Bool + { optConfig :: FilePath + , optQuiet :: Bool + , optForce :: Bool + , optVersion :: Bool } opts :: O.Parser Options @@ -31,17 +32,22 @@ <> O.short 'c' <> O.metavar "CONFIG" <> O.value "./config.dhall" - <> O.help "Configuration file to use (Default: ./config.dhall)" + <> O.help "Configuration file to use (Default: ./config.dhall)." + ) + <*> O.switch + ( O.long "quiet" + <> O.short 'q' + <> O.help "Suppress non-error output." ) <*> O.switch ( O.long "force" <> O.short 'f' - <> O.help "Force regeneration of all files" + <> O.help "Force regeneration of all files." ) <*> O.switch ( O.long "version" <> O.short 'v' - <> O.help "Print the gitserve's version" + <> O.help "Print the gitserve's version." ) parser :: IO Options @@ -58,9 +64,9 @@ main :: IO () main = do options <- parser - if printVersion options + if optVersion options then putStrLn $ "Your gitserve version is: " <> showVersion version else do - conf <- getConfig (config options) - env <- loadEnv (force options) conf + conf <- getConfig (optConfig options) + env <- loadEnv (optQuiet options) (optForce options) conf runIndex env =<< run env
src/Index.hs Modified
@@ -4,7 +4,7 @@ runIndex, ) where -import Control.Monad (void) +import Control.Monad (unless, void) import qualified Data.HashMap.Strict as HashMap import Data.Text (Text, unpack) import Path (toFilePath) @@ -30,7 +30,7 @@ runIndexFile :: Env -> [Repo] -> Template -> IO () runIndexFile env repos template = do let output = combine (toFilePath . envOutputDirectory $ env) . toFilePath . templatePath $ template - putStrLn $ "Writing " <> output + unless (envQuiet env) . putStrLn $ "Writing " <> output writeFile output "" -- Clear contents of file if it exists void $ easyRenderM