feat: Initial commit
This commit is contained in:
73
Sources/CliClient/Helpers.swift
Normal file
73
Sources/CliClient/Helpers.swift
Normal file
@@ -0,0 +1,73 @@
|
||||
import Dependencies
|
||||
import Foundation
|
||||
import ShellClient
|
||||
|
||||
@_spi(Internal)
|
||||
public func findConfigurationFiles(
|
||||
env: [String: String] = ProcessInfo.processInfo.environment
|
||||
) throws -> [URL] {
|
||||
@Dependency(\.logger) var logger
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
|
||||
logger.debug("Begin find configuration files.")
|
||||
logger.trace("Env: \(env)")
|
||||
|
||||
let homeDir = fileClient.homeDir()
|
||||
var url = homeDir.appending(path: ".hparc")
|
||||
|
||||
if fileClient.isReadable(url) {
|
||||
logger.debug("Found configuration in home directory")
|
||||
return [url]
|
||||
}
|
||||
|
||||
if let configHome = env["HPA_CONFIG_HOME"] {
|
||||
url = .init(filePath: configHome)
|
||||
|
||||
if fileClient.isDirectory(url) {
|
||||
logger.debug("Found configuration directory from hpa config home env var.")
|
||||
return try fileClient.contentsOfDirectory(url)
|
||||
}
|
||||
if fileClient.isReadable(url) {
|
||||
logger.debug("Found configuration from hpa config home env var.")
|
||||
return [url]
|
||||
}
|
||||
}
|
||||
|
||||
if let pwd = env["PWD"] {
|
||||
url = .init(filePath: "\(pwd)").appending(path: ".hparc")
|
||||
if fileClient.isReadable(url) {
|
||||
logger.debug("Found configuration in current working directory.")
|
||||
return [url]
|
||||
}
|
||||
}
|
||||
|
||||
if let xdgConfigHome = env["XDG_CONFIG_HOME"] {
|
||||
logger.debug("XDG Config Home: \(xdgConfigHome)")
|
||||
|
||||
url = .init(filePath: "\(xdgConfigHome)")
|
||||
.appending(path: "hpa-playbook")
|
||||
|
||||
logger.debug("XDG Config url: \(url.absoluteString)")
|
||||
|
||||
if fileClient.isDirectory(url) {
|
||||
logger.debug("Found configuration in xdg config home.")
|
||||
return try fileClient.contentsOfDirectory(url)
|
||||
}
|
||||
|
||||
if fileClient.isReadable(url) {
|
||||
logger.debug("Not directory, but readable.")
|
||||
return [url]
|
||||
}
|
||||
}
|
||||
|
||||
// We could not find configuration in any usual places.
|
||||
throw ConfigurationError.configurationNotFound
|
||||
}
|
||||
|
||||
func path(for url: URL) -> String {
|
||||
url.absoluteString.replacing("file://", with: "")
|
||||
}
|
||||
|
||||
enum ConfigurationError: Error {
|
||||
case configurationNotFound
|
||||
}
|
||||
Reference in New Issue
Block a user