This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdiffer.js
executable file
·70 lines (60 loc) · 2.13 KB
/
differ.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
'use strict';
var path = require('path');
var elasticsearch = require('elasticsearch');
var aws = require('aws-sdk');
var program = require('commander');
var awsAutoAuth = require('aws-auto-auth');
var esVar = process.env.ELASTICSEARCH_PORT;
var S3 = new aws.S3();
var credentials = awsAutoAuth(aws);
var esHost;
var esPort;
var esObj = {};
var s3List;
var makeRequest;
if(credentials.accessKeyId){
makeRequest = S3.makeRequest.bind(S3);
}else{
makeRequest= S3.makeUnauthenticatedRequest.bind(S3);
}
if(esVar){
esVar = esVar.split('//')[1].split(':');
esHost = esVar[0];
esPort = +esVar[1];
}
program
.version('0.0.1')
.option('-b, --bucket <bucket>', 'An S3 bucket where data resides.')
.option('-d, --directory <directory>', 'A directory within S3 or on the filesystem relative to where the differ was run.', '.')
.option('--alias <alias>', 'Elasticsearch index alias. Defaults to address.', 'address')
.option('-h, --host <host>', 'ElasticSearch host. Defaults to localhost unless linked to a Docker container aliased to Elasticsearch', esHost || 'localhost')
.option('-p, --port <port>', 'ElasticSearch port. Defaults to 9200 unless linked to a Docker container aliased to Elasticsearch.', Number, esPort || 9200)
.parse(process.argv);
if(program.bucket && program.directory === '.') program.directory = '';
var client = new elasticsearch.Client({
host: program.host + ':' + program.port,
log: []
});
client.indices.get({index: program.alias}, function(err, data){
if (err) throw err;
Object.keys(data).forEach(function(index){
esObj[index.split('-').slice(0, -2).join('-')] = 1;
});
diffLists();
});
makeRequest('listObjects', {'Bucket': program.bucket, 'Prefix': program.directory}, function(err, res){
if(err) throw err;
s3List = res.Contents.filter(function(v){
return v.Key[v.Key.length - 1] !== '/';
}).map(function(v){
return {key: v.Key, basename: path.basename(v.Key, path.extname(v.Key)).toLowerCase()};
});
diffLists();
});
function diffLists(){
if(!s3List || !Object.keys(esObj).length) return;
s3List.forEach(function(v){
if(!esObj[v.basename]) console.log(v.key);
});
}