diff --git a/README.md b/README.md
index 1f19911..8fde1e0 100644
--- a/README.md
+++ b/README.md
@@ -4,13 +4,13 @@ Command line interface for [Homework](github.com/fazo96/homework).
## Setup
-1. Install using `npm install -g homework`
+1. Install using `npm install -g homework-cli`
2. Run with the `homework` command
-- By default, _homework-cli_ connects to homework.meteor.com, but you can customize the url.
+- By default, _homework-cli_ connects to [homework.meteor.com](http://homework.meteor.com), but you can customize the url.
- An account with an _API key_ is required to access the __RESTful API__ used by this tool. The __API__ must also be enabled on the Homework server.
-__Please Note__: as of the time of writing, [Homework](github.com/fazo96/homework) only has experimental __RESTful API__ support, so this tool can't yet be used easily with homework.meteor.com.
+__Please Note__: as of the time of writing, [Homework](http://github.com/fazo96/homework) only has experimental __RESTful API__ support, so this tool can't yet be used easily with [homework.meteor.com](http://homework.meteor.com).
## Usage
@@ -20,8 +20,10 @@ __Please Note__: as of the time of writing, [Homework](github.com/fazo96/homewor
Commands:
- new [options]
[content] create a new note
+ new|add [options] [content] create a new note
delete delete one or more notes
+ show [options] show all notes
+ save-cfg [options] save arguments (apiKey and URL) to configuration file at ~/.config/homework-cli.json
Options:
@@ -30,11 +32,17 @@ __Please Note__: as of the time of writing, [Homework](github.com/fazo96/homewor
-k, --key use given api key
-u, --url use given api endpoint instead of http://homework.meteor.com/api
-v, --verbose be more verbose
- -i, --id show item IDs
-s, --silent don't print note list
- -a, --archived view archived notes
-If running with no _commands_, the tool will show your list of notes ordered by due date, just like the web interface.
+A command may have its own options. To see them, run `homework --help `
+
+### Configuration file
+
+The tool can read the `key` and `url` command line parameters from a configuration file for the comfort of the user.
+The first time you run the program with your own settings, use the `save-cfg` command to save the configuration to `~/.config/homework-cli.json`.
+You can update the configuration with `save-cfg` of course, or temporary use different settings with the command line arguments.
+
+__Example:__ `homework --key my_api_key --url my_custom_url save-cfg` will save the given configuration to the settings file.
## License
diff --git a/homework.js b/homework.js
index 8dc1b9e..2d4eaf9 100755
--- a/homework.js
+++ b/homework.js
@@ -3,6 +3,9 @@ var request = require('request')
var chalk = require('chalk')
var cli = require('commander')
var moment = require('moment')
+var fs = require('fs')
+
+var cfgpath = process.env.HOME+'/.config/homework-cli.json'
cli
.version('1.3.0')
@@ -11,12 +14,11 @@ cli
.option('-k, --key ','use given api key')
.option('-u, --url ','use given api endpoint instead of http://homework.meteor.com/api')
.option('-v, --verbose', 'be more verbose')
- .option('-i, --id', 'show item IDs')
.option('-s, --silent',"don't print note list")
- .option('-a, --archived','view archived notes')
cli
.command('new [content]')
+ .alias('add')
.description('create a new note')
.option('--date ', 'the due date for the program')
.action(postNote)
@@ -26,16 +28,40 @@ cli
.description('delete one or more notes')
.action(delNotes)
-cli.parse(process.argv)
+cli
+ .command('show')
+ .description('show all notes')
+ .option('-a, --archived','view archived notes')
+ .option('-i, --id','show IDs')
+ .action(function(options){ getNotes(options.archived || false, options.showids, console.log) })
+
+cli
+ .command('save-cfg')
+ .description('save arguments (apiKey and URL) to configuration file at '+cfgpath)
+ .option('--stdout','print to stdout instead')
+ .action(function(options){
+ preRan = true
+ var cfg = JSON.stringify({apiKey: cli.key, url: cli.url})
+ if(options.stdout) console.log(cfg)
+ else fs.writeFile(cfgpath,cfg,function(err){
+ if(err) console.log(chalk.red('Error: ') + chalk.bold(err))
+ else console.log(chalk.green('Configuration saved to '+chalk.bold(cfgpath)))
+ })
+ })
function baseurl(){ return (cli.url || 'http://homework.meteor.com/api') + '/' + cli.key }
-if(!cli.key){
- console.log(chalk.red("Invalid API key"));
- cli.help()
+var preRan = false
+function preRun(){
+ if(!cli.key){
+ console.log(chalk.red("Invalid API key"));
+ return false;
+ } else preRan = true
+ return true;
}
function postNote(title,content,options){
+ if(!preRun()) return;
request.post({uri: baseurl(), json: {
title: title,
content: content,
@@ -50,7 +76,8 @@ function postNote(title,content,options){
})
}
-function notes(archived,callback){
+function getNotes(archived,showids,callback){
+ if(!preRun()) return;
if(cli.verbose) console.log(chalk.bold('URL: ') + baseurl())
request({ uri: baseurl() + (archived?'/archived':''), json: true }, function(error,response,body){
if(error)
@@ -64,17 +91,19 @@ function notes(archived,callback){
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(showids) str += ' (' + chalk.bold('ID: ') + chalk.underline(x._id) + ')'
if(archived) str += ' (' + chalk.green('archived') + ')'
if(x.content) str += '\n\t' + chalk.green(x.content)
ret += str.substring(1) + '\n'
})
- } else callback(body)
+ } else if(body.error) console.log(chalk.red("Error: ") + chalk.bold(body.error))
+ else callback(body)
callback(ret.substring(0,ret.length-1))
})
}
function delNotes(list){
+ if(!preRun()) return;
var deleted = 0
list.forEach(function(x){
request.del({ uri: baseurl()+'/'+x, json: true },function(error,response,body){
@@ -90,4 +119,19 @@ function delNotes(list){
if(!cli.silent) console.log()
}
-if(!cli.silent) notes(cli.archived || false, console.log)
+// Run
+
+fs.readFile(cfgpath,function(error,content){
+ var cfg = {}
+ if(error){
+ if(cli.verbose) console.log(chalk.red("Error: ")+chalk.bold(error))
+ } else try {
+ cfg = JSON.parse(content)
+ if(cfg.apiKey) cli.key = cfg.apiKey
+ if(cfg.url) cli.url = cfg.url
+ } catch(e){
+ console.log(chalk.yellow("Warning: ")+ "found configuration file at " + chalk.bold(cfgpath) + " but it is not a valid json:\n\t"+chalk.bold(e))
+ }
+ cli.parse(process.argv)
+ if(!preRan) console.log(chalk.green("Tip: ")+"see "+chalk.bold(cli.name()+" --help"))
+})
diff --git a/package.json b/package.json
index 1661b45..34dd24d 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "homework-cli",
- "version": "1.3.0",
+ "version": "1.3.1",
"description": "command line client for Homework",
"bin":{
"homework": "homework.js"