Skip to content

Commit

Permalink
Merge pull request #10 from iiddoo/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
iiddoo committed Sep 10, 2018
2 parents a46b645 + 98c47a1 commit 6dc699e
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 36 deletions.
25 changes: 19 additions & 6 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@
* @return {object}
*/
module.exports = {
path: {
windows: './index.js',
darwin: './index.js',
ubuntu: './index.js',
testPlatform: './test.exe',
linux: './index.js'
windows: {
path: './index.js',
command: ''
},
darwin: {
path: './test.sh',
command: 'sh test.sh'
},
ubuntu: {
path: './test.sh',
command: 'sh test.sh'
},
testPlatform: {
path: './test.exe',
command: 'sh test.sh'
},
linux: {
path: './test.sh',
command: 'sh test.sh'
}
};
42 changes: 21 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const validateSafe = require('./lib/validate-safe');
const validatePlatform = require('./lib/validate-platform');
const validateExe = require('./lib/validate-exe');
const command = require('./lib/command');
const execute = require('./lib/execute');

/**
* Get user password
Expand All @@ -14,26 +15,25 @@ const command = require('./lib/command');
* @param {string} safe
* @return {string}
*/
module.exports = (user, appID, safe) => {

module.exports = (user, appID, safe) => {
return new Promise((resolve, reject) => {
try {
// validate user input
validateUser(user);
// validate appID input
validateAppID(appID);
// validate safe input
validateSafe(safe);
// validate platform
validatePlatform();
// validate executable file
validateExe();
// set command string
const cmd = command(user, appID, safe);
// execute shell command with parameters
// validate result
resolve('password');
} catch (error) {
reject(error);
}
// validate user input
validateUser(user);
// validate appID input
validateAppID(appID);
// validate safe input
validateSafe(safe);
// validate platform
validatePlatform();
// validate executable file
validateExe();
// set command string
const cmd = command(user, appID, safe);
// execute shell command
const password = execute(cmd);
// return result
resolve(password);
});
};
};

3 changes: 2 additions & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const config = require('../config');
*/
module.exports = (user, appID, safe) => {
const platform = process.platform;
const command = `${config.path[platform]} GetPassword /p AppDescs.AppID=${appID} /p Query="Safe=${safe};Folder=root;Object=${user}" /o Password`;
// const command = `${config.path[platform]} GetPassword /p AppDescs.AppID=${appID} /p Query="Safe=${safe};Folder=root;Object=${user}" /o Password`;
const command = config[platform].command;
return command;
};
13 changes: 13 additions & 0 deletions lib/execute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const execSync = require('child_process').execSync;

/**
* Execute shell command
* @param {string} command
* @return {string}
*/
module.exports = command => {
return new Promise((resolve, reject) => {
const result = execSync(command);
resolve(result.toString());
});
};
Empty file removed lib/execute.test.js
Empty file.
2 changes: 1 addition & 1 deletion lib/validate-exe.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = require('../config');
module.exports = testPlatform => {
try {
const platform = testPlatform || process.platform;
const exe = config.path[platform];
const exe = config[platform].path;
const exists = fs.existsSync(exe);
if (!exists) {
throw new Error(`Executable ${exe} not exists`);
Expand Down
2 changes: 1 addition & 1 deletion lib/validate-platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const config = require('../config');
module.exports = testPlatform => {
try {
const platform = testPlatform || process.platform;
const valid = config.path[platform];
const valid = config[platform];
if (!valid) {
throw new Error(`Platform ${platform} is not supported`);
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@iiddoo/aim-password-retrieve",
"version": "2.1.1",
"version": "2.1.2",
"description": "Retrieve password for user.",
"main": "index.js",
"scripts": {
"test": "mocha --reporter spec",
"update": "npm version patch -m 'Version %s - add sweet badges'",
"update": "npm version patch -m 'Version %s - add badges'",
"cover": "node_modules/istanbul/lib/cli.js cover node_modules/mocha/bin/_mocha -- -R spec test/*"
},
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sleep 1
echo "password"
17 changes: 17 additions & 0 deletions test/execute.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const execute = require('../lib/execute');
const { expect } = require('chai');

describe('Execute', ()=> {
it('Should throw error', done => {
expect(async ()=> {
try {
execute();
} catch (error) {}
}).to.throw
done()
});
it('Should return string', async () => {
const result = await execute('cd ./ && sh test.sh');
expect(result).to.be.a('string');
});
});
11 changes: 9 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ const { expect } = require('chai');

describe('Index', ()=> {
it('Should throw error', done => {
expect(index()).to.throw
done()
expect(async () => {
try {
const pwd = await index();
return pwd;
} catch (error) {
throw new Error(error);
}
}).to.throw
done();
});
it('Should return password', async () => {
const pwd = await index('user', 'my_appID', 'my_safe');
Expand Down
1 change: 0 additions & 1 deletion test/validate-exe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ describe('Validate executable', ()=> {
expect(validate()).to.equal();
});
});

1 change: 0 additions & 1 deletion test/validate-safe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ describe('Validate safe', ()=> {
expect(validate('safe_name')).to.equal();
});
});

0 comments on commit 6dc699e

Please sign in to comment.