var spawn = require('child_process').spawn;
var camelize = require('camelize');

module.exports = {
    project: 'Project name: ',
    description: 'Project description: ',
    name: function(values, callback) {
        var called = false;
        var proc = spawn('git', [
            '--bare',
            'config',
            '--global',
            'user.name'
        ]);

        proc.stdout.once('data', function (chunk) {
            called = true;
            callback(null, chunk);
        });
        proc.stdout.once('error', callback);
        proc.stdout.once('end', function () {
            if (called) {
                return;
            }

            callback(new Error('please configure user.name in git'));
        });
    },
    email: function(values, callback) {
        var called = false;
        var proc = spawn('git', [
            '--bare',
            'config',
            '--global',
            'user.email'
        ]);

        proc.stdout.once('data', function (chunk) {
            called = true;
            callback(null, chunk);
        });
        proc.stdout.once('error', callback);
        proc.stdout.once('end', function () {
            if (called) {
                return;
            }

            callback(new Error('please configure user.email in git'));
        });
    },
    projectName: function (values, callback) {
        callback(null, camelize(values.project));
    }
};

var spawn = require('child_process').spawn;
var camelize = require('camelize');
var format = require('util').format;

function fetchFromGitConfig(key) {
    function readValue(values, callback) {
        var called = false;
        var proc = spawn('git', [
            '--bare',
            'config',
            '--global',
            key
        ]);

        proc.stdout.once('data', function (chunk) {
            called = true;
            callback(null, chunk);
        });
        proc.stdout.once('error', callback);
        proc.stdout.once('end', function () {
            if (called) {
                return;
            }

            var message = format('please configure %s in git', key);
            callback(new Error(message));
        });
    }

    return readValue;
}

module.exports = {
    project: 'Project name: ',
    description: 'Project description: ',
    name: fetchFromGitConfig('user.name'),
    email: fetchFromGitConfig('user.email'),
    projectName: function readProjectName(values, callback) {
        callback(null, camelize(values.project));
    }
};

break repeated code into template