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

import           Prelude                        ( )
import           Protolude
import           System.Process

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

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

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

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

  forall a.
CreateProcess
-> (Maybe Handle
    -> Maybe Handle -> Maybe Handle -> ProcessHandle -> IO a)
-> IO a
withCreateProcess CreateProcess
procSpec 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 -> forall (f :: * -> *). Applicative f => f ()
pass
      ExitFailure Int
1 -> forall a. IO a
exitFailure
      ExitFailure {} -> do
        forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO forall a b. (a -> b) -> a -> b
$ Text -> FatalError
FatalError forall a b. (a -> b) -> a -> b
$ Text
"docker-compose failed with " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, StringConv String b) => a -> b
show ExitCode
exitCode