From d310243e8f4d344b8ab944b668c53486e0763ec4 Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Mon, 9 Mar 2015 10:05:40 +0100 Subject: [PATCH] first commit --- .gitignore | 1 + homework.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 21 +++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 .gitignore create mode 100755 homework.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/homework.js b/homework.js new file mode 100755 index 0000000..efc8ac3 --- /dev/null +++ b/homework.js @@ -0,0 +1,47 @@ +#!/usr/bin/env node +var request = require('request') +var chalk = require('chalk') +var cli = require('commander') +var moment = require('moment') + +cli + .version('1.3.0') + .usage('command [arguments]') + .option('-k, --key [apikey]','use given api key') + .option('-u, --url [endpoint]','use given api endpoint') + .option('-v, --verbose', 'be more verbose') + .option('-i, --id', 'show item IDs') + .parse(process.argv) + +if(!cli.key){ + console.log(chalk.red("Invalid API key")); + process.exit(1) +} + +var url = (cli.url || 'http://homework.meteor.com/api') + '/' + cli.key +if(cli.verbose) console.log(chalk.bold('URL: ') + url) + +function notes(archived,callback){ + request(url, function(error,response,body){ + if(error) + console.log(chalk.red("Error: ") + chalk.bold(error)) + else + if(cli.verbose) console.log(chalk.bold("Response: ") + body) + var obj = JSON.parse(body) + var ret = "" + if(obj.forEach){ + obj.forEach(function(x){ + var date = x.date + if(date != false) date = moment.unix(x.date).format('DD/MM/YYYY') + var str = '\n' + chalk.green('- ') + chalk.bold(x.title) + if(date != false) str += ' (' + chalk.yellow("Due: ") + chalk.underline(date) + ')' + if(cli.id) str += ' (' + chalk.bold('ID: ') + chalk.underline(x._id) + ')' + if(x.content) str += '\n\t' + chalk.green(x.content) + ret += str.substring(1) + '\n' + }) + } else callback(body) + callback(ret) + }) +} + +notes(false,function(x){console.log(x)}) diff --git a/package.json b/package.json new file mode 100644 index 0000000..3351148 --- /dev/null +++ b/package.json @@ -0,0 +1,21 @@ +{ + "name": "homework-cli", + "version": "1.3.0", + "description": "command line client for Homework", + "repository": { + "type": "git", + "url": "http://github.com/fazo96/homework-cli" + }, + "author": "Enrico Fasoli (fazo96)", + "license": "MIT", + "bugs": { + "url": "https://github.com/fazo96/homework-cli/issues" + }, + "homepage": "https://github.com/fazo96/homework-cli", + "dependencies": { + "chalk": "^1.0.0", + "commander": "^2.6.0", + "moment": "^2.9.0", + "request": "^2.53.0" + } +}