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

58 lines
1.4 KiB
Swift

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 {}