#! /usr/bin/env runhaskell {-# LANGUAGE OverloadedStrings , LambdaCase , RecordWildCards #-} import Argotest import Turtle import Data.Default import Control.Concurrent.Async import System.Console.ANSI import System.Console.ANSI.Types (Color) import Options.Applicative import System.Posix.Signals import Control.Monad opts :: Parser (IO ()) opts = subparser ( command "clean" (info (pure $ runClean def) idm)<> help "Target for the greeting" <> command "daemon" (info (pure $ runDaemon def) idm) <> command "helloworld" (info (pure $ runHelloWorld def ) idm) ) main :: IO () main = join $ execParser (info (opts <**>helper) idm) runHelloWorld :: StackArgs -> IO () runHelloWorld a@StackArgs{..} = do let passStr = "HelloWorldFromApp" runSimpleStack $ a {app = format ("echo "%s) passStr} readTextFile cmd_err >>= \x -> case match (has "HelloWorldFromApp") x of [] -> die ("Hello world app failed to run.") _ -> sh $ printInfo "The hello world app executed properly in a \ \container and its message was received by `cmd`.\n" runSimpleStack :: StackArgs -> IO () runSimpleStack a@StackArgs{..} = sh $ do cleanLeftovers daemon_out daemon_err cmd_out cmd_err time_file nrm_log daemonShell <- prepareDaemonShell dargs daemon_out daemon_err nrm_log daemonAsync <- fork $ sh $ daemonShell let handler = do sh $ printInfo "Interrupted. Killing daemon..." cancel daemonAsync sh $ colorShell Green $ printf "Killed daemon.\n" liftIO $ installHandler keyboardSignal (Catch handler) Nothing printInfo "Launching the application through cmd.\n" (_,t) <- time $ shell (format ("cmd run -u toto "%fp%" "%s%" > "%fp%" 2>"%fp) manifest app cmd_out cmd_err) empty >>= \case ExitSuccess -> printInfo "cmd has exited successfuly.\n" ExitFailure n -> die ("cmd failed with exit code " <> repr n <> " . The application logs are at " <> repr cmd_out <> " " <> repr cmd_err ) liftIO $ writeTextFile time_file (repr t) printInfo "Killing the daemon.\n" liftIO $ cancel daemonAsync printInfo "Daemon killed.\n" runClean :: StackArgs -> IO () runClean StackArgs{..} = sh $ cleanLeftovers daemon_out daemon_err cmd_out cmd_err time_file nrm_log runDaemon :: StackArgs -> IO () runDaemon StackArgs{..} = sh $ prepareDaemonShell dargs daemon_out daemon_err nrm_log >>= id