Skip to content

Commit

Permalink
Merge pull request #349 from qutheory/fix-serve-default
Browse files Browse the repository at this point in the history
fix serve by default when not passed explicitly
  • Loading branch information
loganwright committed Jun 3, 2016
2 parents 6e93b63 + f2f2b9c commit 5d9052d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Sources/Vapor/Core/Application.swift
Expand Up @@ -2,7 +2,7 @@ import libc
import MediaType
import Foundation

public let VERSION = "0.9.2"
public let VERSION = "0.10"

public class Application {
/**
Expand Down Expand Up @@ -174,13 +174,21 @@ extension Application {
}

private func executeCommand() {
// arguments prefixed w/ `--` are accessible through `app.config["app", "argument"]`
let consoleCommands = NSProcessInfo.processInfo().arguments.filter { !$0.hasSuffix("--") }
let input = NSProcessInfo.processInfo().arguments
let (command, arguments) = extract(fromInput: input)
command.run(on: self, with: arguments)
}

internal func extract(fromInput input: [String]) -> (command: Command.Type, arguments: [String]) {
// options prefixed w/ `--` are accessible through `app.config["app", "argument"]`
let consoleCommands = NSProcessInfo.processInfo().arguments.filter { !$0.hasPrefix("--") }
var iterator = consoleCommands.makeIterator()
let _ = iterator.next() // dump directory command
let command = iterator.next() ?? "serve"
let commandKey = iterator.next() ?? "serve"
let arguments = Array(iterator)
commands[command]?.run(on: self, with: arguments)

let command = commands[commandKey] ?? Serve.self
return (command, arguments)
}
}

Expand Down
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Expand Up @@ -6,6 +6,7 @@ import XCTest
XCTMain([
testCase(ApplicationTests.allTests),
testCase(ConfigTests.allTests),
testCase(ConsoleTests.allTests),
testCase(ControllerTests.allTests),
testCase(EnvironmentTests.allTests),
testCase(EventTests.allTests),
Expand Down
18 changes: 18 additions & 0 deletions Tests/Vapor/ConsoleTests.swift
@@ -0,0 +1,18 @@
import XCTest
@testable import Vapor

class ConsoleTests: XCTestCase {
static var allTests: [(String, (ConsoleTests) -> () throws -> Void)] {
return [
("testDefaultServe", testDefaultServe)
]
}

func testDefaultServe() {
let args = ["/Path/To/Executable", "--port=8001", "--env=production"]
let app = Application()
let (command, arguments) = app.extract(fromInput: args)
XCTAssert(command == Serve.self)
XCTAssert(arguments.isEmpty)
}
}

0 comments on commit 5d9052d

Please sign in to comment.