mirror of
https://github.com/softinio/Fishee.git
synced 2025-02-22 21:46:05 -08:00
complete backup functionality
This commit is contained in:
parent
9dd3399d5c
commit
02f14b70bf
3 changed files with 17 additions and 10 deletions
|
@ -86,7 +86,6 @@ struct Fishee: ParsableCommand {
|
||||||
let result = writeFishHistory(
|
let result = writeFishHistory(
|
||||||
to: writePath,
|
to: writePath,
|
||||||
history: finalHistory,
|
history: finalHistory,
|
||||||
historyFileLocation: fishHistoryLocation?.path,
|
|
||||||
backup: backup
|
backup: backup
|
||||||
)
|
)
|
||||||
if result {
|
if result {
|
||||||
|
|
|
@ -35,25 +35,25 @@ func backupHistory(_ path: String) -> Bool {
|
||||||
let newFileURL = directory.appendingPathComponent(newFileName).appendingPathExtension(fileExtension)
|
let newFileURL = directory.appendingPathComponent(newFileName).appendingPathExtension(fileExtension)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
try? fileManager.removeItem(at: newFileURL)
|
||||||
try fileManager.copyItem(at: fileURL, to: newFileURL)
|
try fileManager.copyItem(at: fileURL, to: newFileURL)
|
||||||
print("File duplicated successfully to: \(newFileURL.path)")
|
print("File duplicated successfully to: \(newFileURL.path)")
|
||||||
return true
|
return true
|
||||||
} catch {
|
} catch {
|
||||||
|
print("error making a backup of \(path), got error: \(error)")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func writeFishHistory(to path: String, history: [FishHistoryEntry], historyFileLocation: String?, backup: Bool = true) -> Bool {
|
func writeFishHistory(to path: String, history: [FishHistoryEntry], backup: Bool = true) -> Bool {
|
||||||
var output = ""
|
var output = ""
|
||||||
|
|
||||||
if backup {
|
if backup {
|
||||||
if let backupFile = historyFileLocation {
|
let result = backupHistory(path)
|
||||||
let result = backupHistory(backupFile)
|
if !result {
|
||||||
if !result {
|
print("Failed to backup \(path) so aborting!")
|
||||||
print("Failed to backup \(backupFile) so aborting!")
|
return false
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ final class ParserTests {
|
||||||
"myfile.txt"
|
"myfile.txt"
|
||||||
)
|
)
|
||||||
let filePathforFileBackupTest = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask)[0].appendingPathComponent(
|
let filePathforFileBackupTest = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask)[0].appendingPathComponent(
|
||||||
"myfile.txt_copy"
|
"myfile_copy.txt"
|
||||||
)
|
)
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
|
@ -51,7 +51,7 @@ final class ParserTests {
|
||||||
let written = writeFishHistory(
|
let written = writeFishHistory(
|
||||||
to: filePathforWriteTest.path,
|
to: filePathforWriteTest.path,
|
||||||
history: [historyItem],
|
history: [historyItem],
|
||||||
historyFileLocation: fishHistoryFile
|
backup: false
|
||||||
)
|
)
|
||||||
#expect(written)
|
#expect(written)
|
||||||
|
|
||||||
|
@ -64,6 +64,14 @@ final class ParserTests {
|
||||||
|
|
||||||
"""
|
"""
|
||||||
#expect(fileContent == expectedEntry)
|
#expect(fileContent == expectedEntry)
|
||||||
|
|
||||||
|
// confirm backup functionality is working
|
||||||
|
let write_again = writeFishHistory(
|
||||||
|
to: filePathforWriteTest.path,
|
||||||
|
history: [historyItem],
|
||||||
|
backup: true
|
||||||
|
)
|
||||||
|
#expect(write_again)
|
||||||
#expect(FileManager.default.fileExists(atPath: filePathforFileBackupTest.path))
|
#expect(FileManager.default.fileExists(atPath: filePathforFileBackupTest.path))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue