feat: Adds generate-config command.
This commit is contained in:
54
Sources/hpa/GenerateConfigCommand.swift
Normal file
54
Sources/hpa/GenerateConfigCommand.swift
Normal file
@@ -0,0 +1,54 @@
|
||||
import ArgumentParser
|
||||
import CliClient
|
||||
import Dependencies
|
||||
|
||||
struct GenerateConfigurationCommand: AsyncParsableCommand {
|
||||
|
||||
static let commandName = "generate-config"
|
||||
|
||||
static let configuration = CommandConfiguration(
|
||||
commandName: commandName,
|
||||
abstract: "\("Generate a local configuration file.".blue)",
|
||||
discussion: """
|
||||
|
||||
If a directory is not supplied then a configuration file will be created
|
||||
at \("'~/.config/hpa-playbook/config'".yellow).
|
||||
|
||||
"""
|
||||
)
|
||||
|
||||
@Option(
|
||||
name: .shortAndLong,
|
||||
help: "Directory to generate the configuration in.",
|
||||
completion: .directory
|
||||
)
|
||||
var path: String?
|
||||
|
||||
@OptionGroup var globals: BasicGlobalOptions
|
||||
|
||||
mutating func run() async throws {
|
||||
try await _run()
|
||||
}
|
||||
|
||||
private func _run() async throws {
|
||||
try await withSetupLogger(commandName: Self.commandName, globals: globals) {
|
||||
@Dependency(\.cliClient) var cliClient
|
||||
|
||||
let actualPath: String
|
||||
|
||||
if let path {
|
||||
actualPath = "\(path)/config"
|
||||
} else {
|
||||
let path = "~/.config/hpa-playbook/"
|
||||
try await cliClient.runCommand(
|
||||
quiet: false,
|
||||
shell: globals.shellOrDefault,
|
||||
"mkdir", "-p", path
|
||||
)
|
||||
actualPath = "\(path)/config"
|
||||
}
|
||||
|
||||
try cliClient.createConfiguration(actualPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user