<commit_before>var spawn = require('child_process').spawn

function Kubectl(type, conf){
	this.type = type
	this.binary = conf.binary || 'kubectl'
	
	this.kubeconfig = conf.kubeconfig || ''
	this.endpoint = conf.endpoint || ''
}

Kubectl.prototype.spawn = function(args, done)
{
	var ops = new Array();

	// Prefer configuration file over endpoint if both are defined
	if (this.kubeconfig) {
		ops.push('--kubeconfig')
		ops.push(this.kubeconfig)
	} else {
		ops.push('-s')
		ops.push(this.endpoint)
	}

	var kube = spawn(this.binary, ops.concat(args))
		, stdout = ''
		, stderr = ''
	
	kube.stdout.on('data', function (data) {
	  	stdout += data
	})
	
	kube.stderr.on('data', function (data) {
	  	stderr += data
	})
	
	kube.on('close', function (code) {
		if( !stderr )
			stderr = undefined
			
		done(stderr, stdout)
	})
}

Kubectl.prototype.list = function(selector, done)
{
	if( !this.type )
		throw new Error('not a function')
	
	if( typeof selector === 'object'){
		var args = '--selector='
		
		for( var key in selector ){
			args += (key + '=' + selector[key])
		}
		
		selector = args + ''
	}
	else{
		done = selector
		selector = '--output=json'
	}
		
	this.spawn(['get', this.type , selector, '--output=json'], function(err, data){
		done(err, !err ? JSON.parse(data) : data)
	})
}

Kubectl.prototype.get = function(name, done)
{
	if( !this.type )
		throw new Error('not a function')
	
	this.spawn(['get', this.type, name, '--output=json'], function(err, data){
		done(err, !err ? JSON.parse(data) : data)
	})
}

