{-# LANGUAGE OverloadedStrings #-}
module Arion.DockerCompose where

import           Prelude                        ( )
import           Protolude
import           System.Process

data Args = Args
  { Args -> [FilePath]
files :: [FilePath]
  , Args -> [Text]
otherArgs :: [Text]
  }

run :: Args -> IO ()
run :: Args -> IO ()
run Args
args = do
  let fileArgs :: [FilePath]
fileArgs = Args -> [FilePath]
files Args
args [FilePath] -> (FilePath -> [FilePath]) -> [FilePath]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \FilePath
f -> [FilePath
"--file", FilePath
f]
      allArgs :: [FilePath]
allArgs  = [FilePath]
fileArgs [FilePath] -> [FilePath] -> [FilePath]
forall a. [a] -> [a] -> [a]
++ (Text -> FilePath) -> [Text] -> [FilePath]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map Text -> FilePath
forall a b. ConvertText a b => a -> b
toS (Args -> [Text]
otherArgs Args
args)

      procSpec :: CreateProcess
procSpec = FilePath -> [FilePath] -> CreateProcess
proc FilePath
"docker-compose" [FilePath]
allArgs

  -- hPutStrLn stderr ("Running docker-compose with " <> show allArgs :: Text)

  CreateProcess
-> (Maybe Handle
    -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO ())
-> IO ()
forall a.
CreateProcess
-> (Maybe Handle
    -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a)
-> IO a
withCreateProcess CreateProcess
procSpec ((Maybe Handle
  -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO ())
 -> IO ())
-> (Maybe Handle
    -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO ())
-> IO ()
forall a b. (a -> b) -> a -> b
$ \Maybe Handle
_in Maybe Handle
_out Maybe Handle
_err ProcessHandle
procHandle -> do

    ExitCode
exitCode <- ProcessHandle -> IO ExitCode
waitForProcess ProcessHandle
procHandle

    case ExitCode
exitCode of
      ExitCode
ExitSuccess -> IO ()
forall (f :: * -> *). Applicative f => f ()
pass
      ExitFailure Int
1 -> IO ()
forall a. IO a
exitFailure
      ExitFailure {} -> do
        FatalError -> IO ()
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO (FatalError -> IO ()) -> FatalError -> IO ()
forall a b. (a -> b) -> a -> b
$ Text -> FatalError
FatalError (Text -> FatalError) -> Text -> FatalError
forall a b. (a -> b) -> a -> b
$ Text
"docker-compose failed with " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ExitCode -> Text
forall a b. (Show a, ConvertText FilePath b) => a -> b
show ExitCode
exitCode