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

117 lines
2.6 KiB
Swift

import ArgumentParser
import CliClient
import ConfigurationClient
import PlaybookClient
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?
}
@dynamicMemberLookup
struct GlobalOptions: ParsableArguments {
@Option(
name: .long,
help: "Optional path to the ansible hpa playbook directory."
)
var playbookDirectory: String?
@Option(
name: .shortAndLong,
help: "Optional path to the ansible inventory to use."
)
var inventoryPath: String?
@Flag(
name: .long,
help: "Supress only playbook logging."
)
var quietOnlyPlaybook = false
@OptionGroup var basic: BasicGlobalOptions
subscript<T>(dynamicMember keyPath: WritableKeyPath<BasicGlobalOptions, T>) -> T {
get { basic[keyPath: keyPath] }
set { basic[keyPath: keyPath] = newValue }
}
subscript<T>(dynamicMember keyPath: KeyPath<BasicGlobalOptions, T>) -> T {
basic[keyPath: keyPath]
}
}
// TODO: Update these to use CommandClient.LoggingOptions
extension GlobalOptions {
func loggingOptions(commandName: String) -> CliClient.LoggingOptions {
.init(
commandName: commandName,
logLevel: .init(globals: basic, quietOnlyPlaybook: quietOnlyPlaybook)
)
}
}
extension BasicGlobalOptions {
func loggingOptions(commandName: String) -> CliClient.LoggingOptions {
.init(
commandName: commandName,
logLevel: .init(globals: self, quietOnlyPlaybook: false)
)
}
}
// TODO: Remove
extension GlobalOptions {
func playbookOptions(
arguments: [String],
configuration: Configuration?
) -> CliClient.PlaybookOptions {
.init(
arguments: arguments,
configuration: configuration,
inventoryFilePath: inventoryPath,
playbookDirectory: playbookDirectory,
quiet: quietOnlyPlaybook ? true : basic.quiet,
shell: basic.shell
)
}
}
extension GlobalOptions {
func sharedPlaybookRunOptions(
commandName: String,
extraOptions: [String]?
) -> PlaybookClient.RunPlaybook.SharedRunOptions {
return .init(
extraOptions: extraOptions,
inventoryFilePath: inventoryPath,
loggingOptions: .init(
commandName: commandName,
logLevel: .init(globals: basic, quietOnlyPlaybook: quietOnlyPlaybook)
),
quiet: basic.quiet,
shell: basic.shell
)
}
}