Support use of prebuilt docker-compose.yaml
This commit is contained in:
parent
9dabd9bb92
commit
7c20fa9a11
1 changed files with 35 additions and 7 deletions
|
@ -17,6 +17,8 @@ import Control.Monad.Fail
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Data.Text.IO as T
|
import qualified Data.Text.IO as T
|
||||||
|
|
||||||
|
import Data.Aeson(Value)
|
||||||
|
|
||||||
import System.Posix.User (getRealUserID)
|
import System.Posix.User (getRealUserID)
|
||||||
|
|
||||||
data CommonOptions =
|
data CommonOptions =
|
||||||
|
@ -24,6 +26,7 @@ data CommonOptions =
|
||||||
{ files :: NonEmpty FilePath
|
{ files :: NonEmpty FilePath
|
||||||
, pkgs :: Text
|
, pkgs :: Text
|
||||||
, nixArgs :: [Text]
|
, nixArgs :: [Text]
|
||||||
|
, prebuiltComposeFile :: Maybe FilePath
|
||||||
}
|
}
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
|
@ -56,6 +59,10 @@ parseOptions = do
|
||||||
<> help "Causes Nix to print out a stack trace in case of Nix expression evaluation errors.")
|
<> help "Causes Nix to print out a stack trace in case of Nix expression evaluation errors.")
|
||||||
-- TODO --option support (https://github.com/pcapriotti/optparse-applicative/issues/284)
|
-- TODO --option support (https://github.com/pcapriotti/optparse-applicative/issues/284)
|
||||||
userNixArgs <- many (T.pack <$> strOption (long "nix-arg" <> metavar "ARG" <> help "Pass an extra argument to nix. Example: --nix-arg --option --nix-arg substitute --nix-arg false"))
|
userNixArgs <- many (T.pack <$> strOption (long "nix-arg" <> metavar "ARG" <> help "Pass an extra argument to nix. Example: --nix-arg --option --nix-arg substitute --nix-arg false"))
|
||||||
|
prebuiltComposeFile <- optional $ strOption
|
||||||
|
( long "prebuilt-file"
|
||||||
|
<> metavar "JSONFILE"
|
||||||
|
<> help "Do not evaluate and use the prebuilt JSONFILE instead. Causes other evaluation-related options to be ignored." )
|
||||||
pure $
|
pure $
|
||||||
let nixArgs = userNixArgs <|> "--show-trace" <$ guard showTrace
|
let nixArgs = userNixArgs <|> "--show-trace" <$ guard showTrace
|
||||||
in CommonOptions{..}
|
in CommonOptions{..}
|
||||||
|
@ -135,8 +142,7 @@ runDC cmd (DockerComposeArgs args) _opts = do
|
||||||
|
|
||||||
runBuildAndDC :: Text -> DockerComposeArgs -> CommonOptions -> IO ()
|
runBuildAndDC :: Text -> DockerComposeArgs -> CommonOptions -> IO ()
|
||||||
runBuildAndDC cmd dopts opts = do
|
runBuildAndDC cmd dopts opts = do
|
||||||
ea <- defaultEvaluationArgs opts
|
withBuiltComposeFile opts $ \path -> do
|
||||||
Arion.Nix.withBuiltComposition ea $ \path -> do
|
|
||||||
loadImages path
|
loadImages path
|
||||||
DockerCompose.run DockerCompose.Args
|
DockerCompose.run DockerCompose.Args
|
||||||
{ files = [path]
|
{ files = [path]
|
||||||
|
@ -145,13 +151,36 @@ runBuildAndDC cmd dopts opts = do
|
||||||
|
|
||||||
runEvalAndDC :: Text -> DockerComposeArgs -> CommonOptions -> IO ()
|
runEvalAndDC :: Text -> DockerComposeArgs -> CommonOptions -> IO ()
|
||||||
runEvalAndDC cmd dopts opts = do
|
runEvalAndDC cmd dopts opts = do
|
||||||
ea <- defaultEvaluationArgs opts
|
withComposeFile opts $ \path ->
|
||||||
Arion.Nix.withEvaluatedComposition ea $ \path ->
|
|
||||||
DockerCompose.run DockerCompose.Args
|
DockerCompose.run DockerCompose.Args
|
||||||
{ files = [path]
|
{ files = [path]
|
||||||
, otherArgs = [cmd] ++ unDockerComposeArgs dopts
|
, otherArgs = [cmd] ++ unDockerComposeArgs dopts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
withBuiltComposeFile :: CommonOptions -> (FilePath -> IO r) -> IO r
|
||||||
|
withBuiltComposeFile opts cont = case prebuiltComposeFile opts of
|
||||||
|
Just prebuilt -> do
|
||||||
|
cont prebuilt
|
||||||
|
Nothing -> do
|
||||||
|
args <- defaultEvaluationArgs opts
|
||||||
|
Arion.Nix.withBuiltComposition args cont
|
||||||
|
|
||||||
|
withComposeFile :: CommonOptions -> (FilePath -> IO r) -> IO r
|
||||||
|
withComposeFile opts cont = case prebuiltComposeFile opts of
|
||||||
|
Just prebuilt -> do
|
||||||
|
cont prebuilt
|
||||||
|
Nothing -> do
|
||||||
|
args <- defaultEvaluationArgs opts
|
||||||
|
Arion.Nix.withEvaluatedComposition args cont
|
||||||
|
|
||||||
|
getComposeValue :: CommonOptions -> IO Value
|
||||||
|
getComposeValue opts = case prebuiltComposeFile opts of
|
||||||
|
Just prebuilt -> do
|
||||||
|
decodeFile prebuilt
|
||||||
|
Nothing -> do
|
||||||
|
args <- defaultEvaluationArgs opts
|
||||||
|
Arion.Nix.evaluateComposition args
|
||||||
|
|
||||||
defaultEvaluationArgs :: CommonOptions -> IO EvaluationArgs
|
defaultEvaluationArgs :: CommonOptions -> IO EvaluationArgs
|
||||||
defaultEvaluationArgs co = do
|
defaultEvaluationArgs co = do
|
||||||
uid <- getRealUserID
|
uid <- getRealUserID
|
||||||
|
@ -166,7 +195,7 @@ defaultEvaluationArgs co = do
|
||||||
|
|
||||||
runCat :: CommonOptions -> IO ()
|
runCat :: CommonOptions -> IO ()
|
||||||
runCat co = do
|
runCat co = do
|
||||||
v <- Arion.Nix.evaluateComposition =<< defaultEvaluationArgs co
|
v <- getComposeValue co
|
||||||
T.hPutStrLn stdout (pretty v)
|
T.hPutStrLn stdout (pretty v)
|
||||||
|
|
||||||
runRepl :: CommonOptions -> IO ()
|
runRepl :: CommonOptions -> IO ()
|
||||||
|
@ -229,8 +258,7 @@ runExec :: Bool -> Bool -> Maybe Text -> Bool -> Int -> [(Text, Text)] -> Maybe
|
||||||
runExec detach privileged user noTTY index envs workDir service commandAndArgs opts = do
|
runExec detach privileged user noTTY index envs workDir service commandAndArgs opts = do
|
||||||
putErrText $ "Service: " <> service
|
putErrText $ "Service: " <> service
|
||||||
|
|
||||||
ea <- defaultEvaluationArgs opts
|
withComposeFile opts $ \path -> do
|
||||||
Arion.Nix.withEvaluatedComposition ea $ \path -> do
|
|
||||||
commandAndArgs'' <- case commandAndArgs of
|
commandAndArgs'' <- case commandAndArgs of
|
||||||
[] -> getDefaultExec path service
|
[] -> getDefaultExec path service
|
||||||
x -> pure x
|
x -> pure x
|
||||||
|
|
Loading…
Reference in a new issue