feat: Removes cli-client

This commit is contained in:
2024-12-16 17:14:25 -05:00
parent 8c402f3f5f
commit 85b285347b
32 changed files with 837 additions and 1580 deletions

View File

@@ -15,7 +15,7 @@ public extension DependencyValues {
@DependencyClient
public struct ConfigurationClient: Sendable {
public var find: @Sendable () async throws -> File
var generate: @Sendable (File, Bool) async throws -> Void
public var generate: @Sendable (GenerateOptions) async throws -> String
public var load: @Sendable (File?) async throws -> Configuration
var write: @Sendable (File, Configuration, Bool) async throws -> Void
@@ -24,13 +24,6 @@ public struct ConfigurationClient: Sendable {
return try await load(file)
}
public func generate(
at file: File,
force: Bool = false
) async throws {
try await generate(file, force)
}
public func write(
_ configuration: Configuration,
to file: File,
@@ -38,6 +31,28 @@ public struct ConfigurationClient: Sendable {
) async throws {
try await write(file, configuration, force)
}
public struct GenerateOptions: Equatable, Sendable {
public let force: Bool
public let json: Bool
public let path: Path?
public init(
force: Bool = false,
json: Bool = false,
path: Path? = nil
) {
self.force = force
self.json = json
self.path = path
}
public enum Path: Equatable, Sendable {
case file(File)
case directory(String)
}
}
}
extension ConfigurationClient: DependencyKey {
@@ -47,8 +62,8 @@ extension ConfigurationClient: DependencyKey {
let liveClient = LiveConfigurationClient(environment: environment)
return .init {
try await liveClient.find()
} generate: { file, force in
try await liveClient.generate(at: file, force: force)
} generate: {
try await liveClient.generate($0)
} load: { file in
try await liveClient.load(file: file)
} write: { file, configuration, force in
@@ -126,10 +141,24 @@ struct LiveConfigurationClient {
throw ConfigurationError.configurationNotFound
}
func generate(at file: File, force: Bool) async throws {
func generate(_ options: ConfigurationClient.GenerateOptions) async throws -> String {
@Dependency(\.logger) var logger
logger.debug("Begin generating configuration: \(file.path), force: \(force)")
let file: File
if let path = options.path {
switch path {
case let .file(requestedFile):
file = requestedFile
case let .directory(directory):
file = .init("\(directory)/\(HPAKey.defaultFileNameWithoutExtension).\(options.json ? "json" : "toml")")!
}
} else {
let configDir = "\(environment.xdgConfigHome)/\(HPAKey.configDirName)"
file = .init("\(configDir)/\(HPAKey.defaultFileName)")!
}
logger.debug("Begin generating configuration: \(file.path), force: \(options.force)")
let expandedPath = file.path.replacingOccurrences(
of: "~",
@@ -138,7 +167,7 @@ struct LiveConfigurationClient {
let fileUrl = URL(filePath: expandedPath)
if !force {
if !options.force {
guard !fileManager.fileExists(fileUrl) else {
throw ConfigurationError.fileExists(path: file.path)
}
@@ -166,8 +195,10 @@ struct LiveConfigurationClient {
} else {
// Json does not allow comments, so we write the mock configuration
// to the file path.
try await write(.mock, to: File(fileUrl)!, force: force)
try await write(.mock, to: File(fileUrl)!, force: options.force)
}
return fileUrl.cleanFilePath
}
func load(file: File?) async throws -> Configuration {

View File

@@ -11,6 +11,7 @@ public enum HPAKey {
public static let resourceFileName = "hpa"
public static let resourceFileExtension = "toml"
public static let defaultFileName = "config.toml"
public static let defaultFileNameWithoutExtension = "config.toml"
}
extension [String: String] {