Skip to content

Commit

Permalink
fix(s3-bucket-download): 🐛 quality of life improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
mikr13 committed Oct 21, 2021
1 parent cffe6bd commit e0de88f
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 90 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"conventionalCommits.scopes": [
"s3-bucket-download"
]
}
141 changes: 65 additions & 76 deletions lib/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const {
} = require('prompt');
const { say } = require('cfonts');
const { type, userInfo } = require('os');
const { mkdirSync, writeFileSync, existsSync, readFileSync } = require("fs");
const { mkdirSync, writeFileSync, existsSync } = require("fs");


const osType = type();
Expand Down Expand Up @@ -44,23 +44,7 @@ say('opendevs', {
});


const returnFilePath = path => [`${path}/config`, `${path}/credentials`]


const readConfigFile = () => {
const data = readFileSync(returnAWSConfigFolder, { encoding:'utf8', flag:'r' });

//NOTE: parse data, regex?
}


exports.getConfig = () => {
if (existsSync(returnAWSConfigFolder)) {
return readConfigFile();
} else {
return new Error('No config found, create new');
}
};
const returnFilePath = path => [`${path}/config`, `${path}/credentials`];


const chooseBucketAndPath = () => {
Expand Down Expand Up @@ -98,14 +82,14 @@ const chooseBucketAndPath = () => {
});
}
});
}
};


const downloadStart = (bucketName, path) => {
const sync = spawn(`aws s3 sync s3://${bucketName} ${path}/${bucketName}`, [], {
cwd: __dirname,
shell: true
})
});

sync.stdout.on('data', (data) => {
console.log(`${data}`);
Expand All @@ -118,7 +102,7 @@ const downloadStart = (bucketName, path) => {
sync.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
}
};


const initiate = (bucketsName, path = './data') => {
Expand All @@ -128,10 +112,10 @@ const initiate = (bucketsName, path = './data') => {
};


exports.configure = (callback) => {
const configure = (callback) => {
exec(`aws configure set default.s3.max_concurrent_requests 1000 && aws configure set default.s3.max_queue_size 100000`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
console.error(`exec ${error}`);

callback(error, null);
}
Expand All @@ -141,6 +125,58 @@ exports.configure = (callback) => {
};


const starter = () => {
if (argv.all) {
console.log('Running in Download All mode!');

exec(`aws s3api list-buckets --query "Buckets[].Name"`, (error, stdout, stderr) => {
if (error) {
console.error(`exec ${error}`);
return;
} else if (stderr) {
console.error(`AWS CLI error: ${stderr}`);
} else {
console.log(`Buckets to download: ${stdout}`);

configure((error, _) => {
if (error) {
return;
} else {
initiate(Array(stdout).join(','), argv.path);
}
});
}
});
} else if (argv.bucketName === undefined && argv.path === undefined) {
console.log('Running in Interactive mode!');

exec(`aws s3api list-buckets --query "Buckets[].Name"`, (error, stdout, stderr) => {
if (error) {
console.error(`exec ${error}`);

return;
} else if (stderr) {
console.error(`AWS CLI error: ${stderr}`);
} else {
console.log(`Buckets available: ${stdout}`);

chooseBucketAndPath();
}
});
} else {
console.log('Running in CLI mode!');

configure((error, _) => {
if (error) {
return;
} else {
initiate(argv.bucketName, argv.path);
}
});
}
};


const setConfig = (path) => {

mkdirSync(path);
Expand Down Expand Up @@ -180,7 +216,7 @@ const setConfig = (path) => {

get(inputSchema, (err, result) => {
if (err) {
console.error(`exec error: ${err}`);
console.error(`exec ${err}`);
return process.exit(1);
}
const credentialsFileContent = `[default]\naws_access_key_id = ${result.aws_access_key_id}\naws_secret_access_key = ${result.aws_secret_access_key}`;
Expand All @@ -189,58 +225,6 @@ const setConfig = (path) => {
writeFileSync(configFilePath, configFileContent);
starter();
});
}


exports.starter = () => {
if (argv.all) {
console.log('Running in Download All mode!');

exec(`aws s3api list-buckets --query "Buckets[].Name"`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return process.exit(1);
} else if (stderr) {
console.error(`AWS CLI error: ${stderr}`);
} else {
console.log(`Buckets to download: ${stdout}`);

configure((error, _) => {
if (error) {
return;
} else {
initiate(Array(stdout).join(','), argv.path);
}
});
}
});
} else if (argv.bucketName === undefined && argv.path === undefined) {
console.log('Running in Interactive mode!');

exec(`aws s3api list-buckets --query "Buckets[].Name"`, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);

return;
} else if (stderr) {
console.error(`AWS CLI error: ${stderr}`);
} else {
console.log(`Buckets available: ${stdout}`);

chooseBucketAndPath();
}
});
} else {
console.log('Running in CLI mode!');

configure((error, _) => {
if (error) {
return;
} else {
initiate(argv.bucketName, argv.path);
}
});
}
};


Expand All @@ -249,3 +233,8 @@ if (existsSync(returnAWSConfigFolder)) {
} else {
setConfig(returnAWSConfigFolder);
}

module.exports = {
starter,
configure,
};
20 changes: 6 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
{
"name": "s3-bucket-downloader",
"version": "1.1.8",
"version": "1.1.9",
"description": "This downloads file from a single bucket, multiple buckets or all of the buckets if no bucket name specified",
"preferGlobal": true,
"scripts": {
"s3-bucket-downloader": "node bin/download.js",
"commit-a": "git add . && git cz",
"commit": "git cz",
"start": "node bin/download.js",
"s3-bucket-downloader": "node bin/s3-bucket-download.js",
"start": "node bin/s3-bucket-download.js",
"snyk-protect": "snyk protect",
"prepare": "npm run snyk-protect"
},
Expand Down Expand Up @@ -47,18 +45,12 @@
},
"homepage": "https://github.com/open-devs/s3-bucket-download#readme",
"dependencies": {
"cfonts": "^2.9.3",
"cfonts": "^2.10.0",
"minimist": "^1.2.5",
"prompt": "^1.1.0",
"snyk": "^1.667.0"
"prompt": "^1.2.0"
},
"devDependencies": {
"git-cz": "^4.7.6"
},
"config": {
"commitizen": {
"path": "git-cz"
}
"snyk": "^1.742.0"
},
"snyk": true
}
Loading

0 comments on commit e0de88f

Please sign in to comment.