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

63 lines
1.6 KiB
Swift

import ArgumentParser
import CliClient
import Dependencies
struct GenerateProjectTemplateCommand: AsyncParsableCommand {
static let commandName = "generate-template"
static let configuration = CommandConfiguration.playbook(
commandName: commandName,
abstract: "Generate a home performance assesment project template.",
parentCommand: UtilsCommand.commandName,
examples: (label: "Generate Template", example: "\(commandName) /path/to/project-template")
)
@OptionGroup var globals: GlobalOptions
@Option(
name: .shortAndLong,
help: "Customize the directory where template variables are stored."
)
var templateVars: String?
@Flag(
name: .long,
help: "Do not generate ansible-vault variables file."
)
var noVault: Bool = false
@Argument(
help: "Path to the project template directory.",
completion: .directory
)
var path: String
@Argument(
help: "Extra arguments / options passed to the playbook."
)
var extraOptions: [String] = []
mutating func run() async throws {
@Dependency(\.cliClient) var cliClient
var arguments = [
"--tags", "repo-template",
"--extra-vars", "output_dir=\(path)"
]
if let varsDir = templateVars {
arguments.append(contentsOf: ["--extra-vars", "repo_vars_dir=\(varsDir)"])
}
if noVault {
arguments.append(contentsOf: ["--extra-vars", "use_vault=false"])
}
try await cliClient.runPlaybookCommand(
globals.playbookOptions(arguments: arguments, configuration: nil),
logging: globals.loggingOptions(commandName: Self.commandName)
)
}
}