feat: Adds generate-config command.

This commit is contained in:
2024-11-29 20:32:32 -05:00
parent 84b002c997
commit 505f7d8013
8 changed files with 143 additions and 30 deletions

View File

@@ -9,7 +9,10 @@ struct Application: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "hpa",
abstract: "A utility for working with ansible hpa playbook.",
subcommands: [BuildCommand.self, CreateProjectCommand.self, CreateProjectTemplateCommand.self]
subcommands: [
BuildCommand.self, CreateCommand.self, CreateProjectTemplateCommand.self,
GenerateConfigurationCommand.self
]
)
}

View File

@@ -4,7 +4,7 @@ import Dependencies
import Foundation
import Logging
struct CreateProjectCommand: AsyncParsableCommand {
struct CreateCommand: AsyncParsableCommand {
static let commandName = "create"
@@ -97,7 +97,7 @@ struct CreateProjectCommand: AsyncParsableCommand {
}
private func parseOptions(
command: CreateProjectCommand,
command: CreateCommand,
configuration: Configuration,
logger: Logger,
encoder: JSONEncoder

View File

@@ -0,0 +1,54 @@
import ArgumentParser
import CliClient
import Dependencies
struct GenerateConfigurationCommand: AsyncParsableCommand {
static let commandName = "generate-config"
static let configuration = CommandConfiguration(
commandName: commandName,
abstract: "\("Generate a local configuration file.".blue)",
discussion: """
If a directory is not supplied then a configuration file will be created
at \("'~/.config/hpa-playbook/config'".yellow).
"""
)
@Option(
name: .shortAndLong,
help: "Directory to generate the configuration in.",
completion: .directory
)
var path: String?
@OptionGroup var globals: BasicGlobalOptions
mutating func run() async throws {
try await _run()
}
private func _run() async throws {
try await withSetupLogger(commandName: Self.commandName, globals: globals) {
@Dependency(\.cliClient) var cliClient
let actualPath: String
if let path {
actualPath = "\(path)/config"
} else {
let path = "~/.config/hpa-playbook/"
try await cliClient.runCommand(
quiet: false,
shell: globals.shellOrDefault,
"mkdir", "-p", path
)
actualPath = "\(path)/config"
}
try cliClient.createConfiguration(actualPath)
}
}
}

View File

@@ -1,5 +1,27 @@
import ArgumentParser
struct BasicGlobalOptions: ParsableArguments {
@Flag(
name: .shortAndLong,
help: "Increase logging level (can be passed multiple times)."
)
var verbose: Int
@Flag(
name: .shortAndLong,
help: "Supress logging."
)
var quiet = false
@Option(
name: .shortAndLong,
help: "Optional shell to use when calling shell commands.",
completion: .file()
)
var shell: String?
}
struct GlobalOptions: ParsableArguments {
@Option(
@@ -14,29 +36,12 @@ struct GlobalOptions: ParsableArguments {
)
var inventoryPath: String?
@Flag(
name: .shortAndLong,
help: "Increase logging level (can be passed multiple times)."
)
var verbose: Int
@Flag(
name: .shortAndLong,
help: "Supress logging."
)
var quiet = false
@Flag(
name: .long,
help: "Supress only playbook logging."
)
var quietOnlyPlaybook = false
@Option(
name: .shortAndLong,
help: "Optional shell to use when calling shell commands.",
completion: .file()
)
var shell: String?
@OptionGroup var basic: BasicGlobalOptions
}

View File

@@ -40,7 +40,7 @@ extension CommandConfiguration {
}
}
extension GlobalOptions {
extension BasicGlobalOptions {
var shellOrDefault: ShellCommand.Shell {
guard let shell else { return .zsh(useDashC: true) }
@@ -48,6 +48,11 @@ extension GlobalOptions {
}
}
extension GlobalOptions {
var shellOrDefault: ShellCommand.Shell { basic.shellOrDefault }
}
func ensureString(
globals: GlobalOptions,
configuration: Configuration,
@@ -65,13 +70,14 @@ func ensureString(
func withSetupLogger(
commandName: String,
globals: GlobalOptions,
globals: BasicGlobalOptions,
quietOnlyPlaybook: Bool = false,
dependencies setupDependencies: (inout DependencyValues) -> Void = { _ in },
operation: @escaping () async throws -> Void
) async rethrows {
try await withDependencies {
$0.logger = .init(label: "\("hpa".yellow)")
if globals.quietOnlyPlaybook || !globals.quiet {
if quietOnlyPlaybook || !globals.quiet {
switch globals.verbose {
case 0:
$0.logger.logLevel = .info
@@ -89,6 +95,21 @@ func withSetupLogger(
}
}
func withSetupLogger(
commandName: String,
globals: GlobalOptions,
dependencies setupDependencies: (inout DependencyValues) -> Void = { _ in },
operation: @escaping () async throws -> Void
) async rethrows {
try await withSetupLogger(
commandName: commandName,
globals: globals.basic,
quietOnlyPlaybook: globals.quietOnlyPlaybook,
dependencies: setupDependencies,
operation: operation
)
}
func runPlaybook(
commandName: String,
globals: GlobalOptions,
@@ -156,7 +177,7 @@ func runPlaybook(
}
try await cliClient.runCommand(
quiet: globals.quietOnlyPlaybook ? true : globals.quiet,
quiet: globals.quietOnlyPlaybook ? true : globals.basic.quiet,
shell: globals.shellOrDefault,
playbookArgs + args + extraArgs
)