aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/ZipArchive/ZipExporter.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-11-15 13:29:49 +0530
committerRoopesh Chander <roop@roopc.net>2018-11-15 13:39:56 +0530
commit1dac181803c5b673bfbc9729281027628c0bb97b (patch)
treeb7070145b20df45ec7cd2ff63528fc08540c0a34 /WireGuard/WireGuard/ZipArchive/ZipExporter.swift
parentExporting: No need to check for duplicate names - we disallow it at creation time itself (diff)
downloadwireguard-apple-1dac181803c5b673bfbc9729281027628c0bb97b.tar.xz
wireguard-apple-1dac181803c5b673bfbc9729281027628c0bb97b.zip
Exporting: Refactor out zip exporting into a separate class
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/ZipArchive/ZipExporter.swift')
-rw-r--r--WireGuard/WireGuard/ZipArchive/ZipExporter.swift28
1 files changed, 28 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift
new file mode 100644
index 0000000..d0cf9a7
--- /dev/null
+++ b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import UIKit
+
+enum ZipExporterError: Error {
+ case noTunnelsToExport
+}
+
+class ZipExporter {
+ static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to destinationURL: URL) throws {
+
+ guard (!tunnelConfigurations.isEmpty) else { throw ZipExporterError.noTunnelsToExport }
+
+ var inputsToArchiver: [(fileName: String, contents: Data)] = []
+
+ var lastTunnelName: String = ""
+ for tunnelConfiguration in tunnelConfigurations {
+ if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) {
+ let name = tunnelConfiguration.interface.name
+ if (name.isEmpty || name == lastTunnelName) { continue }
+ inputsToArchiver.append((fileName: "\(name).conf", contents: contents))
+ lastTunnelName = name
+ }
+ }
+ try ZipArchive.archive(inputs: inputsToArchiver, to: destinationURL)
+ }
+}