Skip to content

Commit

Permalink
Merge branch 'localization'
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed May 26, 2016
2 parents 7ae2d5d + 772940c commit f4fab08
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
24 changes: 22 additions & 2 deletions Sources/Vapor/Config/Process+Config.swift
Expand Up @@ -2,7 +2,7 @@ import Foundation

extension Process {
static func makeCLIConfig() -> JSONDirectory {
let configArgs = NSProcessInfo.processInfo().arguments.filter { $0.hasPrefix("--config:") }
let configArgs = NSProcessInfo.processInfo().arguments.filter { $0.hasPrefix("--") }

// [FileName: Json]
var directory: [String: JSON] = [:]
Expand Down Expand Up @@ -43,6 +43,14 @@ extension Process {
}

static func parseConfigKey(_ key: String) -> (file: String, path: [PathIndex])? {
if key.hasPrefix("--config:") {
return parseComplexConfigKey(key)
} else {
return parseSimpleConfigKey(key)
}
}

private static func parseComplexConfigKey(_ key: String) -> (file: String, path: [PathIndex])? {
// --config:app.port
// expect [--config, app.port]
let paths = key
Expand All @@ -66,7 +74,19 @@ extension Process {

// first argument is file name, subsequent arguments are paths
keyPaths.remove(at: 0)

return (fileName, keyPaths.map { $0 as PathIndex })
}

private static func parseSimpleConfigKey(_ key: String) -> (file: String, path: [PathIndex])? {
// --key.path.to.automate
guard
let path = key
.components(separatedBy: "--")
.last?
.components(separatedBy: ".")
else { return nil }

return ("app", path.map { $0 as PathIndex })
}
}
16 changes: 16 additions & 0 deletions Tests/Vapor/ConfigTests.swift
Expand Up @@ -36,4 +36,20 @@ class ConfigTests: XCTestCase {
let config = Config(workingDirectory: workDir, environment: .production)
XCTAssert(config["app", "nested", "c", "true"].bool == false, "Nesting config incorrectly loaded.")
}

func testConfigKeys() {
guard let (complexFile, complexPath) = Process.parseConfigKey("--config:file.path.to.value") else {
XCTFail("Couldn't extract complex cli config")
return
}
XCTAssert(complexFile == "file")
XCTAssert(complexPath.map { "\($0)" } == ["path", "to", "value"])

guard let (simpleFile, simplePath) = Process.parseConfigKey("--some-key") else {
XCTFail("Couldn't extract simple cli config")
return
}
XCTAssert(simpleFile == "app")
XCTAssert(simplePath.map { "\($0)" } == ["some-key"])
}
}

0 comments on commit f4fab08

Please sign in to comment.