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

console-kit property wrappers #2042

Merged
merged 3 commits into from Aug 27, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Expand Up @@ -11,7 +11,7 @@ let package = Package(
.package(url: "https://github.com/vapor/async-kit.git", from: "1.0.0-alpha"),

// 💻 APIs for creating interactive CLI tools.
.package(url: "https://github.com/vapor/console-kit.git", from: "4.0.0-alpha"),
.package(url: "https://github.com/vapor/console-kit.git", from: "4.0.0-alpha.2"),

// 🔑 Hashing (BCrypt, SHA2, HMAC), encryption (AES), public-key (RSA), and random data generation.
.package(url: "https://github.com/vapor/open-crypto.git", from: "4.0.0-alpha.2"),
Expand Down
3 changes: 1 addition & 2 deletions Sources/Vapor/Application.swift
Expand Up @@ -111,8 +111,7 @@ public final class Application {
defer { c.shutdown() }
let command = try c.make(Commands.self).group()
let console = try c.make(Console.self)
var runInput = self.environment.commandInput
try console.run(command, input: &runInput)
try console.run(command, input: self.environment.commandInput)
}


Expand Down
9 changes: 4 additions & 5 deletions Sources/Vapor/Commands/BootCommand.swift
Expand Up @@ -5,10 +5,9 @@
///
public final class BootCommand: Command {
/// See `Command`.
public struct Signature: CommandSignature { }

/// See `Command`.
public let signature = Signature()
public struct Signature: CommandSignature {
public init() { }
}

/// See `Command`.
public var help: String {
Expand All @@ -19,7 +18,7 @@ public final class BootCommand: Command {
public init() { }

/// See `Command`.
public func run(using context: CommandContext<BootCommand>) throws {
public func run(using context: CommandContext, signature: Signature) throws {
context.console.success("Done.")
}
}
9 changes: 4 additions & 5 deletions Sources/Vapor/Commands/RoutesCommand.swift
Expand Up @@ -13,10 +13,9 @@
/// An asterisk indicates a catch-all. Any path components after a catch-all will be discarded and ignored.
public final class RoutesCommand: Command {
/// See `Command`.
public struct Signature: CommandSignature { }

/// See `Command`.
public let signature = Signature()
public struct Signature: CommandSignature {
public init() { }
}

/// See `Command`.
public var help: String {
Expand All @@ -32,7 +31,7 @@ public final class RoutesCommand: Command {
}

/// See `Command`.
public func run(using context: CommandContext<RoutesCommand>) throws {
public func run(using context: CommandContext, signature: Signature) throws {
let console = context.console

var longestMethod = 0
Expand Down
24 changes: 15 additions & 9 deletions Sources/Vapor/Commands/ServeCommand.swift
Expand Up @@ -4,11 +4,17 @@
/// Server starting on http://localhost:8080
///
public final class ServeCommand: Command {
/// See `Command`.
public struct Signature: CommandSignature {
public let hostname = Option<String>(name: "hostname", short: "H", type: .value, help: "Set the hostname the server will run on.")
public let port = Option<Int>(name: "port", short: "p", type: .value, help: "Set the port the server will run on.")
public let bind = Option<String>(name: "bind", short: "b", type: .value, help: "Convenience for setting hostname and port together.")
@Option(name: "hostname", short: "H", help: "Set the hostname the server will run on.")
var hostname: String?

@Option(name: "port", short: "p", help: "Set the port the server will run on.")
var port: Int?

@Option(name: "bind", short: "b", help: "Convenience for setting hostname and port together.")
var bind: String?

public init() { }
}

/// See `Command`.
Expand All @@ -29,14 +35,14 @@ public final class ServeCommand: Command {
}

/// See `Command`.
public func run(using context: CommandContext<ServeCommand>) throws {
public func run(using context: CommandContext, signature: Signature) throws {
try self.server.start(
hostname: context.option(\.hostname)
hostname: signature.hostname
// 0.0.0.0:8080, 0.0.0.0, parse hostname
?? context.option(\.bind)?.split(separator: ":").first.flatMap(String.init),
port: context.option(\.port)
?? signature.bind?.split(separator: ":").first.flatMap(String.init),
port: signature.port
// 0.0.0.0:8080, :8080, parse port
?? context.option(\.bind)?.split(separator: ":").last.flatMap(String.init).flatMap(Int.init)
?? signature.bind?.split(separator: ":").last.flatMap(String.init).flatMap(Int.init)
)

// setup signal sources for shutdown
Expand Down
25 changes: 7 additions & 18 deletions Sources/Vapor/Logging/LoggingSystem+Environment.swift
@@ -1,11 +1,14 @@
extension LoggingSystem {
public static func bootstrap(from environment: inout Environment) throws {
struct LogSignature: CommandSignature {
@Option(name: "log", help: "Change log level")
var level: Logger.Level?
init() { }
}
try LoggingSystem.bootstrap(
console: Terminal(),
level: environment.commandInput.parseOption(
value: Logger.Level.self,
name: "log"
) ?? (environment == .production ? .error: .info)
level: LogSignature(from: &environment.commandInput).level
?? (environment == .production ? .error: .info)
)
}
}
Expand Down Expand Up @@ -36,17 +39,3 @@ extension Logger.Level: LosslessStringConvertible {
}
}
}

private extension CommandInput {
mutating func parseOption<Value>(
value: Value.Type,
name: String,
short: Character? = nil
) throws -> Value?
where Value: LosslessStringConvertible
{
let option = Option<Value>(name: name, short: short, type: .value, help: "")
return try self.parse(option: option)
.flatMap { Value.init($0) }
}
}
7 changes: 6 additions & 1 deletion Sources/Vapor/Services/Environment.swift
Expand Up @@ -25,8 +25,13 @@ public struct Environment: Equatable {
/// - arguments: `CommandInput` to parse `--env` flag from.
/// - returns: The detected environment, or default env.
public static func detect(from commandInput: inout CommandInput) throws -> Environment {
struct EnvironmentSignature: CommandSignature {
@Option(name: "env", short: "e", help: "Change the application's environment")
var environment: String?
init() { }
}
var env: Environment
if let value = try commandInput.parse(option: Option<String>(name: "env", short: "e", type: .value)) {
if let value = try EnvironmentSignature(from: &commandInput).environment {
switch value {
case "prod", "production": env = .production
case "dev", "development": env = .development
Expand Down