Permalink
Browse files

Cli changed

  • Loading branch information...
1 parent 31ddcc5 commit 9ac580af8b700f0f467ecd8ee94c7b40ea294b1d @hakovala committed Dec 13, 2012
Showing with 102 additions and 24 deletions.
  1. +85 −8 bin/cli.js
  2. +15 −0 lib/helpers.js
  3. +2 −16 lib/trakt.js
View
@@ -1,22 +1,83 @@
#!/usr/bin/env node
var Trakt = require('../index.js');
-var argv = require('optimist')
- .usage('Usage: $0 -a <action> -m <method> [options]')
+var Helper = require('../lib/helpers.js')
+var pjson = require('../package.json');
+var api = require('../lib/api-actions.js')
+var optimist = require('optimist')
+ .usage('Usage: $0 [options] <action> <method> [--<argument> <value>]')
.alias('u', 'user')
.alias('p', 'pass')
- .alias('a', 'action')
- .alias('m', 'method')
- .demand(['a','m'])
- .argv;
+ .alias('h', 'help')
+ .alias('v', 'version')
+
+optimist.usage('Node wrapper for Trakt API\n\nUsage: $0 [options] <action> <method> [--<parameter> <value>]', {
+ 'version': {
+ description: 'output version information',
+ short: 'v'
+ },
+ 'help': {
+ description: 'this help text',
+ short: 'h'
+ },
+ 'user': {
+ description: 'Trakt username to use with API calls',
+ short: 'u'
+ },
+ 'pass': {
+ description: 'Trakt password to use with API calls',
+ short: 'p'
+ },
+ 'passhash': {
+ description: 'indicates that password is in sha1 format',
+ },
+ 'pretty': {
+ description: 'pretty print result json'
+ }
+})
+var argv = optimist.argv
+
+if (argv.v) {
+ console.log(pjson.version)
+ process.exit()
+}
+if (argv.h) {
+ showHelp()
+ process.exit()
+}
+if (argv._.length < 2) {
+ console.error('Missing actions or method');
+ showHelp()
+ process.exit(1);
+}
+
+var action = undefined
+var method = undefined
+
+if (api[argv._[0]]) {
+ var a = Helper.apiMethod(argv._[0], argv._[1])
+ if (a) {
+ action = argv._[0]
+ method = argv._[1]
+
+ } else {
+ console.error('Invalid method')
+ process.exit(1)
+ }
+} else {
+ console.error('Invalid action');
+ process.exit(1)
+}
var trakt = new Trakt({username: argv.user, password: argv.pass});
trakt.on('error', function(err) {
console.log("Trakt error: " + err.message)
})
-trakt.request(argv.action, argv.method, argv, function(err, result) {
- result = JSON.stringify(result, null, 4)
+trakt.request(action, method, argv, function(err, result) {
+ if (argv.pretty) {
+ result = JSON.stringify(result, null, 4)
+ }
if (err) {
console.log(err);
if (result) {
@@ -27,3 +88,19 @@ trakt.request(argv.action, argv.method, argv, function(err, result) {
}
})
+
+function showHelp() {
+ optimist.showHelp()
+ console.log("Commands:")
+ console.log(" <action>\tBase name of the method in API call")
+ console.log(" <method>\tRest of the method name")
+ console.log(" <argument>\tName of the argument for API call")
+ console.log(" <value>\tValue for the argument")
+ console.log()
+ console.log("Examples:")
+ console.log(" $ trakt search shows continuum 'american dad'")
+ console.log("\t- Searches Trakt for shows named 'continuum' and 'american dad'")
+ console.log(" $ trakt -u username -p password account settings")
+ console.log("\t- Fetches users settings")
+
+}
View
@@ -0,0 +1,15 @@
+var api = require('./api-actions.js');
+
+module.exports = {
+ apiMethod: function(action, method) {
+ if (!api[action]) return undefined;
+
+ for (var i = 0; i < api[action].length; i++) {
+ if (api[action][i].method == method) {
+ return api[action][i]
+ }
+ }
+ return undefined
+ }
+}
+
View
@@ -5,6 +5,7 @@ var QueryString = require('querystring')
var Crypto = require('crypto')
var fs = require('fs')
+var Helper = require('../lib/helpers.js')
var base_url = 'api.trakt.tv'
var api = require('./api-actions.js')
var Url = require('url')
@@ -31,7 +32,7 @@ Trakt.prototype.request = function(action, method, options, callback) {
var self = this;
if (!api[action]) return callback(new Error('Invalid action: ' + action));
- var opts = findMethod(api, action, method)
+ var opts = Helper.apiMethod(action, method)
if (!opts) return callback(new Error('Invalid method ' + method + ' for action ' + action))
if (opts.type == 'GET') {
@@ -148,19 +149,4 @@ var getPostParams = function(action, opts, options) {
return result
}
-/*
- * Helper functions
-*/
-var findMethod = function(api, action, method) {
- if (!api[action]) return undefined;
-
- for (var i = 0; i < api[action].length; i++) {
- if (api[action][i].method == method) {
- return api[action][i]
- }
- }
- return undefined
-}
-
-

0 comments on commit 9ac580a

Please sign in to comment.