Skip to content

Commit 7fe287e

Browse files
Merge branch 'main' into louis/self-update-gpg-verify
2 parents 3d501af + 4fab0c0 commit 7fe287e

File tree

18 files changed

+791
-694
lines changed

18 files changed

+791
-694
lines changed

Package.resolved

Lines changed: 56 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:6.0
1+
// swift-tools-version:6.2
22

33
import PackageDescription
44

@@ -31,6 +31,7 @@ let package = Package(
3131
.package(url: "https://github.com/apple/swift-openapi-generator", from: "1.7.2"),
3232
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.8.2"),
3333
.package(url: "https://github.com/apple/swift-system", from: "1.4.2"),
34+
.package(url: "https://github.com/swiftlang/swift-subprocess", exact: "0.2.1", traits: []),
3435
// This dependency provides the correct version of the formatter so that you can run `swift run swiftformat Package.swift Plugins/ Sources/ Tests/`
3536
.package(url: "https://github.com/nicklockwood/SwiftFormat", exact: "0.49.18"),
3637
],
@@ -67,6 +68,7 @@ let package = Package(
6768
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime"),
6869
.product(name: "OpenAPIAsyncHTTPClient", package: "swift-openapi-async-http-client"),
6970
.product(name: "SystemPackage", package: "swift-system"),
71+
.product(name: "Subprocess", package: "swift-subprocess"),
7072
],
7173
swiftSettings: swiftSettings,
7274
plugins: ["GenerateCommandModels"]

Sources/LinuxPlatform/Linux.swift

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import Subprocess
23
import SwiftlyCore
34
import SystemPackage
45

@@ -263,7 +264,13 @@ public struct Linux: Platform {
263264
}
264265

