simple-cmd 0.1.1 → 0.1.2
raw patch · 5 files changed
+87/−40 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ SimpleCmd: grep :: String -> FilePath -> IO [String]
Files
- ChangeLog.md +4/−0
- SimpleCmd.hs +68/−29
- SimpleCmd/Git.hs +11/−8
- SimpleCmd/Rpm.hs +3/−2
- simple-cmd.cabal +1/−1
ChangeLog.md view
@@ -1,5 +1,9 @@ # Revision history for simple-cmd +## 0.1.2 -- 2018-10-28+- grep+- improve haddock documentation+ ## 0.1.1 -- 2018-10-02 - cmdLines - Git: grepGitConfig
SimpleCmd.hs view
@@ -1,3 +1,31 @@+{-|+Some simple String wrappers of `readProcess`, `readProcessWithExitCode`,+`rawSystem` from the Haskell `process` library.++Simplest is++@cmd_ :: String -> [String] -> IO ()@++which outputs to stdout. For example:++@cmd_ "git" ["clone", url]@++Then++@cmd :: String -> [String] -> IO String@++returns stdout as a @String@.++There are also @cmdBool@, @cmdMaybe@, @cmdList@, @shell@, and others.++Other examples:++@grep_ pat file :: IO Bool@++@sudo c args :: IO ()@++-}+ module SimpleCmd ( cmd, cmd_, cmdBool,@@ -10,7 +38,7 @@ cmdSilent, cmdStdIn, cmdStdErr,- egrep_, grep_,+ egrep_, grep, grep_, logMsg, removePrefix, removeStrictPrefix, removeSuffix, shell, shell_,@@ -38,11 +66,13 @@ quoteCmd :: String -> [String] -> String quoteCmd c args = "'" ++ unwords (c:args) ++ "'" --- | Run a command in a process and return stdout-cmd :: String -> [String] -> IO String+-- | 'cmd c args' runs a command in a process and returns stdout+cmd :: String -- ^ command to run+ -> [String] -- ^ list of arguments+ -> IO String -- ^ stdout cmd c args = cmdStdIn c args "" --- | Run command in process, output goes to stdout and stderr+-- | 'cmd_ c args' runs command in a process, output goes to stdout and stderr cmd_ :: String -> [String] -> IO () cmd_ c args = do ret <- rawSystem c args@@ -50,7 +80,7 @@ ExitSuccess -> return () ExitFailure n -> error $ quoteCmd c args +-+ "failed with exit code" +-+ show n --- | Run a command, and return Boolean status+-- | 'cmdBool c args' runs a command, and return Boolean status cmdBool :: String -> [String] -> IO Bool cmdBool c args = do ret <- rawSystem c args@@ -58,7 +88,7 @@ ExitSuccess -> return True ExitFailure _ -> return False --- | Run a command in a process, maybe returning output if it succeeds+-- | 'cmdMaybe c args' runs a command, maybe returning output if it succeeds cmdMaybe :: String -> [String] -> IO (Maybe String) cmdMaybe c args = do (ret, out, _err) <- readProcessWithExitCode c args ""@@ -66,45 +96,47 @@ ExitSuccess -> return $ Just $ removeTrailingNewline out ExitFailure _ -> return Nothing --- | Run command, return list of stdout lines--- | since 0.1.1+-- | 'cmdLines c args' runs a command, and returns list of stdout lines+--+-- @since 0.1.1 cmdLines :: String -> [String] -> IO [String] cmdLines c args = lines <$> cmd c args --- | Run a command, passing input string as stdin, and return stdout+-- | 'cmdStdIn c args inp' runs a command, passing input string as stdin, and returns stdout cmdStdIn :: String -> [String] -> String -> IO String cmdStdIn c args inp = removeTrailingNewline <$> readProcess c args inp --- | Run a command string in a shell, and return stdout+-- | 'shell cs' runs a command string in a shell, and returns stdout shell :: String -> IO String shell cs = cmd "sh" ["-c", cs] --- | Run a command string in a shell, output goes to stdout+-- | 'shell_ c' runs a command string in a shell, output goes to stdout shell_ :: String -> IO () shell_ c = cmd_ "sh" ["-c", c] --- | Log a command with a datestamp+-- | 'cmdLog c args' logs a command with a datestamp cmdlog :: String -> [String] -> IO () cmdlog c args = do logMsg $ unwords $ c:args cmd_ c args +-- | 'logMsg msg' outputs message with a timestamp logMsg :: String -> IO () logMsg msg = do date <- cmd "date" ["+%T"] putStrLn $ date +-+ msg --- | Dry-run a command: print it to stdout - more used for debugging+-- | 'cmdN c args' dry-runs a command: prints command to stdout - more used for debugging cmdN :: String -> [String] -> IO () cmdN c args = putStrLn $ unwords $ c:args --- | Run command in a process, returning stdout and stderr+-- | 'cmdStdErr c args' runs command in a process, returning stdout and stderr cmdStdErr :: String -> [String] -> IO (String, String) cmdStdErr c args = do (_ret, out, err) <- readProcessWithExitCode c args "" return (removeTrailingNewline out, removeTrailingNewline err) --- -- | Run command, if it fails output msg as error.+-- -- | 'cmdAssert msg c args' runs command, if it fails output msg as error. -- cmdAssert :: String -> String -> [String] -> IO () -- cmdAssert msg c args = do -- ret <- rawSystem c args@@ -112,7 +144,7 @@ -- ExitSuccess -> return () -- ExitFailure _ -> error msg --- | Run a command hiding stderr, if it succeeds return stdout+-- | 'cmdQuiet c args' runs a command hiding stderr, if it succeeds returns stdout cmdQuiet :: String -> [String] -> IO String cmdQuiet c args = do (ret, out, err) <- readProcessWithExitCode c args ""@@ -120,7 +152,7 @@ ExitSuccess -> return $removeTrailingNewline out ExitFailure n -> error $ quoteCmd c args +-+ "failed with status" +-+ show n ++ "\n" ++ err --- | Run a command hiding stdout: stderr is only output if it fails.+-- | 'cmdSilent c args' runs a command hiding stdout: stderr is only output if it fails. cmdSilent :: String -> [String] -> IO () cmdSilent c args = do (ret, _, err) <- readProcessWithExitCode c args ""@@ -128,28 +160,35 @@ ExitSuccess -> return () ExitFailure n -> error $ quoteCmd c args +-+ "failed with status" +-+ show n ++ "\n" ++ err --- | Run a command, drop stderr, and return stdout+-- | 'cmdIgnoreErr c args inp' runs a command with input, drops stderr, and return stdout cmdIgnoreErr :: String -> [String] -> String -> IO String cmdIgnoreErr c args input = do (_exit, out, _err) <- readProcessWithExitCode c args input return out --- grep :: String -> FilePath -> IO [String]--- grep pat file =--- cmdLines "grep" [pat, file]+-- | 'grep pat file' greps pattern in file, and returns list of matches+--+-- @since 0.1.2+grep :: String -> FilePath -> IO [String]+grep pat file =+ cmdLines "grep" [pat, file] --- | grep a pattern in file and return Boolean status-grep_ :: String -> FilePath -> IO Bool+-- | 'grep_ pat file' greps pattern in file and returns Boolean status+grep_ :: String -- ^ pattern+ -> FilePath -- ^ file+ -> IO Bool -- ^ result grep_ pat file = cmdBool "grep" ["-q", pat, file] --- | grep for extended regexp in file, and return Boolean status+-- | 'egrep_ pat file' greps extended regexp in file, and returns Boolean status egrep_ :: String -> FilePath -> IO Bool egrep_ pat file = cmdBool "grep" ["-q", "-e", pat, file] --- | sudo a command-sudo :: String -> [String] -> IO ()+-- | 'sudo c args' runs a command as sudo+sudo :: String -- ^ command+ -> [String] -- ^ arguments+ -> IO () sudo c args = cmdlog "sudo" (c:args) -- | Combine strings with a single space@@ -165,17 +204,17 @@ -- singleLine "" = "" -- singleLine s = (head . lines) s --- | Remove a prefix from a string if there+-- | 'removePrefix prefix original' removes prefix from string if present removePrefix :: String -> String-> String removePrefix prefix orig = fromMaybe orig $ stripPrefix prefix orig --- | Remove prefix, or fail with error+-- | 'removeStrictPrefix prefix original' removes prefix, or fails with error removeStrictPrefix :: String -> String -> String removeStrictPrefix prefix orig = fromMaybe (error prefix +-+ "is not prefix of" +-+ orig) $ stripPrefix prefix orig --- | Remove a suffix from a string if there+-- | 'removeSuffix suffix original' removes suffix from string if present removeSuffix :: String -> String -> String removeSuffix suffix orig = fromMaybe orig $ stripSuffix suffix orig
SimpleCmd/Git.hs view
@@ -17,32 +17,35 @@ import Control.Applicative ((<$>)) #endif --- | Run git command and return output-git :: String -> [String] -> IO String+-- | 'git c args' runs git command and return output+git :: String -- ^ git command+ -> [String] -- ^ arguments+ -> IO String -- ^ output git c args = cmd "git" (c:args) --- | Run git command with output to stdout and stderr+-- | 'git_ c args' run git command with output to stdout and stderr git_ :: String -> [String] -> IO () git_ c args = cmd_ "git" (c:args) --- | Check if directory has a .git/ dir+-- | 'isGitDir dir' checks if directory has a .git/ subdir isGitDir :: FilePath -> IO Bool isGitDir dir = doesDirectoryExist (dir </> ".git") --- | Return the git branch of the current directory+-- | 'gitBranch' returns the git branch of the current directory gitBranch :: IO String gitBranch = removePrefix "* " . head . filter (isPrefixOf "* ") <$> cmdLines "git" ["branch"] --- | Check if a git repo is under ssh+-- | 'rwGitDir' checks if a git repo is under ssh rwGitDir :: IO Bool rwGitDir = grepGitConfig "url = (ssh://|git@)" --- | grep ".git/config"--- | since 0.1.1+-- | 'grepGitConfig pat' greps ".git/config" for extended regexp+--+-- @since 0.1.1 grepGitConfig :: String -> IO Bool grepGitConfig key = do gitdir <- isGitDir "."
SimpleCmd/Rpm.hs view
@@ -4,8 +4,9 @@ import SimpleCmd (cmdLines) --- | Run rpmspec query on file with optional args, a newline is appended to any queryformat--- | since 0.1.1+-- | 'rpmspec args maybe-queryformat specfile' runs rpmspec query on file with optional args (a newline is appended to any queryformat)+--+-- @since 0.1.1 rpmspec :: [String] -> Maybe String -> FilePath -> IO [String] rpmspec args mqf spec = do let qf = maybe [] (\ q -> ["--queryformat", q ++ "\n"]) mqf
simple-cmd.cabal view
@@ -1,5 +1,5 @@ name: simple-cmd-version: 0.1.1+version: 0.1.2 synopsis: Simple String-based process commands description: Thin wrappers over System.Process (readProcess,