propellor/src/Propellor/Property/Cmd.hs

46 lines
1.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE PackageImports #-}
2014-03-31 03:37:54 +00:00
module Propellor.Property.Cmd (
2014-03-30 19:31:57 +00:00
cmdProperty,
cmdProperty',
scriptProperty,
2014-04-01 20:58:11 +00:00
userScriptProperty,
2014-03-30 19:31:57 +00:00
) where
import Control.Applicative
2014-03-31 02:14:14 +00:00
import Data.List
import "mtl" Control.Monad.Reader
2014-03-30 19:31:57 +00:00
2014-03-31 03:37:54 +00:00
import Propellor.Types
import Propellor.Property
2014-03-30 19:31:57 +00:00
import Utility.SafeCommand
import Utility.Env
2014-03-31 03:55:59 +00:00
-- | A property that can be satisfied by running a command.
--
-- The command must exit 0 on success.
cmdProperty :: String -> [String] -> Property NoInfo
2014-03-30 19:31:57 +00:00
cmdProperty cmd params = cmdProperty' cmd params []
2014-03-31 03:55:59 +00:00
-- | A property that can be satisfied by running a command,
-- with added environment.
cmdProperty' :: String -> [String] -> [(String, String)] -> Property NoInfo
cmdProperty' cmd params env = property desc $ liftIO $ do
2014-03-30 19:31:57 +00:00
env' <- addEntries env <$> getEnvironment
toResult <$> boolSystemEnv cmd (map Param params) (Just env')
2014-03-30 19:31:57 +00:00
where
desc = unwords $ cmd : params
2014-03-31 02:14:14 +00:00
2014-03-31 03:55:59 +00:00
-- | A property that can be satisfied by running a series of shell commands.
scriptProperty :: [String] -> Property NoInfo
2014-03-31 03:55:59 +00:00
scriptProperty script = cmdProperty "sh" ["-c", shellcmd]
2014-03-31 02:14:14 +00:00
where
2014-03-31 02:23:18 +00:00
shellcmd = intercalate " ; " ("set -e" : script)
2014-04-01 20:58:11 +00:00
-- | A property that can satisfied by running a series of shell commands,
2014-04-03 06:27:17 +00:00
-- as user (cd'd to their home directory).
userScriptProperty :: UserName -> [String] -> Property NoInfo
2014-04-01 20:58:11 +00:00
userScriptProperty user script = cmdProperty "su" ["-c", shellcmd, user]
where
shellcmd = intercalate " ; " ("set -e" : "cd" : script)