feat: Fixes not creating default config directory
This commit is contained in:
@@ -117,12 +117,31 @@ struct LiveConfigurationClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func generate(at file: File, force: Bool) async throws {
|
func generate(at file: File, force: Bool) async throws {
|
||||||
|
@Dependency(\.logger) var logger
|
||||||
|
|
||||||
|
logger.debug("Begin generating configuration: \(file.path), force: \(force)")
|
||||||
|
|
||||||
|
let expandedPath = file.path.replacingOccurrences(
|
||||||
|
of: "~",
|
||||||
|
with: fileManager.homeDirectory().cleanFilePath
|
||||||
|
)
|
||||||
|
|
||||||
|
let fileUrl = URL(filePath: expandedPath)
|
||||||
|
|
||||||
if !force {
|
if !force {
|
||||||
guard !fileManager.fileExists(file.url) else {
|
guard !fileManager.fileExists(fileUrl) else {
|
||||||
throw ConfigurationError.fileExists(path: file.path)
|
throw ConfigurationError.fileExists(path: file.path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let fileDirectory = fileUrl.deletingLastPathComponent()
|
||||||
|
let directoryExists = try await fileManager.isDirectory(fileDirectory)
|
||||||
|
|
||||||
|
if !directoryExists {
|
||||||
|
logger.debug("Creating directory at: \(fileDirectory.cleanFilePath)")
|
||||||
|
try await fileManager.createDirectory(fileDirectory)
|
||||||
|
}
|
||||||
|
|
||||||
if case .toml = file {
|
if case .toml = file {
|
||||||
// In the case of toml, we copy the internal resource that includes
|
// In the case of toml, we copy the internal resource that includes
|
||||||
// usage comments in the file.
|
// usage comments in the file.
|
||||||
@@ -133,11 +152,11 @@ struct LiveConfigurationClient {
|
|||||||
throw ConfigurationError.resourceNotFound
|
throw ConfigurationError.resourceNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
try await fileManager.copy(resourceFile, file.url)
|
try await fileManager.copy(resourceFile, fileUrl)
|
||||||
} else {
|
} else {
|
||||||
// Json does not allow comments, so we write the mock configuration
|
// Json does not allow comments, so we write the mock configuration
|
||||||
// to the file path.
|
// to the file path.
|
||||||
try await write(.mock, to: file, force: force)
|
try await write(.mock, to: File(fileUrl)!, force: force)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,13 @@ public enum File: Equatable, Sendable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static var `default`: Self {
|
public static var `default`: Self {
|
||||||
.toml("~/.config/\(HPAKey.configDirName)/\(HPAKey.defaultFileName)")
|
let fileUrl = FileManager.default
|
||||||
|
.homeDirectoryForCurrentUser
|
||||||
|
.appending(path: ".config")
|
||||||
|
.appending(path: HPAKey.configDirName)
|
||||||
|
.appending(path: HPAKey.defaultFileName)
|
||||||
|
|
||||||
|
return .toml(fileUrl)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,11 @@ public extension DependencyValues {
|
|||||||
@DependencyClient
|
@DependencyClient
|
||||||
public struct FileClient: Sendable {
|
public struct FileClient: Sendable {
|
||||||
public var copy: @Sendable (URL, URL) async throws -> Void
|
public var copy: @Sendable (URL, URL) async throws -> Void
|
||||||
|
public var createDirectory: @Sendable (URL) async throws -> Void
|
||||||
public var fileExists: @Sendable (URL) -> Bool = { _ in true }
|
public var fileExists: @Sendable (URL) -> Bool = { _ in true }
|
||||||
public var findVaultFileInCurrentDirectory: @Sendable () async throws -> URL?
|
public var findVaultFileInCurrentDirectory: @Sendable () async throws -> URL?
|
||||||
public var homeDirectory: @Sendable () -> URL = { URL(filePath: "~/") }
|
public var homeDirectory: @Sendable () -> URL = { URL(filePath: "~/") }
|
||||||
|
public var isDirectory: @Sendable (URL) async throws -> Bool
|
||||||
public var load: @Sendable (URL) async throws -> Data
|
public var load: @Sendable (URL) async throws -> Data
|
||||||
public var write: @Sendable (Data, URL) async throws -> Void
|
public var write: @Sendable (Data, URL) async throws -> Void
|
||||||
}
|
}
|
||||||
@@ -26,12 +28,16 @@ extension FileClient: DependencyKey {
|
|||||||
let manager = LiveFileClient()
|
let manager = LiveFileClient()
|
||||||
return .init {
|
return .init {
|
||||||
try await manager.copy($0, to: $1)
|
try await manager.copy($0, to: $1)
|
||||||
|
} createDirectory: {
|
||||||
|
try await manager.creatDirectory($0)
|
||||||
} fileExists: { url in
|
} fileExists: { url in
|
||||||
manager.fileExists(at: url)
|
manager.fileExists(at: url)
|
||||||
} findVaultFileInCurrentDirectory: {
|
} findVaultFileInCurrentDirectory: {
|
||||||
try await manager.findVaultFileInCurrentDirectory()
|
try await manager.findVaultFileInCurrentDirectory()
|
||||||
} homeDirectory: {
|
} homeDirectory: {
|
||||||
manager.homeDirectory()
|
manager.homeDirectory()
|
||||||
|
} isDirectory: {
|
||||||
|
manager.isDirectory($0)
|
||||||
} load: { url in
|
} load: { url in
|
||||||
try await manager.load(from: url)
|
try await manager.load(from: url)
|
||||||
} write: { data, url in
|
} write: { data, url in
|
||||||
@@ -48,6 +54,10 @@ struct LiveFileClient: Sendable {
|
|||||||
try manager.copyItem(at: url, to: toUrl)
|
try manager.copyItem(at: url, to: toUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func creatDirectory(_ url: URL) async throws {
|
||||||
|
try manager.createDirectory(at: url, withIntermediateDirectories: true)
|
||||||
|
}
|
||||||
|
|
||||||
func fileExists(at url: URL) -> Bool {
|
func fileExists(at url: URL) -> Bool {
|
||||||
manager.fileExists(atPath: url.cleanFilePath)
|
manager.fileExists(atPath: url.cleanFilePath)
|
||||||
}
|
}
|
||||||
@@ -78,7 +88,7 @@ struct LiveFileClient: Sendable {
|
|||||||
manager.homeDirectoryForCurrentUser
|
manager.homeDirectoryForCurrentUser
|
||||||
}
|
}
|
||||||
|
|
||||||
private func isDirectory(_ url: URL) -> Bool {
|
func isDirectory(_ url: URL) -> Bool {
|
||||||
var isDirectory: ObjCBool = false
|
var isDirectory: ObjCBool = false
|
||||||
manager.fileExists(atPath: url.cleanFilePath, isDirectory: &isDirectory)
|
manager.fileExists(atPath: url.cleanFilePath, isDirectory: &isDirectory)
|
||||||
return isDirectory.boolValue
|
return isDirectory.boolValue
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import ArgumentParser
|
import ArgumentParser
|
||||||
|
import CliClient
|
||||||
|
import Dependencies
|
||||||
|
|
||||||
struct GenerateProjectTemplateCommand: AsyncParsableCommand {
|
struct GenerateProjectTemplateCommand: AsyncParsableCommand {
|
||||||
|
|
||||||
@@ -36,23 +38,25 @@ struct GenerateProjectTemplateCommand: AsyncParsableCommand {
|
|||||||
)
|
)
|
||||||
var extraArgs: [String] = []
|
var extraArgs: [String] = []
|
||||||
|
|
||||||
// FIX:
|
|
||||||
mutating func run() async throws {
|
mutating func run() async throws {
|
||||||
let varsDir = templateVars != nil
|
@Dependency(\.cliClient) var cliClient
|
||||||
? ["--extra-vars", "repo_vars_dir=\(templateVars!)"]
|
|
||||||
: []
|
|
||||||
|
|
||||||
let vault = noVault ? ["--extra-vars", "use_vault=false"] : []
|
var arguments = [
|
||||||
|
"--tags", "repo-template",
|
||||||
|
"--extra-vars", "output_dir=\(path)"
|
||||||
|
]
|
||||||
|
|
||||||
// try await runPlaybook(
|
if let varsDir = templateVars {
|
||||||
// commandName: Self.commandName,
|
arguments.append(contentsOf: ["--extra-vars", "repo_vars_dir=\(varsDir)"])
|
||||||
// globals: globals,
|
}
|
||||||
// extraArgs: extraArgs,
|
|
||||||
// [
|
if noVault {
|
||||||
// "--tags", "repo-template",
|
arguments.append(contentsOf: ["--extra-vars", "use_vault=false"])
|
||||||
// "--extra-vars", "output_dir=\(path)"
|
}
|
||||||
// ] + varsDir
|
|
||||||
// + vault
|
try await cliClient.runPlaybookCommand(
|
||||||
// )
|
globals.playbookOptions(arguments: arguments, configuration: nil),
|
||||||
|
logging: globals.loggingOptions(commandName: Self.commandName)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import ArgumentParser
|
import ArgumentParser
|
||||||
import CliClient
|
import CliClient
|
||||||
import CliDoc
|
import CliDoc
|
||||||
|
import ConfigurationClient
|
||||||
import Dependencies
|
import Dependencies
|
||||||
|
|
||||||
struct GenerateConfigurationCommand: AsyncParsableCommand {
|
struct GenerateConfigurationCommand: AsyncParsableCommand {
|
||||||
@@ -42,32 +43,39 @@ struct GenerateConfigurationCommand: AsyncParsableCommand {
|
|||||||
)
|
)
|
||||||
var json: Bool = false
|
var json: Bool = false
|
||||||
|
|
||||||
|
@Flag(
|
||||||
|
name: .shortAndLong,
|
||||||
|
help: "Force generation, overwriting a file if it exists."
|
||||||
|
)
|
||||||
|
var force: Bool = false
|
||||||
|
|
||||||
@OptionGroup var globals: BasicGlobalOptions
|
@OptionGroup var globals: BasicGlobalOptions
|
||||||
|
|
||||||
mutating func run() async throws {
|
mutating func run() async throws {
|
||||||
try await _run()
|
try await _run()
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIX:
|
|
||||||
private func _run() async throws {
|
private func _run() async throws {
|
||||||
@Dependency(\.cliClient) var cliClient
|
@Dependency(\.cliClient) var cliClient
|
||||||
|
@Dependency(\.configurationClient) var configurationClient
|
||||||
|
|
||||||
try await cliClient.withLogger(globals.loggingOptions(commandName: Self.commandName)) {
|
try await cliClient.withLogger(globals.loggingOptions(commandName: Self.commandName)) {
|
||||||
let actualPath: String
|
@Dependency(\.logger) var logger
|
||||||
|
|
||||||
// if let path {
|
let actualPath: File
|
||||||
// actualPath = "\(path)/config"
|
|
||||||
// } else {
|
|
||||||
// let path = "~/.config/hpa-playbook/"
|
|
||||||
// try await cliClient.runCommand(
|
|
||||||
// quiet: false,
|
|
||||||
// shell: globals.shellOrDefault,
|
|
||||||
// "mkdir", "-p", path
|
|
||||||
// )
|
|
||||||
// actualPath = "\(path)/config"
|
|
||||||
// }
|
|
||||||
|
|
||||||
fatalError()
|
if let path, let file = File("\(path)/config.\(json ? "json" : "toml")") {
|
||||||
// try cliClient.createConfiguration(actualPath, json)
|
actualPath = file
|
||||||
|
} else {
|
||||||
|
actualPath = .default
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug("Generating config at path: \(actualPath.path)")
|
||||||
|
|
||||||
|
try await configurationClient.generate(
|
||||||
|
at: actualPath,
|
||||||
|
force: force
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user