Skip to content

Commit

Permalink
Merge pull request #74 from steelbrain/steelbrain/options-support
Browse files Browse the repository at this point in the history
Add options support in exec
  • Loading branch information
steelbrain authored Apr 2, 2017
2 parents 04a8072 + 3388e0f commit d9c7be4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#### 4.2.0

- Fix a typo in README
- Add support for passing direct options to `ssh2.exec`

#### 4.1.0

- Add sftp `opts` support in `getFile`, `putFile`, `putFiles` and `putDirectory`
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ ssh.connect({
console.log('STDERR: ' + result.stderr)
})
// Command with escaped params
ssh.exec('hh_client', ['--json'], { cwd: '/var/www', stream: 'stdout' }).then(function(result) {
ssh.exec('hh_client', ['--json'], { cwd: '/var/www', stream: 'stdout', options: { pty: true } }).then(function(result) {
console.log('STDOUT: ' + result)
})
})
Expand All @@ -93,8 +93,8 @@ class SSH{
requestSFTP(): Promise<SSH2SFTP>
requestShell(): Promise<SSH2Shell>
mkdir(path: string): Promise<string>
exec(command: string, parameters: Array<string>, options: { cwd?: string, stdin?: string, stream?: 'stdout' | 'stderr', 'both' } = {}): Promise<Object | string>
execCommand(command: string, options: { cwd: string, stdin: string } = {}): Promise<{ stdout: string, stderr: string, signal: ?string, code: number }>
exec(command: string, parameters: Array<string>, options: { cwd?: string, options?: Object, stdin?: string, stream?: 'stdout' | 'stderr', 'both' } = {}): Promise<Object | string>
execCommand(command: string, options: { cwd: string, stdin: string } = {}): Promise<{ stdout: string, options?: Object, stderr: string, signal: ?string, code: number }>
putFile(localFile: string, remoteFile: string, sftp: ?Object = null, opts: ?Object = null): Promise<void>
getFile(localFile: string, remoteFile: string, sftp: ?Object = null, opts: ?Object = null): Promise<void>
putFiles(files: Array<{ local: string, remote: string }>, sftp: ?Object = null, maxAtOnce: number = 5, opts: ?Object = null): Promise<void>
Expand Down
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ class SSH {
}
}
}
async exec(command: string, parameters: Array<string> = [], options: { cwd?: string, stdin?: string, stream?: string } = {}): Promise<string | Object> {
async exec(command: string, parameters: Array<string> = [], options: { cwd?: string, stdin?: string, stream?: string, options?: Object } = {}): Promise<string | Object> {
invariant(this.connection, 'Not connected to server')
invariant(typeof options === 'object' && options, 'options must be an Object')
invariant(!options.cwd || typeof options.cwd === 'string', 'options.cwd must be a string')
invariant(!options.stdin || typeof options.stdin === 'string', 'options.stdin must be a string')
invariant(!options.stream || ['stdout', 'stderr', 'both'].indexOf(options.stream) !== -1, 'options.stream must be among "stdout", "stderr" and "both"')
invariant(!options.options || typeof options.options === 'object', 'options.options must be an object')

const output = await this.execCommand([command].concat(shellEscape(parameters)).join(' '), options)
if (!options.stream || options.stream === 'stdout') {
if (output.stderr) {
Expand All @@ -93,13 +95,14 @@ class SSH {
}
return output
}
async execCommand(givenCommand: string, options: { cwd?: string, stdin?: string } = {}): Promise<{ stdout: string, stderr: string, code: number, signal: ?string }> {
async execCommand(givenCommand: string, options: { cwd?: string, stdin?: string, options?: Object } = {}): Promise<{ stdout: string, stderr: string, code: number, signal: ?string }> {
let command = givenCommand
const connection = this.connection
invariant(connection, 'Not connected to server')
invariant(typeof options === 'object' && options, 'options must be an Object')
invariant(!options.cwd || typeof options.cwd === 'string', 'options.cwd must be a string')
invariant(!options.stdin || typeof options.stdin === 'string', 'options.stdin must be a string')
invariant(!options.options || typeof options.options === 'object', 'options.options must be an object')

if (options.cwd) {
// NOTE: Output piping cd command to hide directory non-existent errors
Expand All @@ -121,7 +124,7 @@ class SSH {
stream.on('close', function(code, signal) {
resolve({ code, signal, stdout: output.stdout.join('').trim(), stderr: output.stderr.join('').trim() })
})
}, reject))
}, reject), options.options || {})
})
}
async getFile(localFile: string, remoteFile: string, givenSftp: ?Object = null, givenOpts: ?Object = null): Promise<void> {
Expand Down

0 comments on commit d9c7be4

Please sign in to comment.