Merge pull request #102 from hercules-ci/prebuilt

Prebuilt
This commit is contained in:
Robert Hensing 2020-10-01 19:43:56 +02:00 committed by GitHub
commit b4c17aac7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 19 deletions

View file

@ -1,12 +0,0 @@
#!/usr/bin/env nix-shell
#!nix-shell ./shell.nix
#!nix-shell -i bash
set -eux -o pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
ghcid \
--command 'ghci -isrc/haskell/exe src/haskell/exe/Main.hs' \
--reload=src/haskell \
--restart=arion-compose.cabal \
;

View file

@ -46,6 +46,7 @@ in
buildInputs = [ buildInputs = [
haskellPkgs.cabal-install haskellPkgs.cabal-install
haskellPkgs.ghcid haskellPkgs.ghcid
haskellPkgs.haskell-language-server
super.docker-compose super.docker-compose
self.niv self.niv
# self.releaser # self.releaser

View file

@ -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