|
1 | 1 | /*
|
2 | 2 | This source file is part of the Swift.org open source project
|
3 | 3 |
|
4 |
| - Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 4 | + Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors |
5 | 5 | Licensed under Apache License v2.0 with Runtime Library Exception
|
6 | 6 |
|
7 | 7 | See http://swift.org/LICENSE.txt for license information
|
@@ -497,35 +497,58 @@ public class SwiftTool {
|
497 | 497 |
|
498 | 498 | func getAuthorizationProvider() throws -> AuthorizationProvider? {
|
499 | 499 | var providers = [AuthorizationProvider]()
|
500 |
| - // netrc file has higher specificity than keychain so use it first |
501 |
| - if let workspaceNetrc = try self.getNetrcConfig()?.get() { |
502 |
| - providers.append(workspaceNetrc) |
503 |
| - } |
504 | 500 |
|
505 |
| - // TODO: add --no-keychain option to allow opt-out |
506 |
| -//#if canImport(Security) |
507 |
| -// providers.append(KeychainAuthorizationProvider(observabilityScope: self.observabilityScope)) |
508 |
| -//#endif |
| 501 | + // netrc has higher specificity than keychain so use it first |
| 502 | + try providers.append(contentsOf: self.getNetrcAuthorizationProviders()) |
| 503 | + |
| 504 | +#if canImport(Security) |
| 505 | + if self.options.keychain { |
| 506 | + providers.append(KeychainAuthorizationProvider(observabilityScope: self.observabilityScope)) |
| 507 | + } |
| 508 | +#endif |
509 | 509 |
|
510 | 510 | return providers.isEmpty ? nil : CompositeAuthorizationProvider(providers, observabilityScope: self.observabilityScope)
|
511 | 511 | }
|
512 | 512 |
|
513 |
| - func getNetrcConfig() -> Workspace.Configuration.Netrc? { |
514 |
| - guard options.netrc else { return nil } |
| 513 | + func getNetrcAuthorizationProviders() throws -> [NetrcAuthorizationProvider] { |
| 514 | + guard options.netrc else { return [] } |
515 | 515 |
|
| 516 | + var providers = [NetrcAuthorizationProvider]() |
| 517 | + |
| 518 | + // Use custom .netrc file if specified, otherwise look for it within workspace and user's home directory. |
516 | 519 | if let configuredPath = options.netrcFilePath {
|
517 | 520 | guard localFileSystem.exists(configuredPath) else {
|
518 | 521 | self.observabilityScope.emit(error: "Did not find .netrc file at \(configuredPath).")
|
519 |
| - return nil |
| 522 | + return providers |
520 | 523 | }
|
521 | 524 |
|
522 |
| - return .init(path: configuredPath, fileSystem: localFileSystem) |
| 525 | + providers.append(try NetrcAuthorizationProvider(path: configuredPath, fileSystem: localFileSystem)) |
523 | 526 | } else {
|
524 |
| - let defaultPath = localFileSystem.homeDirectory.appending(component: ".netrc") |
525 |
| - guard localFileSystem.exists(defaultPath) else { return nil } |
| 527 | + // User didn't tell us to use these .netrc files so be more lenient with errors |
| 528 | + func loadNetrcNoThrows(at path: AbsolutePath) -> NetrcAuthorizationProvider? { |
| 529 | + guard localFileSystem.exists(path) else { return nil } |
| 530 | + |
| 531 | + do { |
| 532 | + return try NetrcAuthorizationProvider(path: path, fileSystem: localFileSystem) |
| 533 | + } catch { |
| 534 | + self.observabilityScope.emit(warning: "Failed to load .netrc file at \(path). Error: \(error)") |
| 535 | + return nil |
| 536 | + } |
| 537 | + } |
| 538 | + |
| 539 | + // Workspace's .netrc file should be consulted before user-global file |
| 540 | + if let workspaceNetrcPath = try? Workspace.DefaultLocations.netrcFile(forRootPackage: self.getPackageRoot()), |
| 541 | + let workspaceNetrcProvider = loadNetrcNoThrows(at: workspaceNetrcPath) { |
| 542 | + providers.append(workspaceNetrcProvider) |
| 543 | + } |
526 | 544 |
|
527 |
| - return .init(path: defaultPath, fileSystem: localFileSystem) |
| 545 | + let userNetrcPath = localFileSystem.homeDirectory.appending(component: ".netrc") |
| 546 | + if let userNetrcProvider = loadNetrcNoThrows(at: userNetrcPath) { |
| 547 | + providers.append(userNetrcProvider) |
| 548 | + } |
528 | 549 | }
|
| 550 | + |
| 551 | + return providers |
529 | 552 | }
|
530 | 553 |
|
531 | 554 | private func getSharedCacheDirectory() throws -> AbsolutePath? {
|
|
0 commit comments