Search code examples
haskellhaskell-stackhaskell-platform

Basic question on executing multiple commands in Haskell


I have the following on my code:

  where
    launch :: MonadIO m => m (Maybe Text)
    launch = do
        line <- launchLine
        return $ lineToText <$> line

    launchLine :: MonadIO m => m (Maybe Line)
    launchLine = fold (inproc "juke" ["launch", "--profile", "jukeplay", pack baseImage] mempty) $
        Fold selectLaunchName Nothing id

The lines above work fine. My problem (and question) is that I want to execute another command before this launch line, so it could be something like:

  where
    launch :: MonadIO m => m (Maybe Text)
    launch = do
        line <- launchLine
        return $ lineToText <$> line

    launchLine :: MonadIO m => m (Maybe Line)
    launchLine = fold (inproc "juke" ["image", "copy", "jukebox:" <> pack baseImage, "local:", "--copy-aliases"] mempty) $
        Fold selectLaunchName Nothing id

    launchLine :: MonadIO m => m (Maybe Line)
    launchLine = fold (inproc "juke" ["launch", "--profile", "jukeplay", pack baseImage] mempty) $
        Fold selectLaunchName Nothing id

This obviously does not work. How could I accomplish this?

I need to have this "juke image copy" done before the "juke launch.." is executed

Thanks in advance for the help


Solution

  • You can combine monadic actions with (>>):

    (>>) :: Monad m => m a -> m b -> m b
    

    Or with do syntax, which desugars to (>>):

    \act1 act2 -> do {act1; act2} :: Monad m => m a -> m b -> m b
    

    For example:

    launch :: MonadIO m => m (Maybe Text)
    launch = do
        preLaunchLine
        line <- launchLine
        return $ lineToText <$> line
    
    preLaunchLine :: MonadIO m => m (Maybe Line)
    preLaunchLine = fold (inproc "juke" ["image", "copy", "jukebox:" <> pack baseImage, "local:", "--copy-aliases"] mempty) $
        Fold selectLaunchName Nothing id
    
    launchLine :: MonadIO m => m (Maybe Line)
    launchLine = fold (inproc "juke" ["launch", "--profile", "jukeplay", pack baseImage] mempty) $
        Fold selectLaunchName Nothing id