From 33a51b7344e690eaf63da199eb087b038a0a1b32 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Mon, 27 Mar 2023 20:59:57 +0400 Subject: [PATCH] tart export: make export path optional (#454) --- Sources/tart/Commands/Export.swift | 34 +++++++++++++++++++++++++++--- Sources/tart/Commands/Import.swift | 2 +- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/Sources/tart/Commands/Export.swift b/Sources/tart/Commands/Export.swift index 7fc3c70..f0b5dca 100644 --- a/Sources/tart/Commands/Export.swift +++ b/Sources/tart/Commands/Export.swift @@ -1,16 +1,44 @@ import ArgumentParser +import Foundation struct Export: AsyncParsableCommand { - static var configuration = CommandConfiguration(abstract: "Export VM to a file") + static var configuration = CommandConfiguration(abstract: "Export VM to a compressed .tvm file") @Argument(help: "Source VM name.") var name: String @Argument(help: "Path to the destination file.") - var path: String + var path: String? func run() async throws { + let correctedPath: String + + if let path = path { + correctedPath = path + } else { + correctedPath = "\(name).tvm" + + if FileManager.default.fileExists(atPath: correctedPath) { + while true { + if userWantsOverwrite(correctedPath) { + break + } else { + return + } + } + } + } + print("exporting...") - try VMStorageHelper.open(name).exportToArchive(path: path) + + try VMStorageHelper.open(name).exportToArchive(path: correctedPath) + } + + func userWantsOverwrite(_ filename: String) -> Bool { + print("file \(filename) already exists, are you sure you want to overwrite it? (yes, [no])? ", terminator: "") + + let answer = readLine()! + + return answer == "yes" } } diff --git a/Sources/tart/Commands/Import.swift b/Sources/tart/Commands/Import.swift index 465334b..46a5162 100644 --- a/Sources/tart/Commands/Import.swift +++ b/Sources/tart/Commands/Import.swift @@ -2,7 +2,7 @@ import ArgumentParser import Foundation struct Import: AsyncParsableCommand { - static var configuration = CommandConfiguration(abstract: "Import VM from a file") + static var configuration = CommandConfiguration(abstract: "Import VM from a compressed .tvm file") @Argument(help: "Path to a file created with \"tart export\".") var path: String