@@ -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}
0 commit comments