Support --no-ansi, --compatibility, --log-level options

This commit is contained in:
Robert Hensing 2020-10-11 12:47:28 +02:00
parent b959ab492d
commit 700297748d

View file

@ -28,6 +28,9 @@ data CommonOptions =
, pkgs :: Text , pkgs :: Text
, nixArgs :: [Text] , nixArgs :: [Text]
, prebuiltComposeFile :: Maybe FilePath , prebuiltComposeFile :: Maybe FilePath
, noAnsi :: Bool
, compatibility :: Bool
, logLevel :: Maybe Text
} }
deriving (Show) deriving (Show)
@ -64,6 +67,11 @@ parseOptions = do
( long "prebuilt-file" ( long "prebuilt-file"
<> metavar "JSONFILE" <> metavar "JSONFILE"
<> help "Do not evaluate and use the prebuilt JSONFILE instead. Causes other evaluation-related options to be ignored." ) <> help "Do not evaluate and use the prebuilt JSONFILE instead. Causes other evaluation-related options to be ignored." )
noAnsi <- flag False True (long "no-ansi"
<> help "Avoid ANSI control sequences")
compatibility <- flag False True (long "no-ansi"
<> help "If set, Docker Compose will attempt to convert deploy keys in v3 files to their non-Swarm equivalent")
logLevel <- optional $ fmap T.pack $ strOption (long "log-level" <> metavar "LEVEL" <> help "Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)")
pure $ pure $
let nixArgs = userNixArgs <|> "--show-trace" <$ guard showTrace let nixArgs = userNixArgs <|> "--show-trace" <$ guard showTrace
in CommonOptions{..} in CommonOptions{..}
@ -143,25 +151,39 @@ 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
withBuiltComposeFile opts $ callDC cmd dopts True withBuiltComposeFile opts $ callDC cmd dopts opts True
runEvalAndDC :: Text -> DockerComposeArgs -> CommonOptions -> IO () runEvalAndDC :: Text -> DockerComposeArgs -> CommonOptions -> IO ()
runEvalAndDC cmd dopts opts = do runEvalAndDC cmd dopts opts = do
withComposeFile opts $ callDC cmd dopts False withComposeFile opts $ callDC cmd dopts opts False
callDC :: Text -> DockerComposeArgs -> Bool -> FilePath -> IO () callDC :: Text -> DockerComposeArgs -> CommonOptions -> Bool -> FilePath -> IO ()
callDC cmd dopts shouldLoadImages path = do callDC cmd dopts opts shouldLoadImages path = do
extendedInfo <- loadExtendedInfoFromPath path extendedInfo <- loadExtendedInfoFromPath path
when shouldLoadImages $ loadImages (images extendedInfo) when shouldLoadImages $ loadImages (images extendedInfo)
let firstOpts = let firstOpts = projectArgs extendedInfo <> commonArgs opts
do
n <- toList (projectName extendedInfo)
["--project-name", n]
DockerCompose.run DockerCompose.Args DockerCompose.run DockerCompose.Args
{ files = [path] { files = [path]
, otherArgs = firstOpts ++ [cmd] ++ unDockerComposeArgs dopts , otherArgs = firstOpts ++ [cmd] ++ unDockerComposeArgs dopts
} }
projectArgs :: ExtendedInfo -> [Text]
projectArgs extendedInfo =
do
n <- toList (projectName extendedInfo)
["--project-name", n]
commonArgs :: CommonOptions -> [Text]
commonArgs opts = do
guard (noAnsi opts)
["--no-ansi"]
<> do
guard (compatibility opts)
["--compatibility"]
<> do
l <- toList (logLevel opts)
["--log-level", l]
withBuiltComposeFile :: CommonOptions -> (FilePath -> IO r) -> IO r withBuiltComposeFile :: CommonOptions -> (FilePath -> IO r) -> IO r
withBuiltComposeFile opts cont = case prebuiltComposeFile opts of withBuiltComposeFile opts cont = case prebuiltComposeFile opts of
Just prebuilt -> do Just prebuilt -> do
@ -260,12 +282,18 @@ orEmpty' :: (Alternative f, Monoid a) => f a -> f a
orEmpty' m = fromMaybe mempty <$> optional m orEmpty' m = fromMaybe mempty <$> optional m
runExec :: Bool -> Bool -> Maybe Text -> Bool -> Int -> [(Text, Text)] -> Maybe Text -> Text -> [Text] -> CommonOptions -> IO () runExec :: Bool -> Bool -> Maybe Text -> Bool -> Int -> [(Text, Text)] -> Maybe Text -> Text -> [Text] -> CommonOptions -> IO ()
runExec detach privileged user noTTY index envs workDir service commandAndArgs opts = do runExec detach privileged user noTTY index envs workDir service commandAndArgs opts =
putErrText $ "Service: " <> service
withComposeFile opts $ \path -> do withComposeFile opts $ \path -> do
extendedInfo <- loadExtendedInfoFromPath path
commandAndArgs'' <- case commandAndArgs of commandAndArgs'' <- case commandAndArgs of
[] -> getDefaultExec path service [] -> do
cmd <- getDefaultExec path service
case cmd of
[] -> do
putErrText "You must provide a command via service.defaultExec or on the command line."
exitFailure
_ ->
pure cmd
x -> pure x x -> pure x
let commandAndArgs' = case commandAndArgs'' of let commandAndArgs' = case commandAndArgs'' of
[] -> ["/bin/sh"] [] -> ["/bin/sh"]
@ -285,7 +313,7 @@ runExec detach privileged user noTTY index envs workDir service commandAndArgs o
] ]
DockerCompose.run DockerCompose.Args DockerCompose.run DockerCompose.Args
{ files = [path] { files = [path]
, otherArgs = args , otherArgs = projectArgs extendedInfo <> commonArgs opts <> args
} }
main :: IO () main :: IO ()