feat: Working on moving commands to cli-client
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import Dependencies
|
||||
import Logging
|
||||
|
||||
// TODO: Move some of this to the cli-client module.
|
||||
extension Logger.Level {
|
||||
|
||||
/// Set the log level based on the user's options supplied.
|
||||
|
||||
19
Sources/hpa/Internal/PlaybookOptions+Globals.swift
Normal file
19
Sources/hpa/Internal/PlaybookOptions+Globals.swift
Normal file
@@ -0,0 +1,19 @@
|
||||
import CliClient
|
||||
import ConfigurationClient
|
||||
|
||||
extension GlobalOptions {
|
||||
|
||||
func playbookOptions(
|
||||
arguments: [String],
|
||||
configuration: Configuration?
|
||||
) -> CliClient.PlaybookOptions {
|
||||
.init(
|
||||
arguments: arguments,
|
||||
configuration: configuration,
|
||||
inventoryFilePath: inventoryPath,
|
||||
playbookDirectory: playbookDir,
|
||||
quiet: quietOnlyPlaybook ? true : basic.quiet,
|
||||
shell: basic.shell
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
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
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
import ConfigurationClient
|
||||
import Dependencies
|
||||
import FileClient
|
||||
import ShellClient
|
||||
|
||||
func runVault(
|
||||
commandName: String,
|
||||
options: VaultOptions,
|
||||
_ args: [String]
|
||||
) async throws {
|
||||
try await withSetupLogger(commandName: commandName, globals: options.globals) {
|
||||
@Dependency(\.cliClient) var cliClient
|
||||
@Dependency(\.configurationClient) var configurationClient
|
||||
@Dependency(\.fileClient) var fileClient
|
||||
@Dependency(\.logger) var logger
|
||||
|
||||
logger.debug("Begin run vault: \(options)")
|
||||
|
||||
let configuration = try await configurationClient.findAndLoad()
|
||||
logger.debug("Configuration: \(configuration)")
|
||||
|
||||
let path: String
|
||||
if let file = options.file {
|
||||
path = file
|
||||
} else {
|
||||
guard let url = try await fileClient.findVaultFileInCurrentDirectory() else {
|
||||
throw VaultFileNotFound()
|
||||
}
|
||||
path = url.cleanFilePath
|
||||
}
|
||||
|
||||
logger.debug("Vault path: \(path)")
|
||||
|
||||
let defaultArgs = configuration.vault.args ?? []
|
||||
|
||||
var vaultArgs = ["ansible-vault"]
|
||||
+ args
|
||||
+ defaultArgs
|
||||
+ options.extraArgs
|
||||
+ [path]
|
||||
|
||||
if args.contains("encrypt"),
|
||||
!vaultArgs.contains("--encrypt-vault-id"),
|
||||
let id = configuration.vault.encryptId
|
||||
{
|
||||
vaultArgs.append(contentsOf: ["--encrypt-vault-id", id])
|
||||
}
|
||||
|
||||
try await cliClient.runCommand(
|
||||
quiet: options.quiet,
|
||||
shell: options.shellOrDefault,
|
||||
vaultArgs
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct VaultFileNotFound: Error {}
|
||||
18
Sources/hpa/Internal/VaultOptions+Globals.swift
Normal file
18
Sources/hpa/Internal/VaultOptions+Globals.swift
Normal file
@@ -0,0 +1,18 @@
|
||||
import CliClient
|
||||
import ConfigurationClient
|
||||
|
||||
extension VaultOptions {
|
||||
|
||||
func vaultOptions(
|
||||
arguments: [String],
|
||||
configuration: Configuration?
|
||||
) -> CliClient.VaultOptions {
|
||||
.init(
|
||||
arguments: arguments,
|
||||
configuration: configuration,
|
||||
quiet: globals.quiet,
|
||||
shell: globals.shell,
|
||||
vaultFilePath: file
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user