Permalink
Browse files

Work in progress

  • Loading branch information...
1 parent 16048e3 commit b3a08f27caad03c4c7c2bacfec92690d24bc699f @davglass davglass committed Jul 20, 2012
Showing with 703 additions and 0 deletions.
  1. +20 −0 bin/yogi.js
  2. +56 −0 lib/api.js
  3. +40 −0 lib/args.js
  4. +13 −0 lib/cmds.js
  5. +50 −0 lib/cmds/config.js
  6. +46 −0 lib/cmds/help.js
  7. +62 −0 lib/cmds/info.js
  8. +59 −0 lib/cmds/login.js
  9. +24 −0 lib/cmds/show.js
  10. +19 −0 lib/cmds/version.js
  11. +19 −0 lib/cmds/whoami.js
  12. +85 −0 lib/config.js
  13. +51 −0 lib/git.js
  14. +51 −0 lib/log.js
  15. +87 −0 lib/util.js
  16. +21 −0 package.json
View
@@ -0,0 +1,20 @@
+#!/usr/bin/env node
+
+var log = require('../lib/log');
+var config = require('../lib/config');
+var args = require('../lib/args');
+
+
+var options = args.parse();
+config.init(options);
+log.debug('starting up');
+
+if (!options.cmd) {
+ log.bail('Command not known: ' + options.main);
+}
+
+var cmd = options.cmd;
+
+cmd.init(options);
+
+
View
@@ -0,0 +1,56 @@
+var util = require('./util');
+var request = require('request');
+var config = require('./config');
+var log = require('./log');
+var qs = require('querystring');
+
+
+var mod = {
+ host: function() {
+ return 'https://' + config.get('apiHost') + '/api/v1';
+ },
+ get: function(url, cb) {
+ request.get({
+ url: this.host() + url,
+ headers: {
+ 'User-Agent': 'yogi CLI tool'
+ }
+ }, function(err, res) {
+ try {
+ var json = JSON.parse(res.body);
+ if (json.error) {
+ cb(json.error);
+ } else {
+ cb(null, json);
+ }
+ } catch (e) {
+ cb(res.body);
+ }
+ });
+ },
+ post: function(url, data, cb) {
+ var u = this.host() + url;
+ request.post({
+ url: u,
+ headers: {
+ 'User-Agent': 'yogi CLI tool',
+ 'Content-type' : 'application/x-www-form-urlencoded'
+ },
+ body: qs.stringify(data)
+ }, function(err, res) {
+ try {
+ var json = JSON.parse(res.body);
+ if (json.error) {
+ cb(json.error);
+ } else {
+ cb(null, json);
+ }
+ } catch (e) {
+ cb(res.body);
+ }
+ });
+ }
+
+};
+
+util.mix(exports, mod);
View
@@ -0,0 +1,40 @@
+var util = require('./util');
+var nopt = require('nopt');
+var log = require('./log');
+var path = require('path');
+var cmds = require('./cmds');
+
+var known = {
+ config: path,
+ version: Boolean,
+ help: Boolean,
+ loglevel: [ 'silent', 'info', 'debug' ]
+};
+
+var shorts = {
+ "v" : ["--version"],
+ "h" : ["--help"],
+ "lvl": ["--loglevel"],
+ "s": ["--loglevel", "silent"],
+ "debug": ["--loglevel", "debug"]
+};
+
+exports.parse = function() {
+ var main = process.argv[2] || 'help';
+ var off = 3;
+ if (main && main.indexOf('-') === 0) {
+ off = 2;
+ main = 'help';
+ }
+ var parsed = nopt(known, shorts, process.argv, off);
+
+ if (parsed.version) {
+ main = 'version';
+ }
+
+ return {
+ main: main,
+ cmd: cmds[main],
+ parsed: parsed
+ };
+};
View
@@ -0,0 +1,13 @@
+
+var fs = require('fs'),
+ path = require('path');
+
+var files = fs.readdirSync(path.join(__dirname, './cmds'));
+var mods = {};
+files.forEach(function(file) {
+ if (path.extname(file) === '.js') {
+ var mod = file.replace('.js', '');
+ exports[mod] = require('./cmds/' + mod);
+ }
+});
+
View
@@ -0,0 +1,50 @@
+var util = require('../util');
+var nopt = require('nopt');
+var log = require('../log');
+var config = require('../config');
+
+var mods = {
+ init: function(options) {
+ var args = options.parsed.argv.remain;
+ var cmd = args.shift();
+ log.debug('config (' + cmd + ')');
+ if (mods[cmd]) {
+ mods[cmd](args);
+ }
+ },
+ get: function(args) {
+ var key = args[0];
+ log.debug('looking up ' + key);
+ log.info(key + ' is currently set to:');
+ log.log(config.get(key));
+ },
+ set: function(args) {
+ var key = args[0];
+ var val = args[1];
+ if (!key) {
+ log.bail('must pass a key');
+ }
+ log.debug('setting ' + key + ' to ' + val);
+ config.set(key, val);
+ },
+ list: function() {
+ log.debug('listing all config options');
+ log.log(config.list());
+ },
+ delete: function(args) {
+ var key = args[0];
+ if (!key) {
+ log.bail('must pass a key');
+ }
+ log.debug('deleting config for ' + key);
+ config.delete(key);
+ },
+ help: function() {
+ return [
+ 'log',
+ 'set/get <value> works with the config'
+ ]
+ }
+};
+
+util.mix(exports, mods);
View
@@ -0,0 +1,46 @@
+var log = require('../log');
+var util = require('../util');
+var version = require('./version').version;
+var cmds = require('../cmds');
+
+
+var mods = {
+
+ init: function() {
+ log.info('yogi@' + version);
+ var help = [];
+
+ Object.keys(cmds).forEach(function(name) {
+ if (cmds[name].help) {
+ help.push(cmds[name].help());
+ }
+ });
+
+ var len = 0;
+ help.forEach(function(i) {
+ if (i[0].length > len) {
+ len = i[0].length;
+ }
+ });
+
+ var out = [];
+ help.forEach(function(i) {
+ var pad = '';
+ if (i[0].length < len) {
+ for (var o = i[0].length; o < len; o++) {
+ pad += ' ';
+ }
+ }
+ out.push('\t' + i[0] + pad + '\t' + i[1]);
+ });
+ log.info('Command line arguments');
+ out.sort();
+ console.log('\n', out.join('\n'));
+ },
+ help: function() {
+ return ['-h, --help', 'show this stuff']
+ }
+
+};
+
+util.mix(exports, mods);
View
@@ -0,0 +1,62 @@
+var util = require('../util');
+var log = require('../log');
+var git = require('../git');
+var api = require('../api');
+
+var mods = {
+ init: function(options) {
+ log.debug('Info Command');
+ var root = git.findRoot(process.cwd());
+
+ var origin = git.getOrigin();
+ var module = options.parsed.argv.remain[0];
+ if (!module) {
+ module = util.findModule();
+ }
+ if (!module) {
+ log.bail('please provide a module name');
+ }
+ log.info('showing info for ' + module);
+ log.debug('fetching package.json for ' + module);
+ var self = this;
+ api.get('/gallery/' + module + '/package.json', function(err, data) {
+ var json = data.yui;
+ self.json = json;
+ self.meta();
+ self.cdn();
+ self.reports();
+ });
+ },
+ reports: function() {
+ var json = this.json.reports;
+ if (!Object.keys(json.coverage).length) {
+ log.warn('this module does not provide code coverage');
+ }
+ if (!Object.keys(json.tests).length) {
+ log.warn('this module does not provide tests');
+ }
+ },
+ meta: function() {
+ var json = this.json;
+ var mod = {
+ requires: json.module.requires,
+ optional: json.module.optional,
+ supersedes: json.module.supersedes,
+ skinnable: json.module.skinnable
+ };
+ log.info('module meta data:');
+ log.log(JSON.stringify(mod, null, 2));
+ },
+ cdn: function() {
+ if (this.json.cdn) {
+ var cdn = new Date(this.json.cdn.oncdn * 1000);
+ log.info('last build tag pushed: ' + this.json.cdn.buildtag);
+ log.info('module has been on the cdn since: ' + cdn);
+ } else {
+ log.info('this module is not on the cdn');
+ }
+ }
+};
+
+util.mix(exports, mods);
+
View
@@ -0,0 +1,59 @@
+var log = require('../log');
+var config = require('../config');
+var util = require('../util');
+var api = require('../api');
+
+var mods = {
+ init: function(options) {
+ this.options = options.parsed;
+ log.debug('login init');
+ this.username = config.get('username');
+ this.token = config.get('token');
+
+ this.process();
+ },
+ process: function() {
+ if (this.options.username && this.options.password) {
+ this.login(this.options.username, this.options.password);
+ } else {
+ var self = this;
+ util.prompt('username: ', function(uname) {
+ self.options.username = uname;
+ log.debug('got username of: ' + uname);
+ util.prompt('password: ', function(passwd) {
+ self.options.password= passwd;
+ log.debug('got password of: ' + passwd);
+ self.process();
+ }, true);
+ });
+ }
+ },
+ login: function(username, password) {
+ log.debug('Attempting to login as ' + username + ' to ' + config.get('apiHost'));
+
+ api.post('/login', {
+ username: username,
+ password: password
+ }, function(err, data) {
+ if (err) {
+ log.bail(err.message || err);
+ }
+ if (!data.username && !data.token) {
+ log.bail('Failed to fetch token!');
+ }
+ log.info('login successful, saving config');
+ config.set('username', data.username);
+ config.set('token', data.token);
+ log.info('config saved, done.');
+ });
+ },
+ help: function() {
+ return [
+ 'login',
+ '--username --password fetch your login token'
+ ];
+ }
+
+};
+
+util.mix(exports, mods);
View
@@ -0,0 +1,24 @@
+var api = require('../api'),
+ util = require('../util'),
+ log = require('../log');
+
+var mods = {
+ init: function(options) {
+ var module = options.parsed.argv.remain[0];
+ if (!module) {
+ module = util.findModule();
+ }
+ if (!module) {
+ log.bail('please provide a module name');
+ }
+ log.info('fetching package.json for ' + module);
+ api.get('/gallery/' + module + '/package.json', function(err, data) {
+ log.log(JSON.stringify(data, null, 2));
+ });
+ },
+ help: function() {
+ return ['show <module>', 'fetches the package.json for <module>']
+ }
+};
+
+util.mix(exports, mods);
View
@@ -0,0 +1,19 @@
+var path = require('path'),
+ util = require('../util');
+ fs = require('fs');
+
+var version = '';
+
+var version = JSON.parse(fs.readFileSync(path.join(__dirname, '../../package.json'), 'utf8')).version;
+
+var mods = {
+ init: function() {
+ console.log(version);
+ },
+ version: version,
+ help: function() {
+ return ['-v, --version', 'show yogi version']
+ }
+};
+
+util.mix(exports, mods);
Oops, something went wrong. Retry.

0 comments on commit b3a08f2

Please sign in to comment.