feat: Adds generate-config as json file.

This commit is contained in:
2024-11-29 22:25:50 -05:00
parent 505f7d8013
commit ed3f752694
6 changed files with 97 additions and 36 deletions

View File

@@ -5,7 +5,7 @@ import ShellClient
@_spi(Internal)
public func findConfigurationFiles(
env: [String: String] = ProcessInfo.processInfo.environment
) throws -> [URL] {
) throws -> URL {
@Dependency(\.logger) var logger
@Dependency(\.fileClient) var fileClient
@@ -13,50 +13,47 @@ public func findConfigurationFiles(
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]
}
// Check for environment variable pointing to a directory that the
// the configuration lives.
if let configHome = env["HPA_CONFIG_HOME"] {
url = .init(filePath: configHome)
let url = URL(filePath: configHome).appending(path: "config")
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]
return url
}
}
// Check home directory for a `.hparc` file.
let url = homeDir.appending(path: ".hparc")
if fileClient.isReadable(url) {
logger.debug("Found configuration in home directory")
return url
}
// Check for environment variable pointing to a directory that the
// the configuration lives.
if let pwd = env["PWD"] {
url = .init(filePath: "\(pwd)").appending(path: ".hparc")
let url = URL(filePath: "\(pwd)").appending(path: ".hparc")
if fileClient.isReadable(url) {
logger.debug("Found configuration in current working directory.")
return [url]
return url
}
}
// Check in xdg config home, under an hpa-playbook directory.
if let xdgConfigHome = env["XDG_CONFIG_HOME"] {
logger.debug("XDG Config Home: \(xdgConfigHome)")
url = .init(filePath: "\(xdgConfigHome)")
let url = URL(filePath: "\(xdgConfigHome)")
.appending(path: "hpa-playbook")
.appending(path: "config")
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]
return url
}
}