From 80556d64aea7b193a1ed6727ed2dfad0a78e990a Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Tue, 27 Aug 2019 17:47:54 -0400 Subject: [PATCH 1/3] console-kit property wrappers --- Sources/Vapor/Application.swift | 3 +-- Sources/Vapor/Commands/BootCommand.swift | 9 +++---- Sources/Vapor/Commands/RoutesCommand.swift | 9 +++---- Sources/Vapor/Commands/ServeCommand.swift | 24 +++++++++++------- .../Logging/LoggingSystem+Environment.swift | 25 ++++++------------- Sources/Vapor/Services/Environment.swift | 7 +++++- 6 files changed, 37 insertions(+), 40 deletions(-) diff --git a/Sources/Vapor/Application.swift b/Sources/Vapor/Application.swift index 8debb7c027..9f569aa09d 100644 --- a/Sources/Vapor/Application.swift +++ b/Sources/Vapor/Application.swift @@ -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) } diff --git a/Sources/Vapor/Commands/BootCommand.swift b/Sources/Vapor/Commands/BootCommand.swift index 8ed2e33a59..f060db5bfb 100644 --- a/Sources/Vapor/Commands/BootCommand.swift +++ b/Sources/Vapor/Commands/BootCommand.swift @@ -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 { @@ -19,7 +18,7 @@ public final class BootCommand: Command { public init() { } /// See `Command`. - public func run(using context: CommandContext) throws { + public func run(using context: CommandContext, signature: Signature) throws { context.console.success("Done.") } } diff --git a/Sources/Vapor/Commands/RoutesCommand.swift b/Sources/Vapor/Commands/RoutesCommand.swift index 0b3e6106d3..367aa2cc89 100644 --- a/Sources/Vapor/Commands/RoutesCommand.swift +++ b/Sources/Vapor/Commands/RoutesCommand.swift @@ -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 { @@ -32,7 +31,7 @@ public final class RoutesCommand: Command { } /// See `Command`. - public func run(using context: CommandContext) throws { + public func run(using context: CommandContext, signature: Signature) throws { let console = context.console var longestMethod = 0 diff --git a/Sources/Vapor/Commands/ServeCommand.swift b/Sources/Vapor/Commands/ServeCommand.swift index dde89d96fe..98c702893a 100644 --- a/Sources/Vapor/Commands/ServeCommand.swift +++ b/Sources/Vapor/Commands/ServeCommand.swift @@ -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(name: "hostname", short: "H", type: .value, help: "Set the hostname the server will run on.") - public let port = Option(name: "port", short: "p", type: .value, help: "Set the port the server will run on.") - public let bind = Option(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`. @@ -29,14 +35,14 @@ public final class ServeCommand: Command { } /// See `Command`. - public func run(using context: CommandContext) 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 diff --git a/Sources/Vapor/Logging/LoggingSystem+Environment.swift b/Sources/Vapor/Logging/LoggingSystem+Environment.swift index 6e053a742f..30e162635a 100644 --- a/Sources/Vapor/Logging/LoggingSystem+Environment.swift +++ b/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) ) } } @@ -36,17 +39,3 @@ extension Logger.Level: LosslessStringConvertible { } } } - -private extension CommandInput { - mutating func parseOption( - value: Value.Type, - name: String, - short: Character? = nil - ) throws -> Value? - where Value: LosslessStringConvertible - { - let option = Option(name: name, short: short, type: .value, help: "") - return try self.parse(option: option) - .flatMap { Value.init($0) } - } -} diff --git a/Sources/Vapor/Services/Environment.swift b/Sources/Vapor/Services/Environment.swift index 5c4bf94d05..c714e00a72 100644 --- a/Sources/Vapor/Services/Environment.swift +++ b/Sources/Vapor/Services/Environment.swift @@ -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(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 From 99e566e835b763204d06f7e204ec97e6a3ff1f9b Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Tue, 27 Aug 2019 18:08:22 -0400 Subject: [PATCH 2/3] change branch --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 696e1518e5..368ef5e2be 100644 --- a/Package.swift +++ b/Package.swift @@ -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", .branch("pw-name")), // 🔑 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"), From da270ec344086ffc429479139c0da0d149f8cc33 Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Tue, 27 Aug 2019 18:21:41 -0400 Subject: [PATCH 3/3] use alpha tag --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 368ef5e2be..4e4dfc8b2f 100644 --- a/Package.swift +++ b/Package.swift @@ -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", .branch("pw-name")), + .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"),