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

Fix routes command symbol usage #2366

Merged
merged 6 commits into from Jun 24, 2020
Merged
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
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,26 +25,17 @@ 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 = ""
var column = [route.method.string.consoleText()]
if route.path.isEmpty {
pathText += "/".consoleText(.info)
column.append(pathSeparator)
} else {
column.append(route.path
.map { pathSeparator + $0.consoleText() }
.reduce("".consoleText(), +)
)
}
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)
}
}
var column = [route.method.string.consoleText(), pathText]
if includeDescription {
let desc = route.userInfo["description"]
.flatMap { $0 as? String }
Expand All @@ -56,6 +47,17 @@ public final class RoutesCommand: Command {
}
}

extension PathComponent {
func consoleText() -> ConsoleText {
switch self {
case .constant:
return description.consoleText()
default:
return description.consoleText(.info)
}
}
}

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