Skip to content

Commit dd3cc9e

Browse files
2.2.2 Fix: Using space inside parameter #16, use exec if command contains "
1 parent 97b37a6 commit dd3cc9e

File tree

7 files changed

+165
-96
lines changed

7 files changed

+165
-96
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ build/Release
2727
node_modules
2828

2929
my_config.json
30-
30+
my_config2.json
3131

3232
.DS_Store
3333
npm-debug.log

lib/index.js

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

lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aws-cli-js",
3-
"version": "2.2.1",
3+
"version": "2.2.2",
44
"description": "A node.js wrapper for the aws command line interface",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
@@ -11,6 +11,7 @@
1111
"lint": "eslint . --fix --ext .ts",
1212
"clean": "npx del-cli lib",
1313
"test": "jest",
14+
"test16": "jest ./src/issue#16.spec.ts",
1415
"prepare": "npm run lint && tsc && npx del-cli lib/**/*.spec.* "
1516
},
1617
"repository": {

src/index.ts

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import nodeify from 'nodeify-ts';
2-
import * as child_process from 'child_process';
3-
const execFile = child_process.execFile;
1+
import * as child_process from 'child_process';
2+
3+
import nodeify from 'nodeify-ts';
44

5+
const execFile = child_process.execFile;
6+
const exec = child_process.exec;
57

68
const extractResult = (result: Result): Result => {
79
try {
@@ -22,64 +24,78 @@ export class Aws {
2224
}) { }
2325

2426
public command(command: string, callback?: (err: any, data: any) => void): Promise<any> {
25-
26-
27-
const promise = Promise.resolve().then( () => {
28-
29-
30-
const env_vars = ('HOME PATH AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY ' +
31-
'AWS_SESSION_TOKEN AWS_DEFAULT_REGION ' +
32-
'AWS_DEFAULT_PROFILE AWS_CONFIG_FILE').split(' ');
33-
34-
35-
const env = env_vars.reduce((result: any, value: string) => {
36-
if (process.env[value]) {
37-
result[value] = process.env[value];
38-
}
39-
return result;
40-
},{});
4127

42-
env['DEBUG'] = '';
28+
const execCommand = `${this.options.cliPath} ${command}`;
29+
30+
const env_vars = ('HOME PATH AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY ' +
31+
'AWS_SESSION_TOKEN AWS_DEFAULT_REGION ' +
32+
'AWS_DEFAULT_PROFILE AWS_CONFIG_FILE').split(' ');
4333

44-
if (this.options.accessKey) {
45-
env['AWS_ACCESS_KEY_ID'] = this.options.accessKey;
46-
}
4734

48-
if (this.options.secretKey) {
49-
env['AWS_SECRET_ACCESS_KEY'] = this.options.secretKey;
35+
const env = env_vars.reduce((result: any, value: string) => {
36+
if (process.env[value]) {
37+
result[value] = process.env[value];
5038
}
51-
52-
if (this.options.sessionToken) {
53-
env['AWS_SESSION_TOKEN'] = this.options.sessionToken;
54-
}
55-
56-
const execOptions = {
57-
cwd: this.options.currentWorkingDirectory,
58-
env: env,
59-
maxBuffer: 200 * 1024 * 1024,
60-
};
61-
62-
//console.log('exec options =', execOptions);
63-
//console.log('options.cliPath =', this.options.cliPath);
64-
let cmd = [...command.split(' ')];
65-
cmd = cmd.filter(v => v.length > 0);
66-
//console.log('cmd2 = ', cmd);
67-
68-
return new Promise<{ stderr: string, stdout: string }>( (resolve, reject) => {
69-
execFile(this.options.cliPath, cmd, execOptions, (error: Error | null, stdout: string, stderr: string) => {
70-
if (error) {
71-
const message = `error: '${error}' stdout = '${stdout}' stderr = '${stderr}'`;
72-
//console.error(message);
73-
reject(message);
74-
}
75-
//console.log(`stdout: ${stdout}`);
76-
resolve({ stderr: stderr, stdout: stdout });
77-
});
39+
return result;
40+
}, {});
41+
42+
env['DEBUG'] = '';
43+
44+
if (this.options.accessKey) {
45+
env['AWS_ACCESS_KEY_ID'] = this.options.accessKey;
46+
}
47+
48+
if (this.options.secretKey) {
49+
env['AWS_SECRET_ACCESS_KEY'] = this.options.secretKey;
50+
}
51+
52+
if (this.options.sessionToken) {
53+
env['AWS_SESSION_TOKEN'] = this.options.sessionToken;
54+
}
55+
56+
const execOptions = {
57+
cwd: this.options.currentWorkingDirectory,
58+
env: env,
59+
maxBuffer: 200 * 1024 * 1024,
60+
};
61+
62+
63+
const promise = Promise.resolve().then(() => {
64+
65+
66+
return new Promise<{ stderr: string, stdout: string }>((resolve, reject) => {
67+
68+
if (command.indexOf('"') < 0) {
69+
let cmd = [...command.split(' ')];
70+
cmd = cmd.filter(v => v.length > 0);
71+
//console.log('execFile');
72+
execFile(this.options.cliPath, cmd, execOptions, (error: Error | null, stdout: string, stderr: string) => {
73+
if (error) {
74+
const message = `error: '${error}' stdout = '${stdout}' stderr = '${stderr}'`;
75+
//console.error(message);
76+
reject(message);
77+
}
78+
//console.log(`stdout: ${stdout}`);
79+
resolve({ stderr: stderr, stdout: stdout });
80+
});
81+
} else {
82+
//console.log('exec');
83+
exec(execCommand, execOptions, (error: Error | null, stdout: string, stderr: string) => {
84+
if (error) {
85+
const message = `error: '${error}' stdout = '${stdout}' stderr = '${stderr}'`;
86+
//console.error(message);
87+
reject(message);
88+
}
89+
//console.log(`stdout: ${stdout}`);
90+
resolve({ stderr: stderr, stdout: stdout });
91+
});
92+
}
7893
});
94+
7995
}).then((data: { stderr: string, stdout: string }) => {
8096

8197
const result: Result = {
82-
command,
98+
command: execCommand,
8399
error: data.stderr,
84100
object: null,
85101
raw: data.stdout,

src/issue#16.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import * as util from 'util';
2+
3+
import { Aws, Options } from './index';
4+
5+
/* eslint-disable @typescript-eslint/no-var-requires */
6+
const config = require('../my_config.json');
7+
8+
//console.log('config', config);
9+
//https://github.com/Quobject/aws-cli-js/issues/16
10+
11+
12+
describe('Using space inside parameter https://github.com/Quobject/aws-cli-js/issues/16', () => {
13+
const options = new Options(
14+
/* accessKey */ config.accessKeyId,
15+
/* secretKey */ config.secretAccessKey,
16+
/* sessionToken */ config.sessionToken,
17+
/* currentWorkingDirectory */ undefined,
18+
/* cliPath */ 'aws',
19+
);
20+
21+
const aws = new Aws(options);
22+
23+
it('with double quotes', () => {
24+
const command = 's3api put-object --bucket test.quobject.io --key pic.png --body ./testdata/smiley.png --content-type image/png --cache-control "public, max-age=31536000"';
25+
return aws.command(command).then((data: any) => {
26+
console.log('data = ', util.inspect(data, { depth: 10 }));
27+
expect(data).toBeTruthy();
28+
});
29+
});
30+
31+
it('without double quotes', () => {
32+
const command = 's3api put-object --bucket test.quobject.io --key pic.png --body ./testdata/smiley.png --content-type image/png --cache-control max-age=31536000';
33+
return aws.command(command).then((data: any) => {
34+
console.log('data = ', util.inspect(data, { depth: 10 }));
35+
expect(data).toBeTruthy();
36+
});
37+
});
38+
});

testdata/smiley.png

79.7 KB
Loading

0 commit comments

Comments
 (0)