Files
swift-hpa/Sources/hpa/UtilsCommands/GenerateConfigCommand.swift

82 lines
2.0 KiB
Swift

import ArgumentParser
import CliClient
import CliDoc
import ConfigurationClient
import Dependencies
struct GenerateConfigurationCommand: AsyncParsableCommand {
static let commandName = "generate-config"
static let configuration = CommandConfiguration(
commandName: commandName,
abstract: createAbstract("Generate a local configuration file."),
discussion: Discussion {
VStack {
Note {
"""
If a directory is not supplied then a configuration file will be created
at \("'~/.config/hpa-playbook/config'".yellow).
"""
}
VStack {
"EXAMPLE:".yellow.bold
"Create a directory and generate the configuration".green
ShellCommand("mkdir -p ~/.config/hpa-playbook")
ShellCommand("hpa generate-config --path ~/.config/hpa-playbook")
}
}
.separator(.newLine(count: 2))
}
)
@Option(
name: .shortAndLong,
help: "Directory to generate the configuration in.",
completion: .directory
)
var path: String?
@Flag(
name: .shortAndLong,
help: "Generate a json file, instead of default env style"
)
var json: Bool = false
@Flag(
name: .shortAndLong,
help: "Force generation, overwriting a file if it exists."
)
var force: Bool = false
@OptionGroup var globals: BasicGlobalOptions
mutating func run() async throws {
try await _run()
}
private func _run() async throws {
@Dependency(\.cliClient) var cliClient
@Dependency(\.configurationClient) var configurationClient
try await cliClient.withLogger(globals.loggingOptions(commandName: Self.commandName)) {
@Dependency(\.logger) var logger
let actualPath: File
if let path, let file = File("\(path)/config.\(json ? "json" : "toml")") {
actualPath = file
} else {
actualPath = .default
}
logger.debug("Generating config at path: \(actualPath.path)")
try await configurationClient.generate(
at: actualPath,
force: force
)
}
}
}