feat: Reorganizes files

This commit is contained in:
2024-11-30 09:21:27 -05:00
parent ed3f752694
commit 2c551e33d3
12 changed files with 212 additions and 113 deletions

View File

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

View File

@@ -1,15 +0,0 @@
import ArgumentParser
struct CreateProjectTemplateCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "create-project-template",
abstract: "Create a home performance assesment project template."
)
@OptionGroup var globals: GlobalOptions
mutating func run() async throws {
fatalError()
}
}

View File

@@ -0,0 +1,35 @@
import ArgumentParser
extension CommandConfiguration {
static func playbookCommandConfiguration(
commandName: String,
abstract: String,
examples: (label: String, example: String)...
) -> Self {
guard examples.count > 0 else {
fatalError("Did not supply any examples.")
}
return Self(
commandName: commandName,
abstract: "\(abstract.blue)",
discussion: """
\("NOTE:".yellow) Most options are not required if you have a configuration file setup.
\("Examples:".yellow)
\(examples.map { "\($0.label.green.italic)\n $ hpa \($0.example)" }.joined(separator: "\n"))
\("Passing extra args to the playbook.".green.italic)
$ hpa \(examples[0].example) -- --vault-id "myId@$SCRIPTS/vault-gopass-client"
\("See Also:".yellow) \("Ansible playbook options.".italic)
$ ansible-playbook --help
\("IMPORTANT NOTE:".red) Any extra arguments to pass to the playbook invocation have to
be at the end with `--` before any arguments otherwise there will
be an "Unkown option" error. See examples above.
"""
)
}
}

View File

@@ -6,40 +6,6 @@ import Logging
import Rainbow
import ShellClient
extension CommandConfiguration {
static func playbookCommandConfiguration(
commandName: String,
abstract: String,
examples: (label: String, example: String)...
) -> Self {
guard examples.count > 0 else {
fatalError("Did not supply any examples.")
}
return Self(
commandName: commandName,
abstract: "\(abstract.blue)",
discussion: """
\("NOTE:".yellow) Most options are not required if you have a configuration file setup.
\("Examples:".yellow)
\(examples.map { "\($0.label.green.italic)\n $ hpa \($0.example)" }.joined(separator: "\n"))
\("Passing extra args to the playbook.".green.italic)
$ hpa \(examples[0].example) -- --vault-id "myId@$SCRIPTS/vault-gopass-client"
\("See Also:".yellow) \("Ansible playbook options.".italic)
$ ansible-playbook --help
\("IMPORTANT NOTE:".red) Any extra arguments to pass to the playbook invocation have to
be at the end with `--` before any arguments otherwise there will
be an "Unkown option" error. See examples above.
"""
)
}
}
extension BasicGlobalOptions {
var shellOrDefault: ShellCommand.Shell {
@@ -167,19 +133,17 @@ func runPlaybook(
configurationKeyPath: \.inventoryPath
)) ?? "\(playbookDir)/inventory.ini"
var playbookArgs = [
"ansible-playbook", playbook,
"--inventory", inventory
]
if let defaultArgs = configuration.defaultPlaybookArgs {
playbookArgs.append(defaultArgs)
}
let defaultArgs = configuration.defaultPlaybookArgs ?? []
try await cliClient.runCommand(
quiet: globals.quietOnlyPlaybook ? true : globals.basic.quiet,
shell: globals.shellOrDefault,
playbookArgs + args + extraArgs
[
"ansible-playbook", playbook,
"--inventory", inventory
] + args
+ extraArgs
+ defaultArgs
)
}
}

View File

@@ -0,0 +1,56 @@
import ArgumentParser
struct CreateProjectTemplateCommand: AsyncParsableCommand {
static let commandName = "create-template"
static let configuration = CommandConfiguration.playbookCommandConfiguration(
commandName: commandName,
abstract: "Create a home performance assesment project template.",
examples: (label: "Create Template", example: "\(commandName) /path/to/project-template")
)
@OptionGroup var globals: GlobalOptions
@Option(
name: .shortAndLong,
help: "Customize the directory where template variables are stored."
)
var templateVars: String?
@Flag(
name: .long,
help: "Do not generate ansible-vault variables file."
)
var noVault: Bool = false
@Argument(
help: "Path to the project template directory.",
completion: .directory
)
var path: String
@Argument(
help: "Extra arguments passed to the playbook."
)
var extraArgs: [String] = []
mutating func run() async throws {
let varsDir = templateVars != nil
? ["--extra-vars", "repo_vars_dir=\(templateVars!)"]
: []
let vault = noVault ? ["--extra-vars", "use_vault=false"] : []
try await runPlaybook(
commandName: Self.commandName,
globals: globals,
extraArgs: extraArgs,
[
"--tags", "repo-template",
"--extra-vars", "output_dir=\(path)"
] + varsDir
+ vault
)
}
}

View File

@@ -0,0 +1,17 @@
import ArgumentParser
struct UtilsCommand: AsyncParsableCommand {
static let commandName = "utils"
static let configuration = CommandConfiguration(
commandName: commandName,
abstract: "\("Utility commands.".blue)",
discussion: """
These are commands that are generally only run on occasion / less frequently used.
""",
subcommands: [
CreateProjectTemplateCommand.self, GenerateConfigurationCommand.self
]
)
}