Skip to content

Commit

Permalink
2.2.2 Fix: Using space inside parameter #16, use exec if command cont…
Browse files Browse the repository at this point in the history
…ains "
  • Loading branch information
Matthias Ludwig committed Apr 11, 2021
1 parent 97b37a6 commit dd3cc9e
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -27,7 +27,7 @@ build/Release
node_modules

my_config.json

my_config2.json

.DS_Store
npm-debug.log
Expand Down
94 changes: 54 additions & 40 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "aws-cli-js",
"version": "2.2.1",
"version": "2.2.2",
"description": "A node.js wrapper for the aws command line interface",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand All @@ -11,6 +11,7 @@
"lint": "eslint . --fix --ext .ts",
"clean": "npx del-cli lib",
"test": "jest",
"test16": "jest ./src/issue#16.spec.ts",
"prepare": "npm run lint && tsc && npx del-cli lib/**/*.spec.* "
},
"repository": {
Expand Down
122 changes: 69 additions & 53 deletions src/index.ts
@@ -1,7 +1,9 @@
import nodeify from 'nodeify-ts';
import * as child_process from 'child_process';
const execFile = child_process.execFile;
import * as child_process from 'child_process';

import nodeify from 'nodeify-ts';

const execFile = child_process.execFile;
const exec = child_process.exec;

const extractResult = (result: Result): Result => {
try {
Expand All @@ -22,64 +24,78 @@ export class Aws {
}) { }

public command(command: string, callback?: (err: any, data: any) => void): Promise<any> {


const promise = Promise.resolve().then( () => {


const env_vars = ('HOME PATH AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY ' +
'AWS_SESSION_TOKEN AWS_DEFAULT_REGION ' +
'AWS_DEFAULT_PROFILE AWS_CONFIG_FILE').split(' ');


const env = env_vars.reduce((result: any, value: string) => {
if (process.env[value]) {
result[value] = process.env[value];
}
return result;
},{});

env['DEBUG'] = '';
const execCommand = `${this.options.cliPath} ${command}`;

const env_vars = ('HOME PATH AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY ' +
'AWS_SESSION_TOKEN AWS_DEFAULT_REGION ' +
'AWS_DEFAULT_PROFILE AWS_CONFIG_FILE').split(' ');

if (this.options.accessKey) {
env['AWS_ACCESS_KEY_ID'] = this.options.accessKey;
}

if (this.options.secretKey) {
env['AWS_SECRET_ACCESS_KEY'] = this.options.secretKey;
const env = env_vars.reduce((result: any, value: string) => {
if (process.env[value]) {
result[value] = process.env[value];
}

if (this.options.sessionToken) {
env['AWS_SESSION_TOKEN'] = this.options.sessionToken;
}

const execOptions = {
cwd: this.options.currentWorkingDirectory,
env: env,
maxBuffer: 200 * 1024 * 1024,
};

//console.log('exec options =', execOptions);
//console.log('options.cliPath =', this.options.cliPath);
let cmd = [...command.split(' ')];
cmd = cmd.filter(v => v.length > 0);
//console.log('cmd2 = ', cmd);

return new Promise<{ stderr: string, stdout: string }>( (resolve, reject) => {
execFile(this.options.cliPath, cmd, execOptions, (error: Error | null, stdout: string, stderr: string) => {
if (error) {
const message = `error: '${error}' stdout = '${stdout}' stderr = '${stderr}'`;
//console.error(message);
reject(message);
}
//console.log(`stdout: ${stdout}`);
resolve({ stderr: stderr, stdout: stdout });
});
return result;
}, {});

env['DEBUG'] = '';

if (this.options.accessKey) {
env['AWS_ACCESS_KEY_ID'] = this.options.accessKey;
}

if (this.options.secretKey) {
env['AWS_SECRET_ACCESS_KEY'] = this.options.secretKey;
}

if (this.options.sessionToken) {
env['AWS_SESSION_TOKEN'] = this.options.sessionToken;
}

const execOptions = {
cwd: this.options.currentWorkingDirectory,
env: env,
maxBuffer: 200 * 1024 * 1024,
};


const promise = Promise.resolve().then(() => {


return new Promise<{ stderr: string, stdout: string }>((resolve, reject) => {

if (command.indexOf('"') < 0) {
let cmd = [...command.split(' ')];
cmd = cmd.filter(v => v.length > 0);
//console.log('execFile');
execFile(this.options.cliPath, cmd, execOptions, (error: Error | null, stdout: string, stderr: string) => {
if (error) {
const message = `error: '${error}' stdout = '${stdout}' stderr = '${stderr}'`;
//console.error(message);
reject(message);
}
//console.log(`stdout: ${stdout}`);
resolve({ stderr: stderr, stdout: stdout });
});
} else {
//console.log('exec');
exec(execCommand, execOptions, (error: Error | null, stdout: string, stderr: string) => {
if (error) {
const message = `error: '${error}' stdout = '${stdout}' stderr = '${stderr}'`;
//console.error(message);
reject(message);
}
//console.log(`stdout: ${stdout}`);
resolve({ stderr: stderr, stdout: stdout });
});
}
});

}).then((data: { stderr: string, stdout: string }) => {

const result: Result = {
command,
command: execCommand,
error: data.stderr,
object: null,
raw: data.stdout,
Expand Down
38 changes: 38 additions & 0 deletions src/issue#16.spec.ts
@@ -0,0 +1,38 @@
import * as util from 'util';

import { Aws, Options } from './index';

/* eslint-disable @typescript-eslint/no-var-requires */
const config = require('../my_config.json');

//console.log('config', config);
//https://github.com/Quobject/aws-cli-js/issues/16


describe('Using space inside parameter https://github.com/Quobject/aws-cli-js/issues/16', () => {
const options = new Options(
/* accessKey */ config.accessKeyId,
/* secretKey */ config.secretAccessKey,
/* sessionToken */ config.sessionToken,
/* currentWorkingDirectory */ undefined,
/* cliPath */ 'aws',
);

const aws = new Aws(options);

it('with double quotes', () => {
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"';
return aws.command(command).then((data: any) => {
console.log('data = ', util.inspect(data, { depth: 10 }));
expect(data).toBeTruthy();
});
});

it('without double quotes', () => {
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';
return aws.command(command).then((data: any) => {
console.log('data = ', util.inspect(data, { depth: 10 }));
expect(data).toBeTruthy();
});
});
});
Binary file added testdata/smiley.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit dd3cc9e

Please sign in to comment.