Handle `tart run --no-graphics --vnc` properly (#137)

Let's start a VNC server but not force open Screen Sharing if `--no-graphics` is also passed.
This commit is contained in:
Fedor Korotkov 2022-06-24 15:54:41 -04:00 committed by GitHub
parent c25364b2d4
commit 92529afa23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 18 deletions

View File

@ -43,7 +43,16 @@ struct Run: AsyncParsableCommand {
let task = Task {
do {
if let vncWrapper = vncWrapper {
await vncWrapper.open()
let port = try await vncWrapper.waitForPort()
let url = URL(string: "vnc://:\(vncWrapper.password)@127.0.0.1:\(port)")!
if noGraphics || ProcessInfo.processInfo.environment["CI"] != nil {
print("VNC server is running at \(url)")
} else {
print("Opening \(url)...")
NSWorkspace.shared.open(url)
}
}
try await vm!.run(recovery)

View File

@ -3,7 +3,7 @@ import Dynamic
import Virtualization
class VNCWrapper {
private let password: String
let password: String
private let vnc: Dynamic
init(virtualMachine: VZVirtualMachine) {
@ -15,21 +15,6 @@ class VNCWrapper {
vnc.start()
}
func open() async {
do {
let port = try await Self.waitForPort(vnc: vnc)
let url = URL(string: "vnc://:\(password)@127.0.0.1:\(port)")!
print("Opening \(url)...")
if ProcessInfo.processInfo.environment["CI"] == nil {
NSWorkspace.shared.open(url)
}
} catch {
print("Failed to retrieve a VNC server port: \(error)")
}
}
func stop() throws {
vnc.stop()
}
@ -38,7 +23,7 @@ class VNCWrapper {
try? stop()
}
private static func waitForPort(vnc: Dynamic) async throws -> UInt16 {
func waitForPort() async throws -> UInt16 {
while true {
// Port is 0 shortly after start(),
// but will be initialized later