Skip to content

Commit

Permalink
Resolves #14 - Separated out cli program from github client
Browse files Browse the repository at this point in the history
  • Loading branch information
codenameyau committed Jun 10, 2015
1 parent c39a6cc commit d3d6656
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 121 deletions.
71 changes: 67 additions & 4 deletions bin/github-label.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
#!/usr/bin/env node
'use strict';

var program = require('commander');
var prompt = require('prompt');
var requireDir = require('require-dir');
var format = require('util').format;
var logging = require('../src/logging');
// var labels = require('../src/labels');
var GithubClient = require('../src/client');
var utils = require('../src/utils');
var presets = requireDir('../src/presets');
var pjson = require('../package.json');

// Command-line argument parser.

/********************************************************************
* HELPER FUNCTIONS
*********************************************************************/
var promptForCredentials = function(client, callback) {
console.log('Please enter your GitHub credentials.\n');
var authPrompt = [
{ name: 'username', type: 'string', required: true },
{ name: 'password', type: 'string', required: true, hidden: true }];
prompt.start();
prompt.get(authPrompt, function(error, result) {
if (error) {
logging.exit('invalid login credentials.');
} client.setupAuthClient(result.username, result.password);
callback();
});
};

var createGithubLabels = function(repository, labels) {
var client = new GithubClient();
client.setRepository(repository);

// TODO: Clean up labels for API here.
// TODO: code

// Authenticate client with access token.
if (client.ACCESS_TOKEN) {
client.setupTokenClient();
client.postLabels(labels);
}

// Authenticate client with credentials.
else {
promptForCredentials(client, function() {
client.postLabels(labels);
});
}
};


/********************************************************************
* MAIN CLI PROGRAM
*********************************************************************/
program.version(pjson.version)
.arguments('repo')
.option('-p, --preset [value]', 'Specify a label preset.', 'default')
Expand All @@ -18,11 +66,26 @@ if (program.rawArgs.length < 3) {
program.help();
}

// Validate that repository is specified.
// Validate that a repository is specified.
var repository = program.args[0];
if (!repository) {
logging.exit('please specify a repository like "user/repo"');
}

// Read JSON file if specfied by user.
if (program.json) {
utils.readJSON(program.json, function(data) {
createGithubLabels(repository, data);
});
}

console.log(program);
// Use one of the specified label preset.
else {
var labelPreset = program.preset;
var labels = presets[labelPreset];
if (!labels) {
logging.exit(format('preset "%s" doesn\'t exist.', labelPreset));
} else {
createGithubLabels(repository, labels);
}
}
56 changes: 56 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

var github = require('octonode');

/********************************************************************
* FUNCTION CONSTRUCTOR
*********************************************************************/
function GithubClient() {
// Only specified if environment variable exists.
this.ACCESS_TOKEN = process.env.GITHUB_LABEL_TOKEN;
this.repository = '';
this.client = null;
this.ghrepo = null;
}


/********************************************************************
* PUBLIC METHODS
*********************************************************************/
GithubClient.prototype.setRepository = function(repository) {
this.repository = repository;
this.ghrepo = this.client.repo(this.repository);
};

GithubClient.prototype.getRepository = function() {
return this.repository;
};

GithubClient.prototype.setupTokenClient = function() {
this.client = github.client(this.ACCESS_TOKEN);
this.setupGithubRepo();
};

GithubClient.prototype.setupAuthClient = function(username, password) {
this.client = github.client({
username: username,
password: password
});
this.setupGithubRepo();
};

GithubClient.prototype.getLabels = function(callback) {
this.ghrepo.labels(function(blank, data, header) {
callback(data, header);
});
};

GithubClient.prototype.postLabels = function(labels) {
console.log('TODO: Send post request');
};


/********************************************************************
* MODULE EXPORT
*********************************************************************/
module.exports = GithubClient;
116 changes: 0 additions & 116 deletions src/labels.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/logging.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

exports.exit = function(message) {
console.log('Exit: ' + message);
console.log('\nExit: ' + message);
process.exit(1);
};
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

var fs = require('fs');
var logging = require('./logging');

exports.readJSON = function(filepath, callback) {
fs.readFile(filepath, 'utf8', function(error, data) {
if (error) {
logging.exit('could not read file: ' + filepath);
} callback(JSON.parse(data));
});
};

0 comments on commit d3d6656

Please sign in to comment.