Skip to content

Commit

Permalink
Update RoutesCommand to align with RoutingKit
Browse files Browse the repository at this point in the history
  • Loading branch information
stevapple committed May 25, 2020
1 parent 74bbf36 commit c60a484
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions Sources/Vapor/Commands/RoutesCommand.swift
Expand Up @@ -10,7 +10,7 @@
/// A colon preceding a path component indicates a variable parameter. A colon with no text following
/// is a parameter whose result will be discarded.
///
/// An asterisk indicates a catch-all. Any path components after a catch-all will be discarded and ignored.
/// The path will be displayed with the same syntax that is used to register a route.
public final class RoutesCommand: Command {
public struct Signature: CommandSignature {
public init() { }
Expand All @@ -25,25 +25,16 @@ public final class RoutesCommand: Command {
public func run(using context: CommandContext, signature: Signature) throws {
let routes = context.application.routes
let includeDescription = !routes.all.filter { $0.userInfo["description"] != nil }.isEmpty
let pathSeparator = "/".consoleText()
context.console.outputASCIITable(routes.all.map { route -> [ConsoleText] in
var pathText: ConsoleText = ""
if route.path.isEmpty {
pathText += "/".consoleText(.info)
}
for path in route.path {
pathText += "/".consoleText(.info)
switch path {
case .constant(let string):
pathText += string.consoleText()
case .parameter(let name):
pathText += ":".consoleText(.info)
pathText += name.consoleText()
case .anything:
pathText += ":".consoleText(.info)
case .catchall:
pathText += "*".consoleText(.info)
let pathText = pathSeparator + route.path.map {
switch $0 {
case .constant:
return $0.description.consoleText()
default:
return $0.description.consoleText(.info)
}
}
}.joined(separator: pathSeparator)
var column = [route.method.string.consoleText(), pathText]
if includeDescription {
let desc = route.userInfo["description"]
Expand All @@ -56,6 +47,17 @@ public final class RoutesCommand: Command {
}
}

extension Collection where Element == ConsoleText {
func joined(separator: ConsoleText) -> ConsoleText {
guard let result: ConsoleText = self.first else {
return ""
}
return self.dropFirst().reduce(into: result) {
$0 += separator + $1
}
}
}

extension Console {
func outputASCIITable(_ rows: [[ConsoleText]]) {
var columnWidths: [Int] = []
Expand Down

0 comments on commit c60a484

Please sign in to comment.