feat: Working on moving commands to cli-client
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
enum Constants {
|
enum Constants {
|
||||||
|
static let executableName = "hpa"
|
||||||
static let playbookCommand = "ansible-playbook"
|
static let playbookCommand = "ansible-playbook"
|
||||||
static let playbookFileName = "main.yml"
|
static let playbookFileName = "main.yml"
|
||||||
static let inventoryFileName = "inventory.ini"
|
static let inventoryFileName = "inventory.ini"
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import DependenciesMacros
|
|||||||
import Foundation
|
import Foundation
|
||||||
import ShellClient
|
import ShellClient
|
||||||
|
|
||||||
|
// TODO: Add logging options and setup in this module and remove from the
|
||||||
|
// executable `hpa` module.
|
||||||
|
|
||||||
public extension DependencyValues {
|
public extension DependencyValues {
|
||||||
var cliClient: CliClient {
|
var cliClient: CliClient {
|
||||||
get { self[CliClient.self] }
|
get { self[CliClient.self] }
|
||||||
|
|||||||
@@ -26,12 +26,30 @@ struct BuildCommand: AsyncParsableCommand {
|
|||||||
var extraArgs: [String] = []
|
var extraArgs: [String] = []
|
||||||
|
|
||||||
mutating func run() async throws {
|
mutating func run() async throws {
|
||||||
try await runPlaybook(
|
try await _run()
|
||||||
commandName: Self.commandName,
|
|
||||||
globals: globals,
|
// try await runPlaybook(
|
||||||
extraArgs: extraArgs,
|
// commandName: Self.commandName,
|
||||||
|
// globals: globals,
|
||||||
|
// extraArgs: extraArgs,
|
||||||
|
// "--tags", "build-project",
|
||||||
|
// "--extra-vars", "project_dir=\(projectDir)"
|
||||||
|
// )
|
||||||
|
}
|
||||||
|
|
||||||
|
private func _run() async throws {
|
||||||
|
try await withSetupLogger(commandName: Self.commandName, globals: globals) {
|
||||||
|
@Dependency(\.cliClient) var cliClient
|
||||||
|
|
||||||
|
try await cliClient.runPlaybookCommand(
|
||||||
|
globals.playbookOptions(
|
||||||
|
arguments: [
|
||||||
"--tags", "build-project",
|
"--tags", "build-project",
|
||||||
"--extra-vars", "project_dir=\(projectDir)"
|
"--extra-vars", "project_dir=\(self.projectDir)"
|
||||||
|
],
|
||||||
|
configuration: nil
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,15 +86,28 @@ struct CreateCommand: AsyncParsableCommand {
|
|||||||
|
|
||||||
logger.debug("JSON string: \(jsonString)")
|
logger.debug("JSON string: \(jsonString)")
|
||||||
|
|
||||||
try await runPlaybook(
|
let arguments = [
|
||||||
commandName: Self.commandName,
|
|
||||||
globals: self.globals,
|
|
||||||
configuration: configuration,
|
|
||||||
extraArgs: extraArgs,
|
|
||||||
"--tags", "setup-project",
|
"--tags", "setup-project",
|
||||||
"--extra-vars", "project_dir=\(self.projectDir)",
|
"--extra-vars", "project_dir=\(self.projectDir)",
|
||||||
"--extra-vars", "'\(jsonString)'"
|
"--extra-vars", "'\(jsonString)'"
|
||||||
|
] + extraArgs
|
||||||
|
|
||||||
|
try await cliClient.runPlaybookCommand(
|
||||||
|
globals.playbookOptions(
|
||||||
|
arguments: arguments,
|
||||||
|
configuration: configuration
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
// try await runPlaybook(
|
||||||
|
// commandName: Self.commandName,
|
||||||
|
// globals: self.globals,
|
||||||
|
// configuration: configuration,
|
||||||
|
// extraArgs: extraArgs,
|
||||||
|
// "--tags", "setup-project",
|
||||||
|
// "--extra-vars", "project_dir=\(self.projectDir)",
|
||||||
|
// "--extra-vars", "'\(jsonString)'"
|
||||||
|
// )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import Dependencies
|
import Dependencies
|
||||||
import Logging
|
import Logging
|
||||||
|
|
||||||
|
// TODO: Move some of this to the cli-client module.
|
||||||
extension Logger.Level {
|
extension Logger.Level {
|
||||||
|
|
||||||
/// Set the log level based on the user's options supplied.
|
/// 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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -36,6 +36,7 @@ struct GenerateProjectTemplateCommand: AsyncParsableCommand {
|
|||||||
)
|
)
|
||||||
var extraArgs: [String] = []
|
var extraArgs: [String] = []
|
||||||
|
|
||||||
|
// FIX:
|
||||||
mutating func run() async throws {
|
mutating func run() async throws {
|
||||||
let varsDir = templateVars != nil
|
let varsDir = templateVars != nil
|
||||||
? ["--extra-vars", "repo_vars_dir=\(templateVars!)"]
|
? ["--extra-vars", "repo_vars_dir=\(templateVars!)"]
|
||||||
@@ -43,15 +44,15 @@ struct GenerateProjectTemplateCommand: AsyncParsableCommand {
|
|||||||
|
|
||||||
let vault = noVault ? ["--extra-vars", "use_vault=false"] : []
|
let vault = noVault ? ["--extra-vars", "use_vault=false"] : []
|
||||||
|
|
||||||
try await runPlaybook(
|
// try await runPlaybook(
|
||||||
commandName: Self.commandName,
|
// commandName: Self.commandName,
|
||||||
globals: globals,
|
// globals: globals,
|
||||||
extraArgs: extraArgs,
|
// extraArgs: extraArgs,
|
||||||
[
|
// [
|
||||||
"--tags", "repo-template",
|
// "--tags", "repo-template",
|
||||||
"--extra-vars", "output_dir=\(path)"
|
// "--extra-vars", "output_dir=\(path)"
|
||||||
] + varsDir
|
// ] + varsDir
|
||||||
+ vault
|
// + vault
|
||||||
)
|
// )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,17 +55,17 @@ struct GenerateConfigurationCommand: AsyncParsableCommand {
|
|||||||
|
|
||||||
let actualPath: String
|
let actualPath: String
|
||||||
|
|
||||||
if let path {
|
// if let path {
|
||||||
actualPath = "\(path)/config"
|
// actualPath = "\(path)/config"
|
||||||
} else {
|
// } else {
|
||||||
let path = "~/.config/hpa-playbook/"
|
// let path = "~/.config/hpa-playbook/"
|
||||||
try await cliClient.runCommand(
|
// try await cliClient.runCommand(
|
||||||
quiet: false,
|
// quiet: false,
|
||||||
shell: globals.shellOrDefault,
|
// shell: globals.shellOrDefault,
|
||||||
"mkdir", "-p", path
|
// "mkdir", "-p", path
|
||||||
)
|
// )
|
||||||
actualPath = "\(path)/config"
|
// actualPath = "\(path)/config"
|
||||||
}
|
// }
|
||||||
|
|
||||||
fatalError()
|
fatalError()
|
||||||
// try cliClient.createConfiguration(actualPath, json)
|
// try cliClient.createConfiguration(actualPath, json)
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import ArgumentParser
|
import ArgumentParser
|
||||||
|
import CliClient
|
||||||
|
import Dependencies
|
||||||
|
|
||||||
struct DecryptCommand: AsyncParsableCommand {
|
struct DecryptCommand: AsyncParsableCommand {
|
||||||
|
|
||||||
@@ -17,15 +19,23 @@ struct DecryptCommand: AsyncParsableCommand {
|
|||||||
)
|
)
|
||||||
var output: String?
|
var output: String?
|
||||||
|
|
||||||
|
// FIX:
|
||||||
mutating func run() async throws {
|
mutating func run() async throws {
|
||||||
|
@Dependency(\.cliClient) var cliClient
|
||||||
|
|
||||||
var args = ["decrypt"]
|
var args = ["decrypt"]
|
||||||
if let output {
|
if let output {
|
||||||
args.append(contentsOf: ["--output", output])
|
args.append(contentsOf: ["--output", output])
|
||||||
}
|
}
|
||||||
try await runVault(
|
|
||||||
commandName: Self.commandName,
|
try await cliClient.runVaultCommand(
|
||||||
options: options,
|
options.vaultOptions(arguments: args, configuration: nil)
|
||||||
args
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// try await runVault(
|
||||||
|
// commandName: Self.commandName,
|
||||||
|
// options: options,
|
||||||
|
// args
|
||||||
|
// )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import ArgumentParser
|
import ArgumentParser
|
||||||
|
import CliClient
|
||||||
|
import Dependencies
|
||||||
|
|
||||||
struct EncryptCommand: AsyncParsableCommand {
|
struct EncryptCommand: AsyncParsableCommand {
|
||||||
|
|
||||||
@@ -17,15 +19,22 @@ struct EncryptCommand: AsyncParsableCommand {
|
|||||||
)
|
)
|
||||||
var output: String?
|
var output: String?
|
||||||
|
|
||||||
|
// FIX:
|
||||||
mutating func run() async throws {
|
mutating func run() async throws {
|
||||||
|
@Dependency(\.cliClient) var cliClient
|
||||||
|
|
||||||
var args = ["encrypt"]
|
var args = ["encrypt"]
|
||||||
if let output {
|
if let output {
|
||||||
args.append(contentsOf: ["--output", output])
|
args.append(contentsOf: ["--output", output])
|
||||||
}
|
}
|
||||||
try await runVault(
|
try await cliClient.runVaultCommand(
|
||||||
commandName: Self.commandName,
|
options.vaultOptions(arguments: args, configuration: nil)
|
||||||
options: options,
|
|
||||||
args
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// try await runVault(
|
||||||
|
// commandName: Self.commandName,
|
||||||
|
// options: options,
|
||||||
|
// args
|
||||||
|
// )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user