Skip to content

Add --print-location flag to swiftly list subcommand #300

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

Draft
wants to merge 2 commits into
base: release/1.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Documentation/SwiftlyDocs.docc/swiftly-cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ Finally, all installed toolchains can be uninstalled by specifying 'all':
List installed toolchains.

```
swiftly list [<toolchain-selector>] [--version] [--help]
swiftly list [<toolchain-selector>] [--print-location] [--version] [--help]
```

**toolchain-selector:**
Expand All @@ -307,6 +307,11 @@ The installed snapshots for a given devlopment branch can be listed by specifyin
$ swiftly list 5.7-snapshot


**--print-location:**

*Print the location of the toolchains prefixed with the version <x.y.z> - <path/to/toolchain>.*


**--version:**

*Show the version.*
Expand Down
11 changes: 11 additions & 0 deletions Sources/Swiftly/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ struct List: SwiftlyCommand {
))
var toolchainSelector: String?

@Flag(name: .shortAndLong, help: "Print the location of the toolchains prefixed with the version <x.y.z> - <path/to/toolchain>.")
var printLocation: Bool = false

internal mutating func run() async throws {
try validateSwiftly()
let selector = try self.toolchainSelector.map { input in
Expand All @@ -42,6 +45,14 @@ struct List: SwiftlyCommand {
var config = try Config.load()

let toolchains = config.listInstalledToolchains(selector: selector).sorted { $0 > $1 }

guard !self.printLocation else {
for toolchain in toolchains {
SwiftlyCore.print("\(toolchain.name) - \(Swiftly.currentPlatform.findToolchainLocation(toolchain).path)")
}

return
}
let (inUse, _) = try await selectToolchain(config: &config)

let printToolchain = { (toolchain: ToolchainVersion) in
Expand Down
16 changes: 15 additions & 1 deletion Tests/SwiftlyTests/ListTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class ListTests: SwiftlyTests {
}

var list = try self.parseCommand(List.self, args)
let output = try await list.runWithMockedIO()
var output = try await list.runWithMockedIO()

let parsedToolchains = output.compactMap { outputLine in
Self.allToolchains.first {
Expand All @@ -56,6 +56,20 @@ final class ListTests: SwiftlyTests {
throw SwiftlyTestError(message: "unexpected listed toolchains in \(output)")
}

// Check that the print location flag runs as expected
list = try self.parseCommand(List.self, args + ["--print-location"])
output = try await list.runWithMockedIO()

let toolchainLocations = output.compactMap { outputLine in
Self.allToolchains.first {
outputLine.contains("\($0.name) - \(Swiftly.currentPlatform.findToolchainLocation($0).path)")
}
}

guard toolchainLocations.count == parsedToolchains.count else {
throw SwiftlyTestError(message: "list --print-location didn't yield the same toolchains as plain list")
}

return parsedToolchains
}

Expand Down