feat: Create backups of configuration when the force option is not used.
Some checks failed
CI / Run Tests (push) Failing after 2m16s
Create and publish a Docker image / build-and-push-image (push) Has been cancelled

This commit is contained in:
2024-12-17 14:38:52 -05:00
parent 54c07886ad
commit 558054464c
4 changed files with 29 additions and 8 deletions

View File

@@ -231,10 +231,10 @@ struct LiveConfigurationClient {
let fileUrl = URL(filePath: expandedPath)
if !options.force {
guard !fileManager.fileExists(fileUrl) else {
throw ConfigurationError.fileExists(path: file.path)
}
let exists = fileManager.fileExists(fileUrl)
if !options.force, exists {
try await createBackup(file.url)
}
let fileDirectory = fileUrl.deletingLastPathComponent()
@@ -245,6 +245,8 @@ struct LiveConfigurationClient {
try await fileManager.createDirectory(fileDirectory)
}
// TODO: The hpa file needs to be copied somewhere on the system during install and
// not use bundle, as it only works if the tool was built on the users system.
if case .toml = file {
// In the case of toml, we copy the internal resource that includes
// usage comments in the file.
@@ -290,9 +292,7 @@ struct LiveConfigurationClient {
let exists = fileManager.fileExists(file.url)
if !force, exists {
let backupUrl = file.url.appendingPathExtension(".back")
logger.warning("File exists, creating a backup of the existing file at: \(backupUrl.cleanFilePath)")
try await fileManager.copy(file.url, backupUrl)
try await createBackup(file.url)
}
let data: Data
@@ -308,6 +308,13 @@ struct LiveConfigurationClient {
try await fileManager.write(data, file.url)
}
private func createBackup(_ url: URL) async throws {
let backupUrl = url.appendingPathExtension("back")
logger.warning("File exists, creating a backup of the existing file at: \(backupUrl.cleanFilePath)")
try await fileManager.copy(url, backupUrl)
try await fileManager.delete(url)
}
private func findInDirectory(_ directory: URL) async -> File? {
for file in validFileNames {
let url = directory.appending(path: file)