Skip to content
This repository was archived by the owner on Apr 14, 2025. It is now read-only.

Commit c3a0f21

Browse files
committed
adding SFTP/File upload
1 parent 13e1f32 commit c3a0f21

File tree

6 files changed

+123
-6
lines changed

6 files changed

+123
-6
lines changed

Package.resolved

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let package = Package(
1212
],
1313
dependencies: [
1414
.package(url: "https://github.com/apple/swift-nio.git", from: "2.3.0"),
15-
.package(url: "https://github.com/jakeheis/Shout.git", from: "0.5.0"),
15+
.package(url: "https://github.com/Einstore/Shout.git", from: "0.5.1"),
1616
.package(url: "https://github.com/kareman/SwiftShell.git", from: "5.0.0")
1717
],
1818
targets: [

Sources/ShellKit/Executors/Executor.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,20 @@ public protocol Executor {
3434
/// - Parameter path: Path
3535
func cd(path: String) -> EventLoopFuture<Void>
3636

37+
38+
/// Upload a file
39+
/// - Parameter file: Path to a local file
40+
/// - Parameter to: Destination path (including filename)
41+
func upload(file: String, to: String) -> EventLoopFuture<Void>
42+
43+
/// Upload data as a file
44+
/// - Parameter data: Path to a local file
45+
/// - Parameter to: Destination path (including filename)
46+
func upload(data: Data, to: String) -> EventLoopFuture<Void>
47+
48+
/// Upload string as a file
49+
/// - Parameter string: Path to a local file
50+
/// - Parameter to: Destination path (including filename)
51+
func upload(string: String, to: String) -> EventLoopFuture<Void>
52+
3753
}

Sources/ShellKit/Executors/LocalExecutor.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,42 @@ public class LocalExecutor: Executor {
131131
return eventLoop.makeSucceededFuture(Void())
132132
}
133133

134+
/// Upload string as a file
135+
/// - Parameter string: Path to a local file
136+
/// - Parameter to: Destination path (including filename)
137+
public func upload(string: String, to path: String) -> EventLoopFuture<Void> {
138+
guard let data = string.data(using: .utf8) else {
139+
return eventLoop.makeFailedFuture(Shell.Error.unableToConvertStringToData)
140+
}
141+
return upload(data: data, to: path)
142+
}
143+
144+
/// Upload data as a file
145+
/// - Parameter data: Path to a local file
146+
/// - Parameter to: Destination path (including filename)
147+
public func upload(data: Data, to path: String) -> EventLoopFuture<Void> {
148+
let promise = eventLoop.makePromise(of: Void.self)
149+
DispatchQueue.global(qos: .background).async {
150+
do {
151+
try data.write(to: URL(fileURLWithPath: path))
152+
promise.succeed(Void())
153+
} catch {
154+
promise.fail(error)
155+
}
156+
}
157+
return promise.futureResult
158+
}
159+
160+
/// Upload a file
161+
/// - Parameter file: Path to a local file
162+
/// - Parameter to: Destination path (including filename)
163+
public func upload(file: String, to path: String) -> EventLoopFuture<Void> {
164+
do {
165+
let data = try Data(contentsOf: URL(fileURLWithPath: path))
166+
return upload(data: data, to: path)
167+
} catch {
168+
return eventLoop.makeFailedFuture(error)
169+
}
170+
}
171+
134172
}

Sources/ShellKit/Executors/SSHExecutor.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,43 @@ public class SSHExecutor: Executor {
8787
return run(bash: "cd \(path.quoteEscape)").void()
8888
}
8989

90+
/// Upload string as a file
91+
/// - Parameter string: Path to a local file
92+
/// - Parameter to: Destination path (including filename)
93+
public func upload(string: String, to path: String) -> EventLoopFuture<Void> {
94+
guard let data = string.data(using: .utf8) else {
95+
return eventLoop.makeFailedFuture(Shell.Error.unableToConvertStringToData)
96+
}
97+
return upload(data: data, to: path)
98+
}
99+
100+
/// Upload data as a file
101+
/// - Parameter data: Path to a local file
102+
/// - Parameter to: Destination path (including filename)
103+
public func upload(data: Data, to path: String) -> EventLoopFuture<Void> {
104+
let promise = eventLoop.makePromise(of: Void.self)
105+
DispatchQueue.global(qos: .background).async {
106+
do {
107+
let sftp = try self.ssh.openSftp()
108+
try sftp.upload(data: data, remotePath: path)
109+
promise.succeed(Void())
110+
} catch {
111+
promise.fail(error)
112+
}
113+
}
114+
return promise.futureResult
115+
}
116+
117+
/// Upload a file
118+
/// - Parameter file: Path to a local file
119+
/// - Parameter to: Destination path (including filename)
120+
public func upload(file: String, to path: String) -> EventLoopFuture<Void> {
121+
do {
122+
let data = try Data(contentsOf: URL(fileURLWithPath: path))
123+
return upload(data: data, to: path)
124+
} catch {
125+
return eventLoop.makeFailedFuture(error)
126+
}
127+
}
128+
90129
}

Sources/ShellKit/Shell.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class Shell: Executor {
1818
/// Bad exit code error
1919
case badExitCode(command: String, exit: Int32, output: String)
2020

21+
/// Umable to convert string to `.utf8` data
22+
case unableToConvertStringToData
23+
2124
}
2225

2326
/// Connection type
@@ -140,4 +143,25 @@ public class Shell: Executor {
140143
return executor.cd(path: path)
141144
}
142145

146+
/// Upload string as a file
147+
/// - Parameter string: Path to a local file
148+
/// - Parameter to: Destination path (including filename)
149+
public func upload(string: String, to path: String) -> EventLoopFuture<Void> {
150+
return executor.upload(file: string, to: path)
151+
}
152+
153+
/// Upload data as a file
154+
/// - Parameter data: Path to a local file
155+
/// - Parameter to: Destination path (including filename)
156+
public func upload(data: Data, to path: String) -> EventLoopFuture<Void> {
157+
return upload(data: data, to: path)
158+
}
159+
160+
/// Upload a file
161+
/// - Parameter file: Path to a local file
162+
/// - Parameter to: Destination path (including filename)
163+
public func upload(file: String, to path: String) -> EventLoopFuture<Void> {
164+
return upload(file: file, to: path)
165+
}
166+
143167
}

0 commit comments

Comments
 (0)