Files
swift-hpa/Sources/hpa/Internal/RunPlaybook.swift

102 lines
2.6 KiB
Swift

import ArgumentParser
import CliClient
import ConfigurationClient
import Dependencies
import Foundation
import Logging
import Rainbow
import ShellClient
func runPlaybook(
commandName: String,
globals: GlobalOptions,
configuration: Configuration? = nil,
extraArgs: [String],
_ args: [String]
) async throws {
try await withSetupLogger(commandName: commandName, globals: globals) {
@Dependency(\.cliClient) var cliClient
@Dependency(\.configurationClient) var configurationClient
@Dependency(\.logger) var logger
logger.debug("Begin run playbook: \(globals)")
let configuration = try await configurationClient.findAndLoad()
logger.debug("Configuration: \(configuration)")
let playbookDir = try ensureString(
globals: globals,
configuration: configuration,
globalsKeyPath: \.playbookDir,
configurationKeyPath: \.playbook?.directory
)
let playbook = "\(playbookDir)/\(Constants.playbookFileName)"
let inventory = (try? ensureString(
globals: globals,
configuration: configuration,
globalsKeyPath: \.inventoryPath,
configurationKeyPath: \.playbook?.inventory
)) ?? "\(playbookDir)/\(Constants.inventoryFileName)"
let defaultArgs = (configuration.args ?? [])
+ (configuration.useVaultArgs ? configuration.vault.args ?? [] : [])
try await cliClient.runCommand(
quiet: globals.quietOnlyPlaybook ? true : globals.quiet,
shell: globals.shellOrDefault,
[
"ansible-playbook", playbook,
"--inventory", inventory
] + args
+ extraArgs
+ defaultArgs
)
}
}
func runPlaybook(
commandName: String,
globals: GlobalOptions,
configuration: Configuration? = nil,
extraArgs: [String],
_ args: String...
) async throws {
try await runPlaybook(
commandName: commandName,
globals: globals,
configuration: configuration,
extraArgs: extraArgs,
args
)
}
extension BasicGlobalOptions {
var shellOrDefault: ShellCommand.Shell {
guard let shell else { return .zsh(useDashC: true) }
return .custom(path: shell, useDashC: true)
}
}
func ensureString(
globals: GlobalOptions,
configuration: Configuration,
globalsKeyPath: KeyPath<GlobalOptions, String?>,
configurationKeyPath: KeyPath<Configuration, String?>
) throws -> String {
if let global = globals[keyPath: globalsKeyPath] {
return global
}
guard let configuration = configuration[keyPath: configurationKeyPath] else {
throw RunPlaybookError.playbookNotFound
}
return configuration
}
enum RunPlaybookError: Error {
case playbookNotFound
case configurationError
}