265266
if requireSignatureValidation {
266-
guard (try? self.runProgram("gpg", "--version", quiet: true)) != nil else {
267+
let result = try await run(
268+
.name("gpg"),
269+
arguments: ["--version"],
270+
output: .discarded
271+
)
272+
273+
if !result.terminationStatus.isSuccess {
267274
var msg = "gpg is not installed. "
268275
if let manager {
269276
msg += """
@@ -278,7 +285,7 @@ public struct Linux: Platform {
278285
throw SwiftlyError(message: msg)
279286
}
280287

281-
try await importGpgKeys(ctx)
288+
try await self.importGpgKeys(ctx)
282289
}
283290

284291
guard let manager = manager else {
@@ -304,7 +311,12 @@ public struct Linux: Platform {
304311
do {
305312
switch manager {
306313
case "apt-get":
307-
if let pkgList = try await self.runProgramOutput("dpkg", "-l", package) {
314+
let result = try await run(.name("dpkg"), arguments: ["-l", package], output: .string(limit: 100 * 1024))
315+
if !result.terminationStatus.isSuccess {
316+
return false
317+
}
318+
319+
if let pkgList = result.standardOutput {
308320
// The package might be listed but not in an installed non-error state.
309321
//
310322
// Look for something like this:
@@ -318,8 +330,8 @@ public struct Linux: Platform {
318330
}
319331
return false
320332
case "yum":
321-
try self.runProgram("yum", "list", "installed", package, quiet: true)
322-
return true
333+
let result = try await run(.name("yum"), arguments: ["list", "installed", package], output: .discarded)
334+
return result.terminationStatus.isSuccess
323335
default:
324336
return true
325337
}
@@ -379,7 +391,15 @@ public struct Linux: Platform {
379391
tmpDir / String(name)
380392
}
381393

382-
try self.runProgram((tmpDir / "swiftly").string, "init")
394+
let config = Configuration(
395+
executable: .path(tmpDir / "swiftly"),
396+
arguments: ["init"]
397+
)
398+
399+
let result = try await run(config, output: .standardOutput, error: .standardError)
400+
if !result.terminationStatus.isSuccess {
401+
throw RunProgramError(terminationStatus: result.terminationStatus, config: config)
402+
}
383403
}
384404
}
385405

@@ -402,8 +422,8 @@ public struct Linux: Platform {
402422
_ ctx: SwiftlyCoreContext, toolchainFile: ToolchainFile, archive: FilePath, verbose: Bool
403423
) async throws {
404424
// Ensure GPG keys are imported before attempting signature verification
405-
try await importGpgKeys(ctx)
406-
425+
try await self.importGpgKeys(ctx)
426+
407427
if verbose {
408428
await ctx.message("Downloading toolchain signature...")
409429
}
@@ -416,11 +436,9 @@ public struct Linux: Platform {
416436
await ctx.message("Verifying toolchain signature...")
417437
do {
418438
if let mockedHomeDir = ctx.mockedHomeDir {
419-
var env = ProcessInfo.processInfo.environment
420-
env["GNUPGHOME"] = (mockedHomeDir / ".gnupg").string
421-
try await sys.gpg().verify(detached_signature: sigFile, signed_data: archive).run(self, env: env, quiet: false)
439+
try await sys.gpg().verify(detached_signature: sigFile, signed_data: archive).run(environment: .inherit.updating(["GNUPGHOME": (mockedHomeDir / ".gnupg").string]), quiet: false)
422440
} else {
423-
try await sys.gpg().verify(detached_signature: sigFile, signed_data: archive).run(self, quiet: !verbose)
441+
try await sys.gpg().verify(detached_signature: sigFile, signed_data: archive).run(quiet: !verbose)
424442
}
425443
} catch {
426444
throw SwiftlyError(message: "Signature verification failed: \(error).")
@@ -437,9 +455,9 @@ public struct Linux: Platform {
437455
if let mockedHomeDir = ctx.mockedHomeDir {
438456
var env = ProcessInfo.processInfo.environment
439457
env["GNUPGHOME"] = (mockedHomeDir / ".gnupg").string
440-
try await sys.gpg()._import(key: tmpFile).run(self, env: env, quiet: true)
458+
try await sys.gpg()._import(key: tmpFile).run(environment: .init(env), quiet: true)
441459
} else {
442-
try await sys.gpg()._import(key: tmpFile).run(self, quiet: true)
460+
try await sys.gpg()._import(key: tmpFile).run(quiet: true)
443461
}
444462
}
445463
}
@@ -448,8 +466,8 @@ public struct Linux: Platform {
448466
_ ctx: SwiftlyCoreContext, archiveDownloadURL: URL, archive: FilePath, verbose: Bool
449467
) async throws {
450468
// Ensure GPG keys are imported before attempting signature verification
451-
try await importGpgKeys(ctx)
452-
469+
try await self.importGpgKeys(ctx)
470+
453471
if verbose {
454472
await ctx.message("Downloading swiftly signature...")
455473
}
@@ -464,11 +482,9 @@ public struct Linux: Platform {
464482
await ctx.message("Verifying swiftly signature...")
465483
do {
466484
if let mockedHomeDir = ctx.mockedHomeDir {
467-
var env = ProcessInfo.processInfo.environment
468-
env["GNUPGHOME"] = (mockedHomeDir / ".gnupg").string
469-
try await sys.gpg().verify(detached_signature: sigFile, signed_data: archive).run(self, env: env, quiet: false)
485+
try await sys.gpg().verify(detached_signature: sigFile, signed_data: archive).run(environment: .inherit.updating(["GNUPGHOME": (mockedHomeDir / ".gnupg").string]), quiet: false)
470486
} else {
471-
try await sys.gpg().verify(detached_signature: sigFile, signed_data: archive).run(self, quiet: !verbose)
487+
try await sys.gpg().verify(detached_signature: sigFile, signed_data: archive).run(quiet: !verbose)
472488
}
473489
} catch {
474490
throw SwiftlyError(message: "Signature verification failed: \(error).")
@@ -622,7 +638,7 @@ public struct Linux: Platform {
622638

623639
public func getShell() async throws -> String {
624640
let userName = ProcessInfo.processInfo.userName
625-
if let entry = try await sys.getent(database: "passwd", key: userName).entries(self).first {
641+
if let entry = try await sys.getent(database: "passwd", key: userName).entries().first {
626642
if let shell = entry.last { return shell }
627643
}
628644

0 commit comments

Comments
 (0)