107 lines
2.4 KiB
Swift
107 lines
2.4 KiB
Swift
import ArgumentParser
|
|
import CliClient
|
|
import ConfigurationClient
|
|
import Dependencies
|
|
import Foundation
|
|
import Logging
|
|
|
|
struct CreateCommand: AsyncParsableCommand {
|
|
|
|
static let commandName = "create"
|
|
|
|
static let configuration = CommandConfiguration.playbook(
|
|
commandName: commandName,
|
|
abstract: "Create a home performance assesment project.",
|
|
examples: (
|
|
label: "Create Assesment",
|
|
example: "\(commandName) /new/assement/path"
|
|
)
|
|
)
|
|
|
|
@OptionGroup var globals: GlobalOptions
|
|
|
|
@Option(
|
|
name: .shortAndLong,
|
|
help: "The template repository to use."
|
|
)
|
|
var repo: String?
|
|
|
|
@Option(
|
|
name: .shortAndLong,
|
|
help: "The repo branch or version to use."
|
|
)
|
|
var branch: String?
|
|
|
|
@Option(
|
|
name: .shortAndLong,
|
|
help: "Path to local template directory to use.",
|
|
completion: .directory
|
|
)
|
|
var templateDir: String?
|
|
|
|
@Flag(
|
|
name: .shortAndLong,
|
|
help: "Force using a local template directory."
|
|
)
|
|
var localTemplateDir = false
|
|
|
|
@Argument(
|
|
help: "Path to the project directory.",
|
|
completion: .directory
|
|
)
|
|
var projectDir: String
|
|
|
|
@Argument(
|
|
help: "Extra arguments passed to the playbook."
|
|
)
|
|
var extraOptions: [String] = []
|
|
|
|
mutating func run() async throws {
|
|
try await _run()
|
|
}
|
|
|
|
private func _run() async throws {
|
|
@Dependency(\.coders) var coders
|
|
@Dependency(\.cliClient) var cliClient
|
|
@Dependency(\.configurationClient) var configurationClient
|
|
|
|
let loggingOptions = globals.loggingOptions(commandName: Self.commandName)
|
|
|
|
try await cliClient.withLogger(loggingOptions) {
|
|
@Dependency(\.logger) var logger
|
|
|
|
let json = try await cliClient.generateJSON(
|
|
generateJsonOptions,
|
|
logging: loggingOptions
|
|
)
|
|
|
|
logger.debug("JSON string: \(json)")
|
|
|
|
let arguments = [
|
|
"--tags", "setup-project",
|
|
"--extra-vars", "project_dir=\(self.projectDir)",
|
|
"--extra-vars", "'\(json)'"
|
|
] + extraOptions
|
|
|
|
try await cliClient.runPlaybookCommand(
|
|
globals.playbookOptions(
|
|
arguments: arguments,
|
|
configuration: nil
|
|
),
|
|
logging: loggingOptions
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension CreateCommand {
|
|
var generateJsonOptions: CliClient.GenerateJsonOptions {
|
|
.init(
|
|
templateDirectory: templateDir,
|
|
templateRepo: repo,
|
|
version: branch,
|
|
useLocalTemplateDirectory: localTemplateDir
|
|
)
|
|
}
|
|
}
|