feat: Preparing to move items out of cli-client module into playbook-client

This commit is contained in:
2024-12-14 10:06:30 -05:00
parent b5afc77428
commit 303cdef84b
7 changed files with 211 additions and 112 deletions

View File

@@ -16,64 +16,40 @@ public extension DependencyValues {
@DependencyClient
public struct PlaybookClient: Sendable {
// TODO: Remove the configuration and have it passed in.
public var installPlaybook: @Sendable (Configuration) async throws -> Void
public var playbookDirectory: @Sendable (Configuration) async throws -> String
public var repository: Repository
}
extension PlaybookClient: DependencyKey {
public static let testValue: PlaybookClient = Self()
public extension PlaybookClient {
public static var liveValue: PlaybookClient {
.init {
try await install(config: $0.playbook)
} playbookDirectory: {
$0.playbook?.directory ?? Constants.defaultInstallationPath
@DependencyClient
struct Repository: Sendable {
public var install: @Sendable (Configuration?) async throws -> Void
public var directory: @Sendable (Configuration?) async throws -> String
public func install() async throws {
try await install(nil)
}
public func directory() async throws -> String {
try await directory(nil)
}
public static var liveValue: Self {
.init {
try await installPlaybook(configuration: $0)
} directory: {
try await findDirectory(configuration: $0)
}
}
}
}
private func install(config: Configuration.Playbook?) async throws {
@Dependency(\.fileClient) var fileClient
@Dependency(\.logger) var logger
@Dependency(\.asyncShellClient) var shell
extension PlaybookClient: DependencyKey {
public static let testValue: PlaybookClient = Self(repository: Repository())
let (path, version) = parsePlaybookPathAndVerion(config)
let parentDirectory = URL(filePath: path)
.deletingLastPathComponent()
let parentExists = try await fileClient.isDirectory(parentDirectory)
if !parentExists {
try await fileClient.createDirectory(parentDirectory)
}
let playbookExists = try await fileClient.isDirectory(URL(filePath: path))
if !playbookExists {
try await shell.foreground(.init([
"git", "clone",
"--branch", version,
PlaybookClient.Constants.playbookRepoUrl, path
]))
} else {
logger.debug("Playbook exists, ensuring it's up to date.")
try await shell.foreground(.init(
in: path,
["git", "pull", "--tags"]
))
try await shell.foreground(.init(
in: path,
["git", "checkout", version]
))
public static var liveValue: PlaybookClient {
.init(
repository: .liveValue
)
}
}
private func parsePlaybookPathAndVerion(_ configuration: Configuration.Playbook?) -> (path: String, version: String) {
return (
path: configuration?.directory ?? PlaybookClient.Constants.defaultInstallationPath,
version: configuration?.version ?? PlaybookClient.Constants.playbookRepoVersion
)
}