mirror of
https://github.com/tsolomko/SWCompression.git
synced 2026-06-23 14:56:41 +00:00
ab7c65249d
This protocol represents common functionality for container-related commands (7z and zip). Extension to this protocol implements execute() function which is the same for both 7z and Zip commands.
52 lines
1.3 KiB
Swift
52 lines
1.3 KiB
Swift
// Copyright (c) 2018 Timofey Solomko
|
|
// Licensed under MIT License
|
|
//
|
|
// See LICENSE for license information
|
|
|
|
import Foundation
|
|
import SWCompression
|
|
import SwiftCLI
|
|
|
|
protocol ContainerCommand: Command {
|
|
|
|
associatedtype Entry: ContainerEntry
|
|
|
|
var info: Flag { get }
|
|
var extract: Key<String> { get}
|
|
var verbose: Flag { get }
|
|
|
|
var archive: Parameter { get }
|
|
|
|
var openFunction: (Data) throws -> [Entry] { get }
|
|
var infoFunction: (Data) throws -> [Entry.Info] { get }
|
|
|
|
}
|
|
|
|
extension ContainerCommand {
|
|
|
|
var optionGroups: [OptionGroup] {
|
|
let actions = OptionGroup(options: [info, extract], restriction: .exactlyOne)
|
|
return [actions]
|
|
}
|
|
|
|
func execute() throws {
|
|
let fileData = try Data(contentsOf: URL(fileURLWithPath: self.archive.value),
|
|
options: .mappedIfSafe)
|
|
if info.value {
|
|
let entries = try infoFunction(fileData)
|
|
swcomp.printInfo(entries)
|
|
} else {
|
|
let outputPath = self.extract.value!
|
|
|
|
if try !isValidOutputDirectory(outputPath, create: true) {
|
|
print("ERROR: Specified path already exists and is not a directory.")
|
|
exit(1)
|
|
}
|
|
|
|
let entries = try openFunction(fileData)
|
|
try swcomp.write(entries, outputPath, verbose.value)
|
|
}
|
|
}
|
|
|
|
}
|