-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateCommand.swift
More file actions
76 lines (65 loc) · 1.89 KB
/
Copy pathUpdateCommand.swift
File metadata and controls
76 lines (65 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import ArgumentParser
import Dependencies
import Foundation
import FXCodexClient
extension AppCommand {
internal struct UpdateCommand: AsyncParsableCommand {
internal enum Channel: String, EnumerableFlag {
case patch
case minor
case major
case latest
var value: UpdateChannel {
switch self {
case .patch: .patch
case .minor: .minor
case .major: .major
case .latest: .latest
}
}
}
internal static let configuration: CommandConfiguration = .init(
commandName: "update",
abstract: "Update fxcodex from GitHub Releases."
)
@Flag(help: "Update channel. Defaults to --patch.")
internal var channel: Channel = .patch
@Flag(
inversion: .prefixedNo,
exclusivity: .chooseLast,
help: "Print a versioned machine-readable JSON response. Defaults from FXCODEX_JSON."
)
internal var json: Bool?
internal init() {}
internal func run() async throws {
try await self.run(executableURL: currentExecutableURL())
}
internal func run(executableURL: URL) async throws {
guard !isHomebrewManagedExecutable(executableURL)
else { throw FXCodexError.homebrewManagedUpdate }
@Dependency(\.fxCodexClient) var client: FXCodexClient
guard let currentVersion = SemanticVersion(AppCommand.version)
else { throw ValidationError("fxcodex has an invalid embedded version.") }
let result: UpdateResult = try await client.update(
currentVersion,
self.channel.value,
executableURL
)
if machineOutputRequested(self.json) {
try printMachineResponse(result)
return
}
let reporter: TerminalReporter = await .init()
switch result.outcome {
case .updated:
await reporter.success(
"Updated fxcodex from \(result.previousVersion) to \(result.version)."
)
case .alreadyCurrent:
await reporter.info(
"fxcodex \(result.version) is already current for the \(self.channel.rawValue) channel."
)
}
}
}
}