feat: Breaking out more dependencies.
This commit is contained in:
@@ -13,7 +13,6 @@ public extension DependencyValues {
|
||||
@DependencyClient
|
||||
public struct CliClient: Sendable {
|
||||
public var runCommand: @Sendable ([String], Bool, ShellCommand.Shell) async throws -> Void
|
||||
public var findVaultFileInCurrentDirectory: @Sendable () throws -> String
|
||||
|
||||
public func runCommand(
|
||||
quiet: Bool,
|
||||
@@ -56,12 +55,6 @@ extension CliClient: DependencyKey {
|
||||
args
|
||||
))
|
||||
}
|
||||
} findVaultFileInCurrentDirectory: {
|
||||
fatalError()
|
||||
// guard let url = try fileClient.findVaultFileInCurrentDirectory() else {
|
||||
// throw CliClientError.vaultFileNotFound
|
||||
// }
|
||||
// return path(for: url)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,183 +0,0 @@
|
||||
// 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") }
|
||||
// }
|
||||
// }
|
||||
@@ -1,116 +0,0 @@
|
||||
// import Dependencies
|
||||
// import Foundation
|
||||
// import ShellClient
|
||||
//
|
||||
// @_spi(Internal)
|
||||
// public func findConfigurationFiles(
|
||||
// env: [String: String] = ProcessInfo.processInfo.environment
|
||||
// ) throws -> File {
|
||||
// @Dependency(\.logger) var logger
|
||||
// @Dependency(\.fileClient) var fileClient
|
||||
//
|
||||
// logger.debug("Begin find configuration files.")
|
||||
// logger.trace("Env: \(env)")
|
||||
//
|
||||
// // Check for environment variable pointing to a directory that the
|
||||
// // the configuration lives.
|
||||
// if let pwd = env["PWD"],
|
||||
// let file = fileClient.checkUrl(.directory(pwd))
|
||||
// {
|
||||
// logger.debug("Found configuration in current working directory.")
|
||||
// return file
|
||||
// }
|
||||
//
|
||||
// // Check for environment variable pointing to a file that the
|
||||
// // the configuration lives.
|
||||
// if let configFile = env[EnvironmentKey.hpaConfigFileKey],
|
||||
// let file = fileClient.checkUrl(.file(configFile))
|
||||
// {
|
||||
// logger.debug("Found configuration from hpa config file env var.")
|
||||
// return file
|
||||
// }
|
||||
//
|
||||
// // Check for environment variable pointing to a directory that the
|
||||
// // the configuration lives.
|
||||
// if let configHome = env[EnvironmentKey.hpaConfigDirKey],
|
||||
// let file = fileClient.checkUrl(.directory(configHome))
|
||||
// {
|
||||
// logger.debug("Found configuration from hpa config home env var.")
|
||||
// return file
|
||||
// }
|
||||
//
|
||||
// // Check home directory for a `.hparc` file.
|
||||
// if let file = fileClient.checkUrl(.directory(fileClient.homeDir())) {
|
||||
// logger.debug("Found configuration in home directory")
|
||||
// return file
|
||||
// }
|
||||
//
|
||||
// // Check in xdg config home, under an hpa-playbook directory.
|
||||
// if let xdgConfigHome = env[EnvironmentKey.xdgConfigHomeKey],
|
||||
// let file = fileClient.checkUrl(.directory(xdgConfigHome, HPAKey.hpaConfigDirectoryName))
|
||||
// {
|
||||
// logger.debug("XDG Config url: \(file.path)")
|
||||
// return file
|
||||
// }
|
||||
//
|
||||
// // We could not find configuration in any usual places.
|
||||
// throw ConfigurationError.configurationNotFound
|
||||
// }
|
||||
//
|
||||
// @_spi(Internal)
|
||||
// public func path(for url: URL) -> String {
|
||||
// url.absoluteString.replacing("file://", with: "")
|
||||
// }
|
||||
//
|
||||
// enum ConfigurationError: Error {
|
||||
// case configurationNotFound
|
||||
// }
|
||||
//
|
||||
// private extension FileClient {
|
||||
//
|
||||
// static let validFileNames = [
|
||||
// "config.json", "config.toml",
|
||||
// ".hparc.json", ".hparc.toml"
|
||||
// ]
|
||||
//
|
||||
// enum ConfigurationUrlCheck {
|
||||
// case file(URL)
|
||||
// case directory(URL)
|
||||
//
|
||||
// static func file(_ path: String) -> Self { .file(URL(filePath: path)) }
|
||||
//
|
||||
// static func file(_ paths: String...) -> Self {
|
||||
// var url = URL(filePath: paths[0])
|
||||
// url = paths.dropFirst().reduce(into: url) { $0.append(path: $1) }
|
||||
// return .file(url)
|
||||
// }
|
||||
//
|
||||
// static func directory(_ path: String) -> Self { .directory(URL(filePath: path)) }
|
||||
//
|
||||
// static func directory(_ paths: String...) -> Self {
|
||||
// var url = URL(filePath: paths[0])
|
||||
// url = paths.dropFirst().reduce(into: url) { $0.append(path: $1) }
|
||||
// return .directory(url)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func checkUrl(_ check: ConfigurationUrlCheck) -> File? {
|
||||
// switch check {
|
||||
// case let .file(url):
|
||||
// if isReadable(url) { return .init(url) }
|
||||
// return nil
|
||||
// case let .directory(url):
|
||||
// return findConfigurationInDirectory(url)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func findConfigurationInDirectory(_ url: URL) -> File? {
|
||||
// for file in Self.validFileNames {
|
||||
// let fileUrl = url.appending(path: file)
|
||||
// if isReadable(fileUrl) {
|
||||
// return .init(fileUrl)
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user