Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get command to access a secret for a key #6

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <key>
```

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`:
Expand Down
3 changes: 3 additions & 0 deletions Sources/Chimney/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 26 additions & 0 deletions Sources/Chimney/GetCommand.swift
Original file line number Diff line number Diff line change
@@ -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<Path>(
"-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
}
}
2 changes: 1 addition & 1 deletion Sources/Chimney/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import Foundation
import SwiftCLI

let cli = CLI(name: "chimney")
cli.commands = [SetupCommand(), GenerateCommand()]
cli.commands = [SetupCommand(), GenerateCommand(), GetCommand()]
cli.goAndExit()