Kubectl.prototype.create = function(filepath, done)
{
	if( !this.type )
		throw new Error('not a function')
		
	this.spawn(['create', '-f', filepath], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.delete = function(id, done)
{
	if( !this.type )
		throw new Error('not a function')
		
	this.spawn(['delete', this.type, id], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.update = function(filepath, done)
{
	if( !this.type )
		throw new Error('not a function')
	
	this.spawn(['update', '-f', filepath], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.apply = function(name, json, done)
{
	if( !this.type )
		throw new Error('not a function')
	
	this.spawn(['update',  this.type, name, '--patch='+ JSON.stringify(json)], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.rollingUpdateByFile = function(name, filepath, done)
{
	if( this.type !== 'rc' )
		throw new Error('not a function')
	
	this.spawn(['rolling-update',  name, '-f', filepath, '--update-period=0s'], function(err, data){
		done(err, data)
	})	
}

Kubectl.prototype.rollingUpdate = function(name, image, done)
{
	if( this.type !== 'rc' )
		throw new Error('not a function')
	
	this.spawn(['rolling-update',  name, '--image=' + image, '--update-period=0s'], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.scale = function(name, replicas, done)
{
	if( this.type !== 'rc' )
		throw new Error('not a function')
	
	this.spawn(['scale', '--replicas=' + replicas, 'replicationcontrollers', name], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.logs = function(name, done)
{
	if( this.type !== 'pods' )
		throw new Error('not a function')

	var action = new Array('logs');

	if (name.indexOf(' ') > -1) {
		var names = name.split(/ /);
		action.push(names[0]);
		action.push(names[1]);
	} else {
		action.push(name);
	}

	this.spawn(action, function(err, data){
		done(err, data)
	})
}

// Kubectl.prototype.describe = function()
// {
// 	if( !this.type )
// 		throw new Error('not a function')
	
// 	this.spawn(['describe', '--replicas=' + replicas, 'replicationcontrollers', name], function(err, data){
// 		done(err, data)
// 	})	
// }



module.exports = function(conf)
{
	return {
		pod: new Kubectl('pods', conf)
		, rc: new Kubectl('rc', conf)
		, service: new Kubectl('service', conf)
		, node: new Kubectl('node', conf)
	}
}<commit_msg>implemented describe function<commit_after>var spawn = require('child_process').spawn

function Kubectl(type, conf){
	this.type = type
	this.binary = conf.binary || 'kubectl'

	this.kubeconfig = conf.kubeconfig || ''
	this.endpoint = conf.endpoint || ''
}

Kubectl.prototype.spawn = function(args, done)
{
	var ops = new Array();

	// Prefer configuration file over endpoint if both are defined
	if (this.kubeconfig) {
		ops.push('--kubeconfig')
		ops.push(this.kubeconfig)
	} else {
		ops.push('-s')
		ops.push(this.endpoint)
	}

	var kube = spawn(this.binary, ops.concat(args))
		, stdout = ''
		, stderr = ''

	kube.stdout.on('data', function (data) {
	  	stdout += data
	})

	kube.stderr.on('data', function (data) {
	  	stderr += data
	})

	kube.on('close', function (code) {
		if( !stderr )
			stderr = undefined

		done(stderr, stdout)
	})
}

Kubectl.prototype.list = function(selector, done)
{
	if( !this.type )
		throw new Error('not a function')

	if( typeof selector === 'object'){
		var args = '--selector='

		for( var key in selector ){
			args += (key + '=' + selector[key])
		}

		selector = args + ''
	}
	else{
		done = selector
		selector = '--output=json'
	}

	this.spawn(['get', this.type , selector, '--output=json'], function(err, data){
		done(err, !err ? JSON.parse(data) : data)
	})
}

Kubectl.prototype.get = function(name, done)
{
	if( !this.type )
		throw new Error('not a function')

	this.spawn(['get', this.type, name, '--output=json'], function(err, data){
		done(err, !err ? JSON.parse(data) : data)
	})
}

Kubectl.prototype.create = function(filepath, done)
{
	if( !this.type )
		throw new Error('not a function')

	this.spawn(['create', '-f', filepath], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.delete = function(id, done)
{
	if( !this.type )
		throw new Error('not a function')

	this.spawn(['delete', this.type, id], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.update = function(filepath, done)
{
	if( !this.type )
		throw new Error('not a function')

	this.spawn(['update', '-f', filepath], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.apply = function(name, json, done)
{
	if( !this.type )
		throw new Error('not a function')

	this.spawn(['update',  this.type, name, '--patch='+ JSON.stringify(json)], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.rollingUpdateByFile = function(name, filepath, done)
{
	if( this.type !== 'rc' )
		throw new Error('not a function')

	this.spawn(['rolling-update',  name, '-f', filepath, '--update-period=0s'], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.rollingUpdate = function(name, image, done)
{
	if( this.type !== 'rc' )
		throw new Error('not a function')

	this.spawn(['rolling-update',  name, '--image=' + image, '--update-period=0s'], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.scale = function(name, replicas, done)
{
	if( this.type !== 'rc' )
		throw new Error('not a function')

	this.spawn(['scale', '--replicas=' + replicas, 'replicationcontrollers', name], function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.logs = function(name, done)
{
	if( this.type !== 'pods' )
		throw new Error('not a function')

	var action = new Array('logs');

	if (name.indexOf(' ') > -1) {
		var names = name.split(/ /);
		action.push(names[0]);
		action.push(names[1]);
	} else {
		action.push(name);
	}

	this.spawn(action, function(err, data){
		done(err, data)
	})
}

Kubectl.prototype.describe = function(type, name, done)
{
	if( !this.type ) {
			throw new Error('not a function')
	}

	if( name === null ) {
		this.spawn(['describe', type], function(err, data) {
			done(err, data);
		})
		return;
	}

	this.spawn(['describe', type, name], function(err, data){
		done(err, data)
		return;
	})
}



module.exports = function(conf)
{
	return {
		pod: new Kubectl('pods', conf)
		, rc: new Kubectl('rc', conf)
		, service: new Kubectl('service', conf)
		, node: new Kubectl('node', conf)
	}
}
{'fraction_non_alphanumeric': 0.18985104771522343, 'fraction_numerical': 0.0012623074981065387, 'mean_word_length': 3.8105646630236794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2042788', 'n_tokens_mistral': 3147, 'n_tokens_neox': 2944, 'n_words': 665}