feat: Breaking out more dependencies.
This commit is contained in:
@@ -3,8 +3,6 @@ import DependenciesMacros
|
||||
import Foundation
|
||||
import ShellClient
|
||||
|
||||
// TODO: Drop support for non-json configuration.
|
||||
|
||||
public extension DependencyValues {
|
||||
var cliClient: CliClient {
|
||||
get { self[CliClient.self] }
|
||||
@@ -14,11 +12,7 @@ public extension DependencyValues {
|
||||
|
||||
@DependencyClient
|
||||
public struct CliClient: Sendable {
|
||||
public var decoder: @Sendable () -> JSONDecoder = { .init() }
|
||||
public var encoder: @Sendable () -> JSONEncoder = { .init() }
|
||||
public var loadConfiguration: @Sendable () throws -> Configuration
|
||||
public var runCommand: @Sendable ([String], Bool, ShellCommand.Shell) async throws -> Void
|
||||
public var createConfiguration: @Sendable (_ path: String, _ json: Bool) throws -> Void
|
||||
public var findVaultFileInCurrentDirectory: @Sendable () throws -> String
|
||||
|
||||
public func runCommand(
|
||||
@@ -40,30 +34,12 @@ public struct CliClient: Sendable {
|
||||
|
||||
extension CliClient: DependencyKey {
|
||||
|
||||
// swiftlint:disable function_body_length
|
||||
public static func live(
|
||||
decoder: JSONDecoder = .init(),
|
||||
encoder: JSONEncoder = .init(),
|
||||
env: [String: String]
|
||||
) -> Self {
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
return .init {
|
||||
decoder
|
||||
} encoder: {
|
||||
encoder
|
||||
} loadConfiguration: {
|
||||
let url = try findConfigurationFiles(env: env)
|
||||
var env = env
|
||||
|
||||
logger.trace("Loading configuration from: \(url)")
|
||||
guard let config = try fileClient.loadFile(url, &env, decoder) else {
|
||||
return try .fromEnv(env)
|
||||
}
|
||||
return config
|
||||
|
||||
} runCommand: { args, quiet, shell in
|
||||
return .init { args, quiet, shell in
|
||||
@Dependency(\.asyncShellClient) var shellClient
|
||||
if !quiet {
|
||||
try await shellClient.foreground(.init(
|
||||
@@ -80,37 +56,15 @@ extension CliClient: DependencyKey {
|
||||
args
|
||||
))
|
||||
}
|
||||
} createConfiguration: { path, json in
|
||||
|
||||
// Early out if a file exists at the path already.
|
||||
guard !fileClient.fileExists(path) else {
|
||||
throw CliClientError.fileExistsAtPath(path)
|
||||
}
|
||||
|
||||
var path = path
|
||||
let data: Data
|
||||
|
||||
if !json {
|
||||
// Write the default env template.
|
||||
data = Data(Configuration.fileTemplate.utf8)
|
||||
} else {
|
||||
if !path.contains(".json") {
|
||||
path += ".json"
|
||||
}
|
||||
data = try jsonEncoder.encode(Configuration.mock)
|
||||
}
|
||||
|
||||
try fileClient.write(path, data)
|
||||
} findVaultFileInCurrentDirectory: {
|
||||
guard let url = try fileClient.findVaultFileInCurrentDirectory() else {
|
||||
throw CliClientError.vaultFileNotFound
|
||||
}
|
||||
return path(for: url)
|
||||
fatalError()
|
||||
// guard let url = try fileClient.findVaultFileInCurrentDirectory() else {
|
||||
// throw CliClientError.vaultFileNotFound
|
||||
// }
|
||||
// return path(for: url)
|
||||
}
|
||||
}
|
||||
|
||||
// swiftlint:enable function_body_length
|
||||
|
||||
public static var liveValue: CliClient {
|
||||
.live(env: ProcessInfo.processInfo.environment)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user