From 0ab9b7f63e8ed51e678956e298dff5d97da32645 Mon Sep 17 00:00:00 2001 From: Matt Czech Date: Thu, 29 Sep 2022 10:03:21 -0500 Subject: [PATCH] Add get command to access a secret for a key --- README.md | 11 +++++++++++ Sources/Chimney/Error.swift | 3 +++ Sources/Chimney/GetCommand.swift | 26 ++++++++++++++++++++++++++ Sources/Chimney/main.swift | 2 +- 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 Sources/Chimney/GetCommand.swift diff --git a/README.md b/README.md index d06b800..47960c7 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,17 @@ Options: - -o, --output: The output file. Defaults to [KeySpecName]Keys.swift - -s, --spec: An optional path to a `.yml` key spec. Defaults to `chimney.yml`. +### Get + +As an alternative to accessing secrets at runtime via the generated file, `get` can be used to get a secret for a key. This enables build time scripts to access secrets. + +``` +chimney get +``` + +Options: + - -s, --spec: An optional path to a `.yml` key spec. Defaults to `chimney.yml` + ### Integration Once the file is generated, go ahead and add it to your project in Xcode, but also make sure to add it to your `.gitignore`: diff --git a/Sources/Chimney/Error.swift b/Sources/Chimney/Error.swift index 67a09fe..d6d277a 100644 --- a/Sources/Chimney/Error.swift +++ b/Sources/Chimney/Error.swift @@ -5,9 +5,12 @@ import SwiftCLI enum KeySpecError: Error, ProcessError { case missingKeySpec(Path) case missingKeys([String]) + case missingKey(String) var message: String? { switch self { + case .missingKey(let key): + return "\("Error:", color: .red) No value found in keychain for \(key, color: .green). Run \("chimney setup", color: .magenta) to enter missing value." case .missingKeys(let keys): return "\("Error:", color: .red) No value found in keychain for \(keys, color: .green). Run \("chimney setup", color: .magenta) to enter missing values." case .missingKeySpec(let path): diff --git a/Sources/Chimney/GetCommand.swift b/Sources/Chimney/GetCommand.swift new file mode 100644 index 0000000..2c4ceab --- /dev/null +++ b/Sources/Chimney/GetCommand.swift @@ -0,0 +1,26 @@ +import PathKit +import SwiftCLI + +class GetCommand: Command { + let name = "get" + let shortDescription = "Outputs a secret from the keychain for the key" + + let spec = Key( + "-s", + "--spec", + description: "The path to the key spec file. Defaults to chimney.yml" + ) + + @Param var key: String + + func execute() throws { + let keySpec = try KeySpec(path: spec.value) + let keyStore = KeyStore(spec: keySpec) + + guard let value = keyStore[key] else { + throw KeySpecError.missingKey(key) + } + + stdout <<< value + } +} diff --git a/Sources/Chimney/main.swift b/Sources/Chimney/main.swift index fa11674..db538dd 100644 --- a/Sources/Chimney/main.swift +++ b/Sources/Chimney/main.swift @@ -2,5 +2,5 @@ import Foundation import SwiftCLI let cli = CLI(name: "chimney") -cli.commands = [SetupCommand(), GenerateCommand()] +cli.commands = [SetupCommand(), GenerateCommand(), GetCommand()] cli.goAndExit()