184 lines
5.6 KiB
Swift
184 lines
5.6 KiB
Swift
// import CodersClient
|
|
// import Dependencies
|
|
// import DependenciesMacros
|
|
// import Foundation
|
|
// import TOMLKit
|
|
//
|
|
// @_spi(Internal)
|
|
// public extension DependencyValues {
|
|
// var fileClient: FileClient {
|
|
// get { self[FileClient.self] }
|
|
// set { self[FileClient.self] = newValue }
|
|
// }
|
|
// }
|
|
//
|
|
// @_spi(Internal)
|
|
// @DependencyClient
|
|
// public struct FileClient: Sendable {
|
|
//
|
|
// /// Load a file as `Data`.
|
|
// public var loadFile: @Sendable (URL) throws -> Data
|
|
//
|
|
// /// 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 findVaultFile: @Sendable (String) throws -> URL?
|
|
//
|
|
// /// Write data to a file.
|
|
// public var write: @Sendable (File, Data) throws -> Void
|
|
//
|
|
// public func findVaultFileInCurrentDirectory() throws -> URL? {
|
|
// try findVaultFile(".")
|
|
// }
|
|
//
|
|
// public func loadFile(_ file: File) throws -> Data {
|
|
// try loadFile(file.url)
|
|
// }
|
|
//
|
|
// public func loadConfiguration(
|
|
// _ file: File
|
|
// ) throws -> Configuration {
|
|
// @Dependency(\.coders) var coders
|
|
//
|
|
// let data = try loadFile(file)
|
|
//
|
|
// switch file {
|
|
// case .json:
|
|
// return try coders.jsonDecoder().decode(Configuration.self, from: data)
|
|
// case .toml:
|
|
// guard let string = String(data: data, encoding: .utf8) else {
|
|
// throw DecodingError()
|
|
// }
|
|
// return try coders.tomlDecoder().decode(Configuration.self, from: string)
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// @_spi(Internal)
|
|
// extension FileClient: DependencyKey {
|
|
// public static let testValue: FileClient = Self()
|
|
//
|
|
// public static func live(fileManager: FileManager = .default) -> Self {
|
|
// let client = LiveFileClient(fileManager: fileManager)
|
|
// return Self(
|
|
// loadFile: { try Data(contentsOf: $0) },
|
|
// homeDir: { client.homeDir },
|
|
// isDirectory: { client.isDirectory(url: $0) },
|
|
// isReadable: { client.isReadable(url: $0) },
|
|
// fileExists: { client.fileExists(at: $0) },
|
|
// findVaultFile: { try client.findVaultFile(in: $0) },
|
|
// write: { file, data in
|
|
// try data.write(to: file.url)
|
|
// }
|
|
// )
|
|
// }
|
|
//
|
|
// public static let liveValue = Self.live()
|
|
// }
|
|
//
|
|
// private struct LiveFileClient: @unchecked Sendable {
|
|
//
|
|
// private let fileManager: FileManager
|
|
//
|
|
// init(fileManager: FileManager) {
|
|
// self.fileManager = fileManager
|
|
// }
|
|
//
|
|
// var homeDir: URL { fileManager.homeDirectoryForCurrentUser }
|
|
//
|
|
// func isDirectory(url: URL) -> Bool {
|
|
// var isDirectory: ObjCBool = false
|
|
// fileManager.fileExists(atPath: path(for: url), isDirectory: &isDirectory)
|
|
// return isDirectory.boolValue
|
|
// }
|
|
//
|
|
// func isReadable(url: URL) -> Bool {
|
|
// fileManager.isReadableFile(atPath: path(for: url))
|
|
// }
|
|
//
|
|
// func fileExists(at path: String) -> Bool {
|
|
// fileManager.fileExists(atPath: path)
|
|
// }
|
|
//
|
|
// 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
|
|
// }
|
|
//
|
|
// // check for folders that end with "vars" and search those next.
|
|
// for folder in urls.filter({ $0.absoluteString.hasSuffix("vars/") }) {
|
|
// let files = try fileManager.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)
|
|
// if let vault = files.firstVaultFile {
|
|
// return vault
|
|
// }
|
|
// }
|
|
//
|
|
// // Fallback to check all sub-folders
|
|
// for folder in urls.filter({ self.isDirectory(url: $0) && !$0.absoluteString.hasSuffix("vars/") }) {
|
|
// let files = try fileManager.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil)
|
|
// if let vault = files.firstVaultFile {
|
|
// return vault
|
|
// }
|
|
// }
|
|
// return nil
|
|
// }
|
|
//
|
|
// 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)
|
|
// return try decoder.decode(Configuration.self, from: data)
|
|
// }
|
|
//
|
|
// let string = try String(contentsOfFile: path(for: url), encoding: .utf8)
|
|
//
|
|
// logger.trace("Loaded file contents: \(string)")
|
|
//
|
|
// let lines = string.split(separator: "\n")
|
|
// for line in lines {
|
|
// logger.trace("Line: \(line)")
|
|
// let strippedLine = line.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
// let splitLine = strippedLine.split(separator: "=").map {
|
|
// $0.replacingOccurrences(of: "\"", with: "")
|
|
// }
|
|
// logger.trace("Split Line: \(splitLine)")
|
|
// guard splitLine.count >= 2 else { continue }
|
|
//
|
|
// if splitLine.count > 2 {
|
|
// let rest = splitLine.dropFirst()
|
|
// env[String(splitLine[0])] = String(rest.joined(separator: "="))
|
|
// } else {
|
|
// env[String(splitLine[0])] = String(splitLine[1])
|
|
// }
|
|
// }
|
|
// return nil
|
|
// }
|
|
// }
|
|
//
|
|
// struct DecodingError: Error {}
|
|
//
|
|
// private extension Array where Element == URL {
|
|
// var firstVaultFile: URL? {
|
|
// first { $0.absoluteString.hasSuffix("vault.yml") }
|
|
// }
|
|
// }
|