on master help sign in
Extend and share shell quoting and escaping logic
commit1199ef3d74e7ddc23b32269dabd2026d763fbbde authorElliot Cameron <eacameron@gmail.com> dateWed 17 Jul 2019, 11:18 (7 years ago) parent73bef50
3 files changed+12−4
lib/cliapp/src/Obelisk/CliApp.hs+2 lib/cliapp/src/Obelisk/CliApp/Process.hs+9−2 lib/command/src/Obelisk/Command/Utils.hs+1−2
lib/cliapp/src/Obelisk/CliApp.hs+2
@@ -43,6 +43,8 @@ module Obelisk.CliApp
4343 , callProcess
4444 , callCommand
4545 , reconstructCommand
46+ , shellQuoteAndEscapeDouble
47+ , shellQuoteAndEscapeSingle
4648 ) where
4749
4850import Control.Monad.Log (Severity (..))
lib/cliapp/src/Obelisk/CliApp/Process.hs+9−2
@@ -20,6 +20,8 @@ module Obelisk.CliApp.Process
2020 , callProcess
2121 , callCommand
2222 , reconstructCommand
23+ , shellQuoteAndEscapeDouble
24+ , shellQuoteAndEscapeSingle
2325 ) where
2426
2527import Control.Monad ((<=<), join, void)
@@ -181,5 +183,10 @@ reconstructCommand :: Process.CmdSpec -> Text
181183reconstructCommand (Process.ShellCommand str) = T.pack str
182184reconstructCommand (Process.RawCommand c as) = processToShellString c as
183185 where
184− processToShellString cmd args = T.unwords $ map quoteAndEscape (cmd : args)
185− quoteAndEscape x = "'" <> T.replace "'" "'\''" (T.pack x) <> "'"
186+ processToShellString cmd args = T.unwords $ map (shellQuoteAndEscapeSingle . T.pack) (cmd : args)
187+
188+shellQuoteAndEscapeSingle :: Text -> Text
189+shellQuoteAndEscapeSingle x = "'" <> T.replace "'" "'\''" x <> "'"
190+
191+shellQuoteAndEscapeDouble :: Text -> Text
192+shellQuoteAndEscapeDouble x = "\"" <> T.replace "\"" "\\\"" x <> "\""
lib/command/src/Obelisk/Command/Utils.hs+1−2
@@ -83,8 +83,7 @@ readGitProcessNoRepo :: MonadObelisk m => [String] -> m Text
8383readGitProcessNoRepo = readProcessAndLogOutput (Debug, Notice) . gitProcNoRepo
8484
8585processToShellString :: FilePath -> [String] -> String
86−processToShellString cmd args = unwords $ map quoteAndEscape (cmd : args)
87− where quoteAndEscape x = T.unpack $ "'" <> T.replace "'" "'\''" (T.pack x) <> "'"
86+processToShellString cmd args = T.unpack $ T.unwords $ map (shellQuoteAndEscapeSingle . T.pack) (cmd : args)
8887
8988-- | A simpler wrapper for CliApp's most used process function with sensible defaults.
9089runProc :: MonadObelisk m => P.CreateProcess -> m ()