feat: Adds playbook client
This commit is contained in:
72
Sources/PlaybookClient/PlaybookClient.swift
Normal file
72
Sources/PlaybookClient/PlaybookClient.swift
Normal file
@@ -0,0 +1,72 @@
|
||||
import ConfigurationClient
|
||||
import Dependencies
|
||||
import DependenciesMacros
|
||||
import FileClient
|
||||
import Foundation
|
||||
import ShellClient
|
||||
|
||||
// TODO: Add update checks and pull for the playbook.
|
||||
|
||||
public extension DependencyValues {
|
||||
var playbookClient: PlaybookClient {
|
||||
get { self[PlaybookClient.self] }
|
||||
set { self[PlaybookClient.self] = newValue }
|
||||
}
|
||||
}
|
||||
|
||||
@DependencyClient
|
||||
public struct PlaybookClient: Sendable {
|
||||
|
||||
public var installPlaybook: @Sendable (Configuration) async throws -> Void
|
||||
public var playbookDirectory: @Sendable (Configuration) async throws -> String
|
||||
|
||||
}
|
||||
|
||||
extension PlaybookClient: DependencyKey {
|
||||
public static let testValue: PlaybookClient = Self()
|
||||
|
||||
public static var liveValue: PlaybookClient {
|
||||
.init {
|
||||
try await install(config: $0.playbook)
|
||||
} playbookDirectory: {
|
||||
$0.playbook?.directory ?? Constants.defaultInstallationPath
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func install(config: Configuration.Playbook?) async throws {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
@Dependency(\.asyncShellClient) var shell
|
||||
|
||||
let path = config?.directory ?? PlaybookClient.Constants.defaultInstallationPath
|
||||
let version = config?.version ?? PlaybookClient.Constants.playbookRepoVersion
|
||||
|
||||
let parentDirectory = URL(filePath: path)
|
||||
.deletingLastPathComponent()
|
||||
|
||||
let exists = try await fileClient.isDirectory(parentDirectory)
|
||||
if !exists {
|
||||
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]
|
||||
))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user