-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTart.swift
66 lines (58 loc) · 2.37 KB
/
Tart.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import Foundation
import ShellDomain
public struct Tart {
private let homeProvider: TartHomeProvider
private let shell: Shell
private var environment: [String: String]? {
guard let homeFolderURL = homeProvider.homeFolderURL else {
return nil
}
return ["TART_HOME": homeFolderURL.absoluteString]
}
public init(homeProvider: TartHomeProvider, shell: Shell) {
self.homeProvider = homeProvider
self.shell = shell
}
public func clone(sourceName: String, newName: String) async throws {
try await executeCommand(withArguments: ["clone", sourceName, newName])
}
public func run(name: String) async throws {
let homeFolderURL = homeProvider.homeFolderURL ??
FileManager.default.homeDirectoryForCurrentUser.appending(component: ".tart")
let cacheFolder = homeFolderURL.appendingPathComponent("cache")
if !FileManager.default.fileExists(atPath: cacheFolder.path) {
try FileManager.default.createDirectory(atPath: cacheFolder.path, withIntermediateDirectories: true)
}
try await executeCommand(withArguments: ["run", "--dir=cache:\(cacheFolder.path())", name])
}
public func delete(name: String) async throws {
try await executeCommand(withArguments: ["delete", name])
}
public func list() async throws -> [String] {
let result = try await executeCommand(withArguments: ["list", "-q", "--source", "local"])
return result.split(separator: "\n").map(String.init)
}
public func getIPAddress(ofVirtualMachineNamed name: String) async throws -> String {
let result = try await executeCommand(withArguments: ["ip", name])
return result.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
private extension Tart {
@discardableResult
private func executeCommand(withArguments arguments: [String]) async throws -> String {
let locator = TartLocator(shell: shell)
let filePath = try locator.locate()
if let environment {
return try await shell.runExecutable(
atPath: filePath,
withArguments: arguments,
environment: environment
)
} else {
return try await shell.runExecutable(
atPath: filePath,
withArguments: arguments
)
}
}
}