-std::string shellQuote(StringPiece argument) {
- std::string quoted = "'";
- for (auto c : argument) {
- if (c == '\'') {
- quoted += "'\\''";
- } else {
- quoted += c;
- }
+std::string shellQuote(StringPiece argument);
+
+namespace detail {
+template <typename... Arguments>
+std::vector<std::string> shellify(
+ StringPiece format,
+ Arguments&&... arguments) {
+ auto command = sformat(
+ format,
+ shellQuote(to<std::string>(std::forward<Arguments>(arguments)))...);
+ return {"/bin/sh", "-c", command};
+}
+
+struct ShellCmdFormat {
+ StringPiece format;
+ template <typename... Arguments>
+ std::vector<std::string> operator()(Arguments&&... arguments) const {
+ return ::folly::detail::shellify(
+ format, std::forward<Arguments>(arguments)...);