This commit is contained in:
@@ -4,66 +4,17 @@ import Rainbow
|
||||
|
||||
extension CommandConfiguration {
|
||||
|
||||
static func create(
|
||||
commandName: String,
|
||||
abstract: String,
|
||||
usesExtraArgs: Bool = true,
|
||||
discussion: Discussion
|
||||
) -> Self {
|
||||
.init(
|
||||
commandName: commandName,
|
||||
abstract: createAbstract(abstract),
|
||||
discussion: discussion.render()
|
||||
)
|
||||
}
|
||||
|
||||
static func create(
|
||||
commandName: String,
|
||||
abstract: String,
|
||||
usesExtraArgs: Bool = true,
|
||||
discussion nodes: [Node]
|
||||
) -> Self {
|
||||
.init(
|
||||
commandName: commandName,
|
||||
abstract: createAbstract(abstract),
|
||||
discussion: Discussion(nodes: nodes, usesExtraArgs: usesExtraArgs).render()
|
||||
)
|
||||
}
|
||||
|
||||
static func playbook2(
|
||||
commandName: String,
|
||||
abstract: String,
|
||||
parentCommand: String? = nil,
|
||||
examples: (label: String, example: String)...
|
||||
) -> Self {
|
||||
.init(
|
||||
commandName: commandName,
|
||||
abstract: Abstract { abstract.blue },
|
||||
usage: """
|
||||
\(Constants.appName)\(parentCommand != nil ? " \(parentCommand!)" : "") \(commandName)
|
||||
""".blue.bold.italic
|
||||
+ " [OPTIONS]".green
|
||||
+ " [ARGUMENTS]".cyan
|
||||
+ " --" + " [EXTRA-OPTIONS...]".magenta,
|
||||
discussion: CliDoc.Discussion.playbook(examples: examples)
|
||||
)
|
||||
}
|
||||
|
||||
static func playbook(
|
||||
commandName: String,
|
||||
abstract: String,
|
||||
parentCommand: String? = nil,
|
||||
examples: (label: String, example: String)...
|
||||
examples: Example...
|
||||
) -> Self {
|
||||
.create(
|
||||
.init(
|
||||
commandName: commandName,
|
||||
abstract: abstract,
|
||||
discussion: .default(
|
||||
usesExtraArgs: true,
|
||||
parentCommand: parentCommand,
|
||||
examples: examples,
|
||||
seeAlso: .seeAlso(label: "Ansible playbook options.", command: "ansible-playbook")
|
||||
)
|
||||
abstract: Abstract { abstract.blue },
|
||||
usage: Usage.default(commandName: commandName, parentCommand: parentCommand),
|
||||
discussion: Discussion.playbook(examples: examples)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -72,272 +23,39 @@ func createAbstract(_ string: String) -> String {
|
||||
"\(string.blue)"
|
||||
}
|
||||
|
||||
// TODO: Remove
|
||||
struct Discussion {
|
||||
let nodes: [Node]
|
||||
extension Usage where Content == AnyTextNode {
|
||||
|
||||
static func `default`(
|
||||
usesExtraArgs: Bool,
|
||||
commandName: String,
|
||||
parentCommand: String?,
|
||||
examples: [(label: String, example: String)],
|
||||
seeAlso: Node?
|
||||
usesArguments: Bool = true,
|
||||
usesOptions: Bool = true,
|
||||
usesExtraArguments: Bool = true
|
||||
) -> Self {
|
||||
var nodes = Array.defaultNodes + examples.nodes(parentCommand)
|
||||
if let seeAlso { nodes.append(seeAlso) }
|
||||
if usesExtraArgs && examples.count > 0 { nodes.append(.important(label: Constants.importantExtraArgsNote)) }
|
||||
return .init(
|
||||
nodes: nodes,
|
||||
usesExtraArgs: usesExtraArgs
|
||||
)
|
||||
}
|
||||
|
||||
init(usesExtraArgs: Bool = true, _ nodes: Node...) {
|
||||
self.init(nodes: nodes, usesExtraArgs: usesExtraArgs)
|
||||
}
|
||||
|
||||
init(nodes: [Node], usesExtraArgs: Bool = true) {
|
||||
var nodes = nodes
|
||||
|
||||
if let firstExampleIndex = nodes.firstIndex(where: \.isExampleNode) {
|
||||
nodes.insert(.exampleHeading, at: firstExampleIndex)
|
||||
}
|
||||
|
||||
if usesExtraArgs {
|
||||
if let lastExampleIndex = nodes.lastIndex(where: \.isExampleNode) {
|
||||
guard case let .example(.container(exampleNodes, _)) = nodes[lastExampleIndex] else {
|
||||
// this should never happen.
|
||||
print(nodes[lastExampleIndex])
|
||||
fatalError()
|
||||
.init {
|
||||
HStack {
|
||||
HStack {
|
||||
"\(Constants.appName)"
|
||||
if let parentCommand {
|
||||
parentCommand
|
||||
}
|
||||
commandName
|
||||
}
|
||||
.color(.blue).bold()
|
||||
|
||||
// Get the last element of the example container, which will be the
|
||||
// text for the example.
|
||||
guard let exampleNode = exampleNodes.last else {
|
||||
print(exampleNodes)
|
||||
fatalError()
|
||||
if usesOptions {
|
||||
"[OPTIONS]".color(.green)
|
||||
}
|
||||
if usesArguments {
|
||||
"[ARGUMENTS]".color(.cyan)
|
||||
}
|
||||
if usesExtraArguments {
|
||||
"--"
|
||||
"[EXTRA-OPTIONS]".color(.magenta)
|
||||
}
|
||||
|
||||
// replace the first element, which is the header so that we can
|
||||
// replace it with a new header.
|
||||
nodes.insert(
|
||||
.example(.exampleContainer(
|
||||
"Passing extra args.",
|
||||
"\(exampleNode.render().replacingOccurrences(of: " $ ", with: ""))"
|
||||
+ " -- --vault-id \"myId@$SCRIPTS/vault-gopass-client\""
|
||||
)),
|
||||
at: nodes.index(after: lastExampleIndex)
|
||||
)
|
||||
}
|
||||
}
|
||||
self.nodes = nodes
|
||||
}
|
||||
|
||||
func render() -> String { nodes.render() }
|
||||
}
|
||||
|
||||
enum Node {
|
||||
|
||||
struct Separator {
|
||||
|
||||
let string: String
|
||||
let count: Int
|
||||
|
||||
init(_ string: String, repeating count: Int = 1) {
|
||||
self.string = string
|
||||
self.count = count
|
||||
}
|
||||
|
||||
var value: String { string.repeating(count) }
|
||||
|
||||
static var empty: Self { .init("") }
|
||||
|
||||
static func space(_ count: Int = 1) -> Self {
|
||||
.init(" ", repeating: count)
|
||||
}
|
||||
|
||||
static func newLine(_ count: Int = 1) -> Self {
|
||||
.init("\n", repeating: count)
|
||||
}
|
||||
|
||||
static func tab(_ count: Int) -> Self {
|
||||
.init("\t", repeating: count)
|
||||
}
|
||||
}
|
||||
|
||||
/// A container that holds onto other nodes to be rendered.
|
||||
indirect case container(_ nodes: [Node], separator: Separator = .newLine())
|
||||
|
||||
/// A container to identify example nodes, so some other nodes can get injected
|
||||
/// when this is rendered.
|
||||
///
|
||||
/// NOTE: Things blow up if this is not used correctly, at least when using the `Discussion`
|
||||
/// to render the nodes, so this is not safe to create aside from the static methods that
|
||||
/// create an example node. If not using in the context of a `Discussion` then it's fine to
|
||||
/// use, as it doesn't do anything except stand as an type identifier.
|
||||
indirect case example(_ node: Node)
|
||||
|
||||
/// A root node that renders the given string without modification.
|
||||
case text(_ text: String)
|
||||
|
||||
static func container(separator: Separator = .newLine(), _ nodes: Node...) -> Self {
|
||||
.container(nodes, separator: separator)
|
||||
}
|
||||
|
||||
static func container(_ lhs: Node, _ rhs: Node, separator: Separator = .newLine()) -> Self {
|
||||
.container([lhs, rhs], separator: separator)
|
||||
}
|
||||
|
||||
static func styledText(_ text: String, color: NamedColor? = nil, styles: [Style]? = nil) -> Self {
|
||||
var string = text
|
||||
if let color {
|
||||
string = string.applyingColor(color)
|
||||
}
|
||||
if let styles {
|
||||
for style in styles {
|
||||
string = string.applyingStyle(style)
|
||||
}
|
||||
}
|
||||
return .text(string)
|
||||
}
|
||||
|
||||
static func styledText(_ text: String, color: NamedColor? = nil, style: Style) -> Self {
|
||||
.styledText(text, color: color, styles: [style])
|
||||
}
|
||||
|
||||
static func boldYellowText(_ text: String) -> Self {
|
||||
.styledText(text, color: .yellow, styles: [.bold])
|
||||
}
|
||||
|
||||
static func section(header: Node, content: Node, separator: Separator = .newLine()) -> Self {
|
||||
.container([header, content], separator: separator)
|
||||
}
|
||||
|
||||
static func section(header: String, label: Node, separator: Separator = .newLine()) -> Self {
|
||||
.section(header: .boldYellowText(header), content: label, separator: separator)
|
||||
}
|
||||
|
||||
static func important(label: String) -> Self {
|
||||
.section(
|
||||
header: .text("IMPORTANT NOTE:".red.bold.underline),
|
||||
content: .text(label.italic),
|
||||
separator: .newLine()
|
||||
)
|
||||
}
|
||||
|
||||
static func note(_ text: String, inline: Bool = true) -> Self {
|
||||
.section(header: "NOTE:", label: .text(text.italic), separator: inline ? .space() : .newLine())
|
||||
}
|
||||
|
||||
static func shellCommand(_ text: String, symbol: String = " $") -> Self {
|
||||
.text("\(symbol) \(text)")
|
||||
}
|
||||
|
||||
static func seeAlso(label: String, command: String, appendHelpToCommand: Bool = true) -> Self {
|
||||
.container([
|
||||
.container(.boldYellowText("See Also:"), .text(label.italic), separator: .space()),
|
||||
.shellCommand("\(appendHelpToCommand ? command + " --help" : command)")
|
||||
])
|
||||
}
|
||||
|
||||
static var exampleHeading: Self {
|
||||
.section(header: "Examples:", label: .text("Some common examples."), separator: .space())
|
||||
}
|
||||
|
||||
var isExampleNode: Bool {
|
||||
if case .example = self { return true }
|
||||
return false
|
||||
}
|
||||
|
||||
fileprivate static func exampleContainer(_ label: String, _ command: String) -> Self {
|
||||
.container(
|
||||
.exampleLabel("\(label)\n"),
|
||||
.shellCommand(command, symbol: " $"),
|
||||
separator: .empty
|
||||
)
|
||||
}
|
||||
|
||||
/// Renders an example usage of the command.
|
||||
static func example(label: String, example: String, parentCommand: String?) -> Self {
|
||||
let string: String
|
||||
if let parent = parentCommand {
|
||||
string = "\(Constants.appName) \(parent) \(example)"
|
||||
} else {
|
||||
string = "\(Constants.appName) \(example)"
|
||||
}
|
||||
return .example(.exampleContainer(label, string))
|
||||
}
|
||||
|
||||
static func exampleLabel(_ label: String) -> Node {
|
||||
.text(" \(label.green.italic)")
|
||||
}
|
||||
|
||||
func render() -> String {
|
||||
switch self {
|
||||
case let .container(nodes, separator):
|
||||
return nodes.render(separator: separator)
|
||||
case let .text(text):
|
||||
return text
|
||||
case let .example(node):
|
||||
return node.render()
|
||||
.eraseToAnyTextNode()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Node: ExpressibleByStringLiteral {
|
||||
|
||||
typealias StringLiteralType = String
|
||||
|
||||
init(stringLiteral value: String) {
|
||||
self = .text(value)
|
||||
}
|
||||
}
|
||||
|
||||
extension Node: ExpressibleByArrayLiteral {
|
||||
|
||||
typealias ArrayLiteralElement = Node
|
||||
|
||||
init(arrayLiteral elements: Node...) {
|
||||
self = .container(elements)
|
||||
}
|
||||
}
|
||||
|
||||
private extension Array where Element == (label: String, example: String) {
|
||||
func nodes(_ parentCommand: String?) -> [Node] {
|
||||
map { .example(label: $0.label, example: $0.example, parentCommand: parentCommand) }
|
||||
}
|
||||
}
|
||||
|
||||
extension Array where Element == Node {
|
||||
|
||||
static var defaultNodes: Self {
|
||||
[.note("Most options are not required if you have a configuration file setup.")]
|
||||
}
|
||||
|
||||
fileprivate func render(separator: Node.Separator = .newLine()) -> String {
|
||||
map {
|
||||
// Strip of any new-line characters from the last section of the rendered string
|
||||
// of the node. This allows us to have a consistent single new-line between each
|
||||
// rendered node.
|
||||
var string = $0.render()[...]
|
||||
while string.hasSuffix("\n") {
|
||||
string = string.dropLast()
|
||||
}
|
||||
return String(string)
|
||||
}
|
||||
.joined(separator: separator.value)
|
||||
}
|
||||
}
|
||||
|
||||
private extension String {
|
||||
func repeating(_ count: Int) -> Self {
|
||||
guard count > 0 else { return self }
|
||||
var count = count
|
||||
var output = self
|
||||
|
||||
while count > 0 {
|
||||
output = "\(output)\(self)"
|
||||
count -= 1
|
||||
}
|
||||
return output
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@ import Rainbow
|
||||
// Constant string values.
|
||||
enum Constants {
|
||||
static let appName = "hpa"
|
||||
static let brewPackages = [
|
||||
"ansible", "imagemagick", "pandoc", "texLive"
|
||||
]
|
||||
static let playbookFileName = "main.yml"
|
||||
static let inventoryFileName = "inventory.ini"
|
||||
static let importantExtraArgsNote = """
|
||||
|
||||
@@ -1,86 +1,146 @@
|
||||
import CliDoc
|
||||
|
||||
extension CliDoc.Discussion {
|
||||
extension CliDoc.Discussion where Content == AnyTextNode {
|
||||
static func playbook(
|
||||
examples: [(label: String, example: String)]
|
||||
examples: [Example]
|
||||
) -> Self {
|
||||
.init {
|
||||
Section {
|
||||
VStack {
|
||||
Note.mostOptionsNotRequired
|
||||
Examples(examples: examples)
|
||||
ExampleSection.default(examples: examples)
|
||||
SeeAlso(label: "Ansible playbook options.", command: "ansible-playbook --help")
|
||||
ImportantNote.passingExtraArgs
|
||||
}
|
||||
.labeledContentStyle(.custom("foo:"))
|
||||
.separator(.newLine(count: 2))
|
||||
.eraseToAnyTextNode()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ShellCommand {
|
||||
extension Array where Element == Example {
|
||||
func addingPassingOptions(command: String) -> Self {
|
||||
var output = self
|
||||
output.append((
|
||||
label: "Passing extra arguments / options",
|
||||
example: "\(command) -- --vaultId=myId@$SCRIPTS/vault-gopass-client"
|
||||
))
|
||||
return output
|
||||
}
|
||||
|
||||
func addingPipeToOtherCommand(command: String) -> Self {
|
||||
var output = self
|
||||
output.append((
|
||||
label: "Piping output to other command.",
|
||||
example: "\(command) --quiet | xargs open"
|
||||
))
|
||||
return output
|
||||
}
|
||||
}
|
||||
|
||||
extension ExampleSection where Header == String, Label == String {
|
||||
static func `default`(
|
||||
examples: [Example],
|
||||
usesPassingExtraOptions: Bool = true,
|
||||
usesPipeToOtherCommand: Bool = true
|
||||
) -> some TextNode {
|
||||
var examples = examples
|
||||
|
||||
if let first = examples.first {
|
||||
if usesPassingExtraOptions {
|
||||
examples = examples.addingPassingOptions(command: first.example)
|
||||
}
|
||||
if usesPipeToOtherCommand {
|
||||
examples = examples.addingPipeToOtherCommand(command: first.example)
|
||||
}
|
||||
}
|
||||
return Self(examples: examples) {
|
||||
"EXAMPLES:"
|
||||
} label: {
|
||||
"Some common usage examples."
|
||||
}
|
||||
.exampleStyle(HPAExampleStyle())
|
||||
}
|
||||
}
|
||||
|
||||
struct HPAExampleStyle: ExampleStyle {
|
||||
|
||||
func render(content: ExampleConfiguration) -> some TextNode {
|
||||
VStack {
|
||||
content.examples.map { example in
|
||||
VStack {
|
||||
example.label.color(.green).bold()
|
||||
ShellCommand.hpaCommand(example.example)
|
||||
}
|
||||
}
|
||||
}
|
||||
.separator(.newLine(count: 2))
|
||||
}
|
||||
}
|
||||
|
||||
extension ShellCommand where Content == String, Symbol == String {
|
||||
static func hpaCommand(_ command: String) -> Self {
|
||||
.init(command: "\(Constants.appName) \(command)")
|
||||
.init { "\(Constants.appName) \(command)" } symbol: { "$" }
|
||||
}
|
||||
}
|
||||
|
||||
struct SeeAlso: NodeRepresentable {
|
||||
struct SeeAlso<Label: TextNode, Content: TextNode>: TextNode {
|
||||
|
||||
let node: any NodeRepresentable
|
||||
|
||||
init(label: String, command: String) {
|
||||
self.node = Group(separator: "\n") {
|
||||
Note("SEE ALSO:") {
|
||||
label
|
||||
}
|
||||
ShellCommand(command: command)
|
||||
}
|
||||
}
|
||||
|
||||
func render() -> String {
|
||||
node.render()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct Examples: NodeRepresentable {
|
||||
typealias Example = (label: String, example: String)
|
||||
let examples: [Example]
|
||||
|
||||
func render() -> String {
|
||||
Group(separator: "\n") {
|
||||
Note("EXAMPLES:") { "Common usage examples." }
|
||||
for (label, command) in examples {
|
||||
LabeledContent("\t\(label.green.bold)", separator: "\n") {
|
||||
ShellCommand.hpaCommand(command)
|
||||
}
|
||||
}
|
||||
if let first = examples.first {
|
||||
LabeledContent("\n\tPassing extra options.".green.bold, separator: "\n") {
|
||||
ShellCommand.hpaCommand("\(first.example) -- --vault-id \"myId@$SCRIPTS/vault-gopass-client\"")
|
||||
}
|
||||
}
|
||||
}.render()
|
||||
}
|
||||
}
|
||||
|
||||
struct Note: NodeRepresentable {
|
||||
|
||||
let node: any NodeRepresentable
|
||||
let title = "SEE ALSO:"
|
||||
let label: Label
|
||||
let content: Content
|
||||
|
||||
init(
|
||||
_ label: String = "NOTE:",
|
||||
@NodeBuilder _ content: () -> any NodeRepresentable
|
||||
@TextBuilder content: () -> Content,
|
||||
@TextBuilder label: () -> Label
|
||||
) {
|
||||
self.node = LabeledContent(
|
||||
separator: " ",
|
||||
label: { label.yellow.bold },
|
||||
content: { content().style(.italic) }
|
||||
)
|
||||
self.content = content()
|
||||
self.label = label()
|
||||
}
|
||||
|
||||
func render() -> String {
|
||||
node.render()
|
||||
var body: some TextNode {
|
||||
VStack {
|
||||
LabeledContent {
|
||||
label.italic()
|
||||
} label: {
|
||||
title.color(.yellow).bold().underline()
|
||||
}
|
||||
ShellCommand { content } symbol: { "$" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension SeeAlso where Content == String, Label == Empty {
|
||||
init(command: String) {
|
||||
self.init { "\(Constants.appName) \(command)" } label: { Empty() }
|
||||
}
|
||||
}
|
||||
|
||||
extension SeeAlso where Content == String, Label == String {
|
||||
init(label: String, command: String) {
|
||||
self.init { "\(Constants.appName) \(command)" } label: { label }
|
||||
}
|
||||
}
|
||||
|
||||
struct Note<Content: TextNode>: TextNode {
|
||||
|
||||
let label: String = "NOTE:"
|
||||
let content: Content
|
||||
|
||||
init(
|
||||
@TextBuilder _ content: () -> Content
|
||||
) {
|
||||
self.content = content()
|
||||
}
|
||||
|
||||
var body: some TextNode {
|
||||
HStack {
|
||||
label.color(.yellow).bold()
|
||||
content
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Note where Content == String {
|
||||
static var mostOptionsNotRequired: Self {
|
||||
.init {
|
||||
"Most options are not required if you have a configuration file setup."
|
||||
@@ -88,26 +148,27 @@ struct Note: NodeRepresentable {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Fix the text.
|
||||
struct ImportantNote: NodeRepresentable {
|
||||
struct ImportantNote<Content: TextNode>: TextNode {
|
||||
|
||||
let node: any NodeRepresentable
|
||||
let label: String = "IMPORTANT NOTE:"
|
||||
let content: Content
|
||||
|
||||
init(
|
||||
_ label: String = "IMPORTANT NOTE:",
|
||||
@NodeBuilder _ content: () -> any NodeRepresentable
|
||||
@TextBuilder _ content: () -> Content
|
||||
) {
|
||||
self.node = LabeledContent(
|
||||
separator: "\n",
|
||||
label: { label.red.bold.underline },
|
||||
content: { content().style(.italic) }
|
||||
)
|
||||
self.content = content()
|
||||
}
|
||||
|
||||
func render() -> String {
|
||||
node.render()
|
||||
var body: some TextNode {
|
||||
HStack {
|
||||
label.color(.red).bold().underline()
|
||||
content
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension ImportantNote where Content == String {
|
||||
static var passingExtraArgs: Self {
|
||||
.init {
|
||||
"""
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import ArgumentParser
|
||||
import CommandClient
|
||||
import ConfigurationClient
|
||||
import PlaybookClient
|
||||
|
||||
struct BasicGlobalOptions: ParsableArguments {
|
||||
@Flag(
|
||||
@@ -26,10 +29,10 @@ struct BasicGlobalOptions: ParsableArguments {
|
||||
struct GlobalOptions: ParsableArguments {
|
||||
|
||||
@Option(
|
||||
name: .shortAndLong,
|
||||
name: .long,
|
||||
help: "Optional path to the ansible hpa playbook directory."
|
||||
)
|
||||
var playbookDir: String?
|
||||
var playbookDirectory: String?
|
||||
|
||||
@Option(
|
||||
name: .shortAndLong,
|
||||
@@ -55,3 +58,39 @@ struct GlobalOptions: ParsableArguments {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension GlobalOptions {
|
||||
func loggingOptions(commandName: String) -> LoggingOptions {
|
||||
.init(
|
||||
commandName: commandName,
|
||||
logLevel: .init(globals: basic, quietOnlyPlaybook: quietOnlyPlaybook)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension BasicGlobalOptions {
|
||||
func loggingOptions(commandName: String) -> LoggingOptions {
|
||||
.init(
|
||||
commandName: commandName,
|
||||
logLevel: .init(globals: self, quietOnlyPlaybook: false)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import Dependencies
|
||||
import Logging
|
||||
|
||||
extension Logger.Level {
|
||||
@@ -21,34 +20,3 @@ extension Logger.Level {
|
||||
self = .info
|
||||
}
|
||||
}
|
||||
|
||||
func withSetupLogger(
|
||||
commandName: String,
|
||||
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)")
|
||||
$0.logger.logLevel = .init(globals: globals, quietOnlyPlaybook: quietOnlyPlaybook)
|
||||
$0.logger[metadataKey: "command"] = "\(commandName.blue)"
|
||||
} operation: {
|
||||
try await operation()
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
import ArgumentParser
|
||||
import CliClient
|
||||
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(\.logger) var logger
|
||||
|
||||
logger.debug("Begin run playbook: \(globals)")
|
||||
|
||||
let configuration = try cliClient.ensuredConfiguration(configuration)
|
||||
|
||||
logger.debug("Configuration: \(configuration)")
|
||||
|
||||
let playbookDir = try ensureString(
|
||||
globals: globals,
|
||||
configuration: configuration,
|
||||
globalsKeyPath: \.playbookDir,
|
||||
configurationKeyPath: \.playbookDir
|
||||
)
|
||||
let playbook = "\(playbookDir)/\(Constants.playbookFileName)"
|
||||
|
||||
let inventory = (try? ensureString(
|
||||
globals: globals,
|
||||
configuration: configuration,
|
||||
globalsKeyPath: \.inventoryPath,
|
||||
configurationKeyPath: \.inventoryPath
|
||||
)) ?? "\(playbookDir)/\(Constants.inventoryFileName)"
|
||||
|
||||
let defaultArgs = (configuration.defaultPlaybookArgs ?? [])
|
||||
+ (configuration.defaultVaultArgs ?? [])
|
||||
|
||||
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 CliClient {
|
||||
func ensuredConfiguration(_ configuration: Configuration?) throws -> Configuration {
|
||||
guard let configuration else {
|
||||
return try loadConfiguration()
|
||||
}
|
||||
return configuration
|
||||
}
|
||||
}
|
||||
|
||||
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,48 +0,0 @@
|
||||
import Dependencies
|
||||
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(\.logger) var logger
|
||||
|
||||
logger.debug("Begin run vault: \(options)")
|
||||
|
||||
let configuration = try cliClient.ensuredConfiguration(nil)
|
||||
logger.debug("Configuration: \(configuration)")
|
||||
|
||||
let path: String
|
||||
if let file = options.file {
|
||||
path = file
|
||||
} else {
|
||||
path = try cliClient.findVaultFileInCurrentDirectory()
|
||||
}
|
||||
|
||||
logger.debug("Vault path: \(path)")
|
||||
|
||||
let defaultArgs = configuration.defaultVaultArgs ?? []
|
||||
|
||||
var vaultArgs = ["ansible-vault"]
|
||||
+ args
|
||||
+ defaultArgs
|
||||
+ options.extraArgs
|
||||
+ [path]
|
||||
|
||||
if args.contains("encrypt"),
|
||||
!vaultArgs.contains("--encrypt-vault-id"),
|
||||
let id = configuration.defaultVaultEncryptId
|
||||
{
|
||||
vaultArgs.append(contentsOf: ["--encrypt-vault-id", id])
|
||||
}
|
||||
|
||||
try await cliClient.runCommand(
|
||||
quiet: options.quiet,
|
||||
shell: options.shellOrDefault,
|
||||
vaultArgs
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user