feat: Begins work on supporting toml for configuration.

This commit is contained in:
2024-12-09 17:00:52 -05:00
parent a8d35fed37
commit 87390c4b63
14 changed files with 354 additions and 196 deletions

View File

@@ -13,13 +13,30 @@ public extension DependencyValues {
@_spi(Internal)
@DependencyClient
public struct FileClient: Sendable {
public var loadFile: @Sendable (URL, inout [String: String], JSONDecoder) throws -> Void
/// Loads a file at the given path into the environment unless it can decode it as json,
/// at which point it will return the decoded file contents as a ``Configuration`` item.
public var loadFile: @Sendable (URL, inout [String: String], JSONDecoder) throws -> Configuration?
/// Returns the user's home directory path.
public var homeDir: @Sendable () -> URL = { URL(string: "~/")! }
/// Check if a path is a directory.
public var isDirectory: @Sendable (URL) -> Bool = { _ in false }
/// Check if a path is a readable.
public var isReadable: @Sendable (URL) -> Bool = { _ in false }
/// Check if a file exists at the path.
public var fileExists: @Sendable (String) -> Bool = { _ in false }
public var findVaultFileInCurrentDirectory: @Sendable () throws -> URL?
public var findVaultFile: @Sendable (String) throws -> URL?
/// Write data to a file.
public var write: @Sendable (String, Data) throws -> Void
public func findVaultFileInCurrentDirectory() throws -> URL? {
try findVaultFile(".")
}
}
@_spi(Internal)
@@ -34,7 +51,7 @@ extension FileClient: DependencyKey {
isDirectory: { client.isDirectory(url: $0) },
isReadable: { client.isReadable(url: $0) },
fileExists: { client.fileExists(at: $0) },
findVaultFileInCurrentDirectory: { try client.findVaultFileInCurrentDirectory() },
findVaultFile: { try client.findVaultFile(in: $0) },
write: { path, data in
try data.write(to: URL(filePath: path))
}
@@ -68,8 +85,11 @@ private struct LiveFileClient: @unchecked Sendable {
fileManager.fileExists(atPath: path)
}
func findVaultFileInCurrentDirectory() throws -> URL? {
let urls = try fileManager.contentsOfDirectory(at: URL(filePath: "."), includingPropertiesForKeys: nil)
// func findVaultFileInCurrentDirectory() throws -> URL? {
func findVaultFile(in filePath: String) throws -> URL? {
let urls = try fileManager
.contentsOfDirectory(at: URL(filePath: filePath), includingPropertiesForKeys: nil)
if let vault = urls.firstVaultFile {
return vault
@@ -93,16 +113,18 @@ private struct LiveFileClient: @unchecked Sendable {
return nil
}
func loadFile(at url: URL, into env: inout [String: String], decoder: JSONDecoder) throws {
func loadFile(
at url: URL,
into env: inout [String: String],
decoder: JSONDecoder
) throws -> Configuration? {
@Dependency(\.logger) var logger
logger.trace("Begin load file for: \(path(for: url))")
if url.absoluteString.hasSuffix(".json") {
// Handle json file.
let data = try Data(contentsOf: url)
let dict = (try? decoder.decode([String: String].self, from: data)) ?? [:]
env.merge(dict, uniquingKeysWith: { $1 })
return
return try decoder.decode(Configuration.self, from: data)
}
let string = try String(contentsOfFile: path(for: url), encoding: .utf8)
@@ -126,6 +148,7 @@ private struct LiveFileClient: @unchecked Sendable {
env[String(splitLine[0])] = String(splitLine[1])
}
}
return nil
}
}