Skip to content

Instantly share code, notes, and snippets.

@teddscofield
Created September 7, 2014 08:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save teddscofield/8a0d5bbf5af479101a98 to your computer and use it in GitHub Desktop.
Save teddscofield/8a0d5bbf5af479101a98 to your computer and use it in GitHub Desktop.
Simple nodejs script for generating cURL commands that tag content using data from a .tsv file.
/**
* A simple AEM bulk tagging utility script.
*
* Intended to be used in conjunction with the AEM bulk editor tool.
* Since the tool doesn't handle array values very well, one can
* use a script like this to achieve the same effect.
*
*
* This script reads a tab separated file whose first column is the
* content path that will be tagged and whose second column is a
* comma separated list of AEM tag ID's to tag the specified content
* path with.
*
* It outputs cURL commands that will apply the tags specified in
* the .tsv. (Could just as easily perform the post as well). These
* cURL commands can be redirected into a file and checked into
* version control for later use.
*
* WARNING: this is a port of a shell script that has worked for me
* in the past. I haven't tested this version of the script so I
* probably screwed something up and it could not work. But it
* serves to illustrate the technique and should be tailored to suite.
*
* Created by tedd on 9/7/2014.
*/
var fs = require('fs'),
readline = require('readline');
// script configuration
var config = {
tsvFileToRead: 'test.data.tsv',
serverName : '127.0.0.1',
serverPort : '4502',
adminUser : 'admin',
adminPass : 'admin'
}
/**
* Generate cURL command and log to stdout
*
* @param {string} path - AEM content path
* @param {string} tags - comma separated AEM tag ID's
*/
function generateCurlCommand(path, tags) {
tags = tags.replace('[','');
tags = tags.replace(']','');
path = path.replace(':','%3A');
// TODO: account for empty tags value, perhaps delete any existing tags?
var s = 'curl -u '+config.adminUser+':'+config.adminPass+' ';
s += 'http://' + config.serverName + ':' + config.serverPort + path;
tags.split(',').forEach(function(val){
s +=' -F"cq:tags='+val+'"';
});
console.log(s);
}
// open file to process
var readDataFileHandle = readline.createInterface({
input: fs.createReadStream(config.tsvFileToRead),
output: process.stdout,
terminal: false
});
// read file line by line, splitting on tabs and passing
// and generating the cURL command for the row in the .tsv
var firstRead = false;
readDataFileHandle.on('line', function(line) {
if (! firstRead) {
firstRead = true;
return;
}
var vals = line.split('\t');
var path = vals[0];
var tags = vals[1];
generateCurlCommand(path,tags);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment