Skip to content

Instantly share code, notes, and snippets.

@Eleeleth
Created October 2, 2013 02:05
Show Gist options
  • Save Eleeleth/6788089 to your computer and use it in GitHub Desktop.
Save Eleeleth/6788089 to your computer and use it in GitHub Desktop.
Turn s3 xml from hitting our Maven repository into a useable json object.
'use strict';
var http = require('http');
var url = '';
var data = '';
var libraries = {
releases: {},
snapshots: {}
};
process.argv.forEach(function(val, index, array) {
if(index === 2) {
url = val || 'http://repo.pykl.com/';
}
});
function parseKeys(xml) {
var regex = new RegExp('<Key>(.+?)<\/Key>', 'g');
var keys = [];
var result = [];
var done = false;
console.log(JSON.stringify(regex, null, 2));
while((result = regex.exec(xml)) !== null) {
keys.push(result[1]);
}
return keys;
}
console.log('Url: ' + url);
var res = http.get(url, function (res) {
res.setEncoding('utf-8');
res.on('data', function(chunk) {
data = data + chunk;
});
res.on('end', function(e) {
console.log('Data: ' + data);
var keys = parseKeys(data);
console.log('Keys: ' + keys);
if(typeof keys !== 'undefined' && keys !== null) {
keys.forEach(function (key) {
key = key.split('/');
if(key.length >= 5) {
// split key looks like <releases/snapshots>, <groupid half>, <groupid half>,
// <artifactid>, <version>, <files>
var type = key[0];
var versionKey;
if(key[1] !== '.') {
for(var i=0; i<key.length; i++) {
if(/^\d/.test(key[i])) {
versionKey = i;
}
}
var groupid = [];
for(var j=1; j<(versionKey-1); j++) {
groupid.push(key[j]);
}
var groupid = groupid.join('.');
var artifactid = key[(versionKey-1)];
var version = key[versionKey];
var fullName = [groupid, artifactid].join('.');
if(groupid) {
if(typeof libraries[type][fullName] === 'undefined') {
libraries[type][fullName] = {
groupid: groupid,
artifactid: artifactid
};
}
if(typeof libraries[type][fullName].versions === 'undefined') {
libraries[type][fullName].versions = {};
}
libraries[type][fullName].versions[version] = true;
}
}
}
});
console.log(JSON.stringify(libraries, null, 2));
}
});
}).on('error', function(e) {
console.log('Error: ' + e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment