Skip to content

Commit 38c0115

Browse files
authored
Merge pull request #61 from Automattic/fix/authorized-key-sync
Fix Authorized Key sync
2 parents f3a63ff + 5276fa8 commit 38c0115

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Sources/hostmgr/commands/sync/SyncAuthorizedKeysCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct SyncAuthorizedKeysCommand: AsyncParsableCommand, FollowsCommandPolicies {
3737
bucket: Configuration.shared.authorizedKeysBucket,
3838
region: Configuration.shared.authorizedKeysRegion,
3939
credentials: credentials,
40-
endpoint: .accelerated
40+
endpoint: .default // Secrets doesn't use the accelerated endpoint
4141
)
4242

4343
guard let object = try await s3Manager.lookupObject(atPath: Constants.s3Key) else {

Sources/libhostmgr/Model/RemoteVMRepository.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@ public struct RemoteVMRepository {
7474
try await self.s3Manager.download(
7575
key: image.checksumObject.key,
7676
to: destinationDirectory.appendingPathComponent(image.checksumFileName),
77+
replacingExistingFile: false,
7778
progressCallback: nil)
7879

7980
let imageFileDestination = destinationDirectory.appendingPathComponent(image.checksumFileName)
8081

8182
try await self.s3Manager.download(
8283
key: image.imageObject.key,
8384
to: destinationDirectory.appendingPathComponent(image.checksumFileName),
85+
replacingExistingFile: false,
8486
progressCallback: progressCallback
8587
)
8688

Sources/libhostmgr/S3Manager.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public protocol S3ManagerProtocol {
1111
func download(
1212
key: String,
1313
to destination: URL,
14+
replacingExistingFile shouldReplaceExistingFile: Bool,
1415
progressCallback: FileTransferProgressCallback?
1516
) async throws
1617

@@ -19,6 +20,10 @@ public protocol S3ManagerProtocol {
1920

2021
public struct S3Manager: S3ManagerProtocol {
2122

23+
enum Errors: Error {
24+
case fileExistsAtDestination
25+
}
26+
2227
private let bucket: String
2328
private let region: String
2429

@@ -41,13 +46,23 @@ public struct S3Manager: S3ManagerProtocol {
4146
public func download(
4247
key: String,
4348
to destination: URL,
49+
replacingExistingFile shouldReplaceExistingFile: Bool = true,
4450
progressCallback: FileTransferProgressCallback?
4551
) async throws {
4652
let tempUrl = try await s3Client.download(
4753
objectWithKey: key,
4854
inBucket: self.bucket,
4955
progressCallback: progressCallback
5056
)
57+
58+
if FileManager.default.fileExists(at: destination) {
59+
if shouldReplaceExistingFile {
60+
try FileManager.default.removeItem(at: destination)
61+
} else {
62+
throw Errors.fileExistsAtDestination
63+
}
64+
}
65+
5166
try FileManager.default.moveItem(at: tempUrl, to: destination)
5267
}
5368

0 commit comments

Comments
 (0)