From fcf270c80c2a7e47a020f67ab060a0e1bacd2b7c Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 27 Sep 2019 21:45:06 +0200 Subject: [PATCH] Make arion repl work --- src/haskell/exe/Main.hs | 20 +++++++++++++++----- src/haskell/lib/Arion/DockerCompose.hs | 1 - src/haskell/lib/Arion/Nix.hs | 23 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/haskell/exe/Main.hs b/src/haskell/exe/Main.hs index b5910d0..325c546 100644 --- a/src/haskell/exe/Main.hs +++ b/src/haskell/exe/Main.hs @@ -70,9 +70,9 @@ textArgument = fmap T.pack . strArgument parseCommand :: Parser (CommonOptions -> IO ()) parseCommand = hsubparser - ( command "cat" (info (pure runCat) (progDesc "TODO: cat doc" <> fullDesc)) - <> command "repl" (info (pure runRepl) (progDesc "TODO: repl doc" <> fullDesc)) - <> command "exec" (info (pure runExec) (progDesc "TODO: exec doc" <> fullDesc)) + ( command "cat" (info (pure runCat) (progDesc "Spit out the docker compose file as JSON" <> fullDesc)) + <> command "repl" (info (pure runRepl) (progDesc "Start a nix repl for the whole composition" <> fullDesc)) + -- <> command "exec" (info (pure runExec) (progDesc "TODO: exec doc" <> fullDesc)) ) <|> hsubparser @@ -172,8 +172,18 @@ runCat co = do T.hPutStrLn stdout (pretty v) runRepl :: CommonOptions -> IO () -runRepl opts = - T.putStrLn "Running repl ... TODO" +runRepl co = do + putErrText + "Launching a repl for you. To get started:\n\ + \\n\ + \To see deployment-wide configuration\n\ + \ type config. and hit TAB\n\ + \To see the services\n\ + \ type config.docker-compose.evaluatedServices TAB or ENTER\n\ + \To bring the top-level Nixpkgs attributes into scope\n\ + \ type :a (config._module.args.pkgs) // { inherit config; }\n\ + \" + Arion.Nix.replForComposition =<< defaultEvaluationArgs co runExec :: CommonOptions -> IO () runExec opts = diff --git a/src/haskell/lib/Arion/DockerCompose.hs b/src/haskell/lib/Arion/DockerCompose.hs index 804f95a..898eb9f 100644 --- a/src/haskell/lib/Arion/DockerCompose.hs +++ b/src/haskell/lib/Arion/DockerCompose.hs @@ -40,7 +40,6 @@ run args = do withCreateProcess procSpec $ \_in _out _err procHandle -> do - -- Wait for process exit and 'err' printout exitCode <- waitForProcess procHandle case exitCode of diff --git a/src/haskell/lib/Arion/Nix.hs b/src/haskell/lib/Arion/Nix.hs index dd06692..6eb40d9 100644 --- a/src/haskell/lib/Arion/Nix.hs +++ b/src/haskell/lib/Arion/Nix.hs @@ -2,7 +2,9 @@ module Arion.Nix ( evaluateComposition , withEvaluatedComposition + , buildComposition , withBuiltComposition + , replForComposition , EvaluationArgs(..) , EvaluationMode(..) ) where @@ -137,6 +139,27 @@ withBuiltComposition ea f = do buildComposition path ea f path + +replForComposition :: EvaluationArgs -> IO () +replForComposition ea = do + evalComposition <- getEvalCompositionFile + let args = + [ "repl", evalComposition ] + ++ argArgs ea + ++ map toS (evalUserArgs ea) + procSpec = (proc "nix" args) { cwd = evalWorkDir ea } + + withCreateProcess procSpec $ \_in _out _err procHandle -> do + + exitCode <- waitForProcess procHandle + + case exitCode of + ExitSuccess -> pass + ExitFailure 1 -> exitFailure + e@ExitFailure {} -> do + throwIO $ FatalError $ "nix repl failed with " <> show exitCode + exitWith e + argArgs :: EvaluationArgs -> [[Char]] argArgs ea = [ "--argstr"