feat: Reorganizes files
This commit is contained in:
@@ -12,49 +12,45 @@ public func findConfigurationFiles(
|
||||
logger.debug("Begin find configuration files.")
|
||||
logger.trace("Env: \(env)")
|
||||
|
||||
let homeDir = fileClient.homeDir()
|
||||
|
||||
// Check for environment variable pointing to a directory that the
|
||||
// the configuration lives.
|
||||
if let configHome = env["HPA_CONFIG_HOME"] {
|
||||
let url = URL(filePath: configHome).appending(path: "config")
|
||||
|
||||
if fileClient.isReadable(url) {
|
||||
logger.debug("Found configuration from hpa config home env var.")
|
||||
return url
|
||||
}
|
||||
if let pwd = env["PWD"],
|
||||
let url = fileClient.checkUrl(.file(pwd, ".hparc"))
|
||||
{
|
||||
logger.debug("Found configuration in current working directory.")
|
||||
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")
|
||||
// Check for environment variable pointing to a file that the
|
||||
// the configuration lives.
|
||||
if let configFile = env["HPA_CONFIG_FILE"],
|
||||
let url = fileClient.checkUrl(.file(configFile))
|
||||
{
|
||||
logger.debug("Found configuration from hpa config file env var.")
|
||||
return url
|
||||
}
|
||||
|
||||
// Check for environment variable pointing to a directory that the
|
||||
// the configuration lives.
|
||||
if let pwd = env["PWD"] {
|
||||
let url = URL(filePath: "\(pwd)").appending(path: ".hparc")
|
||||
if fileClient.isReadable(url) {
|
||||
logger.debug("Found configuration in current working directory.")
|
||||
return url
|
||||
}
|
||||
if let configHome = env["HPA_CONFIG_HOME"],
|
||||
let url = fileClient.checkUrl(.directory(configHome))
|
||||
{
|
||||
logger.debug("Found configuration from hpa config home env var.")
|
||||
return url
|
||||
}
|
||||
|
||||
// Check home directory for a `.hparc` file.
|
||||
if let url = fileClient.checkUrl(.file(fileClient.homeDir().appending(path: ".hparc"))) {
|
||||
logger.debug("Found configuration in home directory")
|
||||
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)")
|
||||
|
||||
let url = URL(filePath: "\(xdgConfigHome)")
|
||||
.appending(path: "hpa-playbook")
|
||||
.appending(path: "config")
|
||||
|
||||
if let xdgConfigHome = env["XDG_CONFIG_HOME"],
|
||||
let url = fileClient.checkUrl(.directory(xdgConfigHome, "hpa-playbook"))
|
||||
{
|
||||
logger.debug("XDG Config url: \(url.absoluteString)")
|
||||
|
||||
if fileClient.isReadable(url) {
|
||||
return url
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
// We could not find configuration in any usual places.
|
||||
@@ -68,3 +64,46 @@ func path(for url: URL) -> String {
|
||||
enum ConfigurationError: Error {
|
||||
case configurationNotFound
|
||||
}
|
||||
|
||||
private extension FileClient {
|
||||
|
||||
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) -> URL? {
|
||||
switch check {
|
||||
case let .file(url):
|
||||
if isReadable(url) { return url }
|
||||
return nil
|
||||
case let .directory(url):
|
||||
return findConfigurationInDirectory(url)
|
||||
}
|
||||
}
|
||||
|
||||
func findConfigurationInDirectory(_ url: URL) -> URL? {
|
||||
for file in ["config", "config.json"] {
|
||||
let fileUrl = url.appending(path: file)
|
||||
if isReadable(fileUrl) {
|
||||
return fileUrl
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user