Skip to content

Commit e8cfcee

Browse files
committed
Refactor config loading, use in use toolchain when linking
1 parent 4995e6b commit e8cfcee

File tree

11 files changed

+20
-28
lines changed

11 files changed

+20
-28
lines changed

Sources/Swiftly/Install.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,7 @@ struct Install: SwiftlyCommand {
8181
}
8282

8383
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
84-
try validateSwiftly(ctx)
85-
86-
var config = try Config.load(ctx)
84+
var config = try validatedConfig(ctx)
8785
let toolchainVersion = try await Self.determineToolchainVersion(ctx, version: self.version, config: &config)
8886

8987
let (postInstallScript, pathChanged) = try await Self.execute(

Sources/Swiftly/Link.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ struct Link: SwiftlyCommand {
2323
}
2424

2525
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
26-
try validateSwiftly(ctx)
27-
28-
var config = try Config.load(ctx)
26+
var config = try validatedConfig(ctx)
2927
let toolchainVersion = try await Install.determineToolchainVersion(
3028
ctx,
31-
version: nil,
29+
version: config.inUse?.name,
3230
config: &config
3331
)
3432

@@ -40,7 +38,11 @@ struct Link: SwiftlyCommand {
4038
)
4139

4240
if pathChanged {
43-
await ctx.print(Messages.refreshShell)
41+
await ctx.print("""
42+
Linked swiftly to \(toolchainVersion.name).
43+
44+
\(Messages.refreshShell)
45+
""")
4446
}
4547
}
4648
}

Sources/Swiftly/List.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ struct List: SwiftlyCommand {
3838
}
3939

4040
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
41-
try validateSwiftly(ctx)
41+
var config = try validatedConfig(ctx)
4242
let selector = try self.toolchainSelector.map { input in
4343
try ToolchainSelector(parsing: input)
4444
}
4545

46-
var config = try Config.load(ctx)
47-
4846
let toolchains = config.listInstalledToolchains(selector: selector).sorted { $0 > $1 }
4947
let (inUse, _) = try await selectToolchain(ctx, config: &config)
5048

Sources/Swiftly/ListAvailable.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,11 @@ struct ListAvailable: SwiftlyCommand {
4444
}
4545

4646
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
47-
try validateSwiftly(ctx)
47+
let config = try validatedConfig(ctx)
4848
let selector = try self.toolchainSelector.map { input in
4949
try ToolchainSelector(parsing: input)
5050
}
5151

52-
let config = try Config.load(ctx)
53-
5452
let tc: [ToolchainVersion]
5553

5654
switch selector {

Sources/Swiftly/Run.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,13 @@ struct Run: SwiftlyCommand {
5858
}
5959

6060
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
61-
try validateSwiftly(ctx)
61+
var config = try validatedConfig(ctx)
6262

6363
// Handle the specific case where help is requested of the run subcommand
6464
if command == ["--help"] {
6565
throw CleanExit.helpRequest(self)
6666
}
6767

68-
var config = try Config.load(ctx)
69-
7068
let (command, selector) = try Self.extractProxyArguments(command: self.command)
7169

7270
let toolchain: ToolchainVersion?

Sources/Swiftly/SelfUpdate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct SelfUpdate: SwiftlyCommand {
2121
}
2222

2323
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
24-
try validateSwiftly(ctx)
24+
try validatedConfig(ctx)
2525

2626
let swiftlyBin = Swiftly.currentPlatform.swiftlyBinDir(ctx).appendingPathComponent("swiftly")
2727
guard FileManager.default.fileExists(atPath: swiftlyBin.path) else {

Sources/Swiftly/Swiftly.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ extension Data {
8282
}
8383

8484
extension SwiftlyCommand {
85-
public mutating func validateSwiftly(_ ctx: SwiftlyCoreContext) throws {
85+
@discardableResult
86+
public mutating func validatedConfig(_ ctx: SwiftlyCoreContext) throws -> Config {
8687
for requiredDir in Swiftly.requiredDirectories(ctx) {
8788
guard requiredDir.fileExists() else {
8889
do {
@@ -94,7 +95,7 @@ extension SwiftlyCommand {
9495
}
9596
}
9697

97-
// Verify that the configuration exists and can be loaded
98-
_ = try Config.load(ctx)
98+
// Verifies that the configuration exists and can be loaded
99+
return try Config.load(ctx)
99100
}
100101
}

Sources/Swiftly/Uninstall.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ struct Uninstall: SwiftlyCommand {
4848
}
4949

5050
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
51-
try validateSwiftly(ctx)
52-
let startingConfig = try Config.load(ctx)
51+
let startingConfig = try validatedConfig(ctx)
5352

5453
let toolchains: [ToolchainVersion]
5554
if self.toolchain == "all" {

Sources/Swiftly/Unlink.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct Unlink: SwiftlyCommand {
2525
}
2626

2727
mutating func run(_ ctx: SwiftlyCoreContext) async throws {
28-
try validateSwiftly(ctx)
28+
try validatedConfig(ctx)
2929

3030
var pathChanged = false
3131
if let proxyTo = try? Swiftly.currentPlatform.findSwiftlyBin(ctx) {

Sources/Swiftly/Update.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ struct Update: SwiftlyCommand {
8282
}
8383

8484
public mutating func run(_ ctx: SwiftlyCoreContext) async throws {
85-
try validateSwiftly(ctx)
86-
var config = try Config.load(ctx)
85+
var config = try validatedConfig(ctx)
8786

8887
guard let parameters = try await self.resolveUpdateParameters(ctx, &config) else {
8988
if let toolchain = self.toolchain {

0 commit comments

Comments
 (0)