Skip to content

Commit

Permalink
updating logging behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Jul 27, 2020
1 parent 2246045 commit 8d2ff2a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 36 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swrlab/node-storage-wrapper",
"version": "0.0.4",
"version": "0.0.5",
"description": "Wrapping AWS S3, GCP GCS, file storage",
"main": "./src/index.js",
"engines": {
Expand Down
6 changes: 3 additions & 3 deletions src/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = async function (uri) {
path = structure.join('/');

// log progress
this.log('log', ['storage.delete.s3 >', uri]);
this.sdk.log(this, 'log', ['storage.delete.s3 >', uri]);

// delete from aws
await this.sdk.s3
Expand All @@ -42,7 +42,7 @@ module.exports = async function (uri) {
path = structure.join('/');

// log progress
this.log('log', ['storage.delete.gs >', uri]);
this.sdk.log(this, 'log', ['storage.delete.gs >', uri]);

// delete from gcp
await this.sdk.gs.bucket(bucket).file(path).delete(path);
Expand All @@ -51,7 +51,7 @@ module.exports = async function (uri) {
return Promise.resolve();
} else {
// log progress
this.log('log', ['storage.delete.local >', uri]);
this.sdk.log(this, 'log', ['storage.delete.local >', uri]);

// delete file
await deleteLocalFile(this, uri);
Expand Down
35 changes: 19 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,27 @@ function StorageWrapper(config) {
AWS.config.loadFromPath(config.s3);
this.sdk.s3 = new AWS.S3();

// enable logging
this.log = config.logging
? (level, message) => {
if (message instanceof Array) {
message = message.join(' ');
}

if (level == 'log') {
console.log(message);
} else if (level == 'warn') {
console.warn(message);
} else if (level == 'error') {
console.error(message);
}
// configure logging
this.config = {
logging: config.logging,
};

// set logging
this.sdk.log = (that, level, message) => {
if (message instanceof Array) {
message = message.join(' ');
}

if (level == 'log') {
that.config.logging ? console.log(message) : null;
} else if (level == 'warn') {
that.config.logging ? console.warn(message) : null;
} else if (level == 'error') {
that.config.logging ? console.error(message) : null;
}
: null;
};

this.log('log', ['storage.index', 'loaded config >', JSON.stringify({ config })]);
this.sdk.log(this, 'log', ['storage.index', 'loaded config >', JSON.stringify({ config })]);
}

// enable utils
Expand Down
6 changes: 3 additions & 3 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = async function (uri) {
path = structure.join('/');

// log progress
this.log('log', ['storage.list.aws >', uri]);
this.sdk.log(this, 'log', ['storage.list.aws >', uri]);

// load file
var next;
Expand All @@ -75,7 +75,7 @@ module.exports = async function (uri) {
path = structure.join('/');

// log request
this.log('log', ['storage.list.gcp >', uri]);
this.sdk.log(this, 'log', ['storage.list.gcp >', uri]);

// load file
file = await this.sdk.gs.bucket(bucket).getFiles({
Expand All @@ -86,7 +86,7 @@ module.exports = async function (uri) {
return Promise.resolve(file[0]);
} else {
// log request
this.log('log', ['storage.list.local >', uri]);
this.sdk.log(this, 'log', ['storage.list.local >', uri]);

// local file
let file = await listLocalFiles(this, uri);
Expand Down
10 changes: 6 additions & 4 deletions src/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ module.exports = async function (uri) {
bucket = structure.shift();
path = structure.join('/');

console.log(this);

// log progress
this.log('log', ['storage.load.aws >', uri]);
this.sdk.log(this, 'log', ['storage.load.aws >', uri]);

// load file
let file = await this.sdk.s3
Expand All @@ -44,7 +46,7 @@ module.exports = async function (uri) {
bucket = structure.shift();
path = structure.join('/');

this.log('log', ['storage.load gcp >', uri]);
this.sdk.log(this, 'log', ['storage.load gcp >', uri]);

// load file
file = await this.sdk.gs.bucket(bucket).file(path).download();
Expand All @@ -56,7 +58,7 @@ module.exports = async function (uri) {
uri.substr(0, 8).toLowerCase() == 'https://'
) {
// log progress
this.log('log', ['storage.load.https >', uri]);
this.sdk.log(this, 'log', ['storage.load.https >', uri]);

// public http(s) endpoint
let file = await fetch(uri);
Expand All @@ -71,7 +73,7 @@ module.exports = async function (uri) {
// local file
let file = await loadLocalFile(this, uri);

this.log('log', ['storage.load.local >', uri]);
this.sdk.log(this, 'log', ['storage.load.local >', uri]);

return Promise.resolve(file);
}
Expand Down
16 changes: 10 additions & 6 deletions src/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) {
// move file within gcs
if (keepOriginal != true) {
// move is productive/ destructive
this.log('log', ['storage.move.gcp2gcp >', sourceUri, destinationUri]);
this.sdk.log(this, 'log', ['storage.move.gcp2gcp >', sourceUri, destinationUri]);

// move file
await this.sdk.gs.bucket(bucket).file(path).move(destinationUri);
} else {
// mode is dev, only copy file
this.log('log', ['storage.move.gcp2gcp (only copying) >', sourceUri, destinationUri]);
this.sdk.log(this, 'log', [
'storage.move.gcp2gcp (only copying) >',
sourceUri,
destinationUri,
]);

// copy file
await this.sdk.gs.bucket(bucket).file(path).copy(destinationUri);
Expand All @@ -55,7 +59,7 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) {
path = structure.join('/');

// always copying
this.log('log', ['storage.move.aws2aws copying >', sourceUri, destinationUri]);
this.sdk.log(this, 'log', ['storage.move.aws2aws copying >', sourceUri, destinationUri]);

// copy file
await this.sdk.s3
Expand All @@ -69,7 +73,7 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) {
// move file within gcs
if (keepOriginal != true) {
// move is productive/ destructive
this.log('log', ['storage.move.aws2aws deleting source >', sourceUri]);
this.sdk.log(this, 'log', ['storage.move.aws2aws deleting source >', sourceUri]);

// parse source
structure = sourceUri.substr(5).split('/');
Expand All @@ -89,7 +93,7 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) {
return Promise.resolve();
} else {
// any to any transfer
this.log('log', ['storage.move.any2any >', keepOriginal, sourceUri, destinationUri]);
this.sdk.log(this, 'log', ['storage.move.any2any >', keepOriginal, sourceUri, destinationUri]);

// download file
blob = await storage.load(sourceUri);
Expand All @@ -101,7 +105,7 @@ module.exports = async function (sourceUri, destinationUri, keepOriginal) {
if (keepOriginal != true) {
await storage.delete(sourceUri);
} else {
this.log('log', [
this.sdk.log(this, 'log', [
'storage.move.any2any not deleting sourceUri >',
keepOriginal,
sourceUri,
Expand Down
6 changes: 3 additions & 3 deletions src/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = async function (uri, buffer) {
path = structure.join('/');

// log progress
this.log('log', ['storage.save.s3 >', uri]);
this.sdk.log(this, 'log', ['storage.save.s3 >', uri]);

// upload to aws
await this.sdk.s3
Expand All @@ -60,7 +60,7 @@ module.exports = async function (uri, buffer) {
await saveLocalFile(this, tempFilePath, buffer);

// log progress
this.log('log', ['storage.save.gs >', uri]);
this.sdk.log(this, 'log', ['storage.save.gs >', uri]);

// upload file to gcs
await this.sdk.gs.bucket(bucket).upload(tempFilePath, {
Expand All @@ -78,7 +78,7 @@ module.exports = async function (uri, buffer) {
// local file

// log progress
this.log('log', ['storage.save.local >', uri]);
this.sdk.log(this, 'log', ['storage.save.local >', uri]);

// save file
let file = await saveLocalFile(this, uri, buffer);
Expand Down

0 comments on commit 8d2ff2a

Please sign in to comment.