Skip to content

Commit a40a731

Browse files
committed
Add quoteArguments option (default: true) to shellOut command
1 parent b62dc4d commit a40a731

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

Package.resolved

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ let package = Package(
1313
products: [
1414
.library(name: "ShellOut", targets: ["ShellOut"])
1515
],
16+
dependencies: [
17+
.package(url: "https://github.com/SwiftPackageIndex/ShellQuote", from: "1.0.0"),
18+
],
1619
targets: [
1720
.target(
1821
name: "ShellOut",
22+
dependencies: [
23+
.product(name: "ShellQuote", package: "ShellQuote")
24+
],
1925
path: "Sources"
2026
),
2127
.testTarget(

Sources/ShellOut.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import Foundation
88
import Dispatch
9+
import ShellQuote
910

1011
// MARK: - API
1112

@@ -35,8 +36,11 @@ import Dispatch
3536
process: Process = .init(),
3637
outputHandle: FileHandle? = nil,
3738
errorHandle: FileHandle? = nil,
38-
environment: [String : String]? = nil
39+
environment: [String : String]? = nil,
40+
quoteArguments: Bool = true
3941
) throws -> String {
42+
let arguments = quoteArguments ? arguments.map(ShellQuote.quote) : arguments
43+
print("*** arguments: ", arguments)
4044
let command = "cd \(path.escapingSpaces) && \(command) \(arguments.joined(separator: " "))"
4145

4246
return try process.launchBash(

Tests/ShellOutTests/ShellOutTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,9 @@ class ShellOutTests: XCTestCase {
174174
try shellOut(to: .generateSwiftPackageXcodeProject(), at: packagePath)
175175
XCTAssertTrue(try shellOut(to: "ls -a", at: packagePath).contains("SwiftPackageManagerTest.xcodeproj"))
176176
}
177+
178+
func testArgumentQuoting() throws {
179+
XCTAssertEqual(try shellOut(to: "echo", arguments: ["foo ; echo bar"]), "foo ; echo bar")
180+
XCTAssertEqual(try shellOut(to: "echo", arguments: ["foo ; echo bar"], quoteArguments: false), "foo\nbar")
181+
}
177182
}

0 commit comments

Comments
 (0)