Skip to content

Commit fb59fec

Browse files
committed
Improve error handling for 'package-registry login' command
Motivation: When running `login` with `--netrc` and `--netrc-file` options and the custom netrc file doesn't exist, the command fails immediately with file not found error: ``` swift-package-registry --netrc --netrc-file ./netrc-test login Error: Did not find netrc file at /Users/appleseed/netrc-test. ``` Which can be confusing. Modifications: Rearrange logic so that we initialize `AuthorizationProvider` first thing and display better error messages to user. ``` Error: credential store is invalid: did not find netrc file at /Users/appleseed/netrc-test ```
1 parent 65453fb commit fb59fec

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

Sources/PackageRegistryTool/PackageRegistryTool+Auth.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ extension SwiftPackageRegistryTool {
8888
private static let PLACEHOLDER_TOKEN_USER = "token"
8989

9090
func run(_ swiftTool: SwiftTool) throws {
91+
// We need to be able to read/write credentials
92+
// Make sure credentials store is available before proceeding
93+
let authorizationProvider: AuthorizationProvider?
94+
do {
95+
authorizationProvider = try swiftTool.getRegistryAuthorizationProvider()
96+
} catch {
97+
throw ValidationError.invalidCredentialStore(error)
98+
}
99+
100+
guard let authorizationProvider = authorizationProvider else {
101+
throw ValidationError.unknownCredentialStore
102+
}
103+
91104
let configuration = try getRegistriesConfig(swiftTool)
92105

93106
// compute and validate registry URL
@@ -101,11 +114,6 @@ extension SwiftPackageRegistryTool {
101114
throw ValidationError.invalidURL(registryURL)
102115
}
103116

104-
// We need to be able to read/write credentials
105-
guard let authorizationProvider = try swiftTool.getRegistryAuthorizationProvider() else {
106-
throw ValidationError.unknownCredentialStore
107-
}
108-
109117
let authenticationType: RegistryConfiguration.AuthenticationType
110118
let storeUsername: String
111119
let storePassword: String

Sources/PackageRegistryTool/PackageRegistryTool.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
2727
abstract: "Interact with package registry and manage related configuration",
2828
discussion: "SEE ALSO: swift package",
2929
version: SwiftVersion.current.completeDisplayString,
30-
subcommands:[
30+
subcommands: [
3131
Set.self,
3232
Unset.self,
3333
Login.self,
@@ -137,6 +137,7 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
137137
case invalidPackageIdentity(PackageIdentity)
138138
case unknownRegistry
139139
case unknownCredentialStore
140+
case invalidCredentialStore(Error)
140141
}
141142

142143
static func getRegistriesConfig(_ swiftTool: SwiftTool) throws -> Workspace.Configuration.Registries {
@@ -172,13 +173,15 @@ extension SwiftPackageRegistryTool.ValidationError: CustomStringConvertible {
172173
var description: String {
173174
switch self {
174175
case .invalidURL(let url):
175-
return "Invalid URL: \(url)"
176+
return "invalid URL: \(url)"
176177
case .invalidPackageIdentity(let identity):
177-
return "Invalid package identifier '\(identity)'"
178+
return "invalid package identifier '\(identity)'"
178179
case .unknownRegistry:
179-
return "Unknown registry, is one configured?"
180+
return "unknown registry, is one configured?"
180181
case .unknownCredentialStore:
181-
return "No credential store available"
182+
return "no credential store available"
183+
case .invalidCredentialStore(let error):
184+
return "credential store is invalid: \(error)"
182185
}
183186
}
184187
}

Sources/Workspace/Workspace+Configuration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ extension Workspace.Configuration {
354354
switch self.netrc {
355355
case .custom(let path):
356356
guard fileSystem.exists(path) else {
357-
throw StringError("Did not find netrc file at \(path).")
357+
throw StringError("did not find netrc file at \(path)")
358358
}
359359
providers.append(try NetrcAuthorizationProvider(path: path, fileSystem: fileSystem))
360360
case .user:

0 commit comments

Comments
 (0)