1
0
mirror of https://github.com/fazo96/homework-cli.git synced 2025-01-10 12:14:23 +01:00

huge update

This commit is contained in:
Enrico Fasoli 2015-03-09 13:04:08 +01:00
parent d310243e8f
commit 0d354387a7

View File

@ -6,42 +6,84 @@ var moment = require('moment')
cli cli
.version('1.3.0') .version('1.3.0')
.usage('command [arguments]') .description('interact with a running instance of Homework (by default homework.meteor.com)')
.option('-k, --key [apikey]','use given api key') .usage('[command] [arguments]')
.option('-u, --url [endpoint]','use given api endpoint') .option('-k, --key <apikey>','use given api key')
.option('-u, --url <endpoint>','use given api endpoint instead of http://homework.meteor.com/api')
.option('-v, --verbose', 'be more verbose') .option('-v, --verbose', 'be more verbose')
.option('-i, --id', 'show item IDs') .option('-i, --id', 'show item IDs')
.parse(process.argv) .option('-d, --delete <noteid> [otherids...]', 'delete note(s) with given id')
.option('-s, --silent',"don't print note list")
.option('-a, --archived','view archived notes')
cli
.command('new <title> [content]')
.description('create a new note')
.option('--date <date>', 'the due date for the program')
.action(postNote)
cli.parse(process.argv)
var baseurl = (cli.url || 'http://homework.meteor.com/api') + '/' + cli.key
if(!cli.key){ if(!cli.key){
console.log(chalk.red("Invalid API key")); console.log(chalk.red("Invalid API key"));
process.exit(1) cli.help()
} }
var url = (cli.url || 'http://homework.meteor.com/api') + '/' + cli.key function postNote(title,content,options){
if(cli.verbose) console.log(chalk.bold('URL: ') + url) request.post({uri: (cli.url || 'http://homework.meteor.com/api') + '/' + cli.key, json: {
title: title,
function notes(archived,callback){ content: content,
request(url, function(error,response,body){ date: options.date || false
}},function(error,response,body){
if(error) if(error)
console.log(chalk.red("Error: ") + chalk.bold(error)) console.log(chalk.red("Error: ") + chalk.bold(error))
else else
if(cli.verbose) console.log(chalk.bold("Response: ") + body) if(cli.verbose) console.log(chalk.bold("Response: ") + JSON.stringify(body))
var obj = JSON.parse(body) if(body.error) console.log(chalk.red("Error: ") + chalk.bold(body.error))
else console.log(chalk.green("Inserted note: " + chalk.bold(body.inserted)))
})
}
function notes(archived,callback){
if(cli.verbose) console.log(chalk.bold('URL: ') + baseurl)
request({ uri: baseurl + (archived?'/archived':''), json: true }, function(error,response,body){
if(error)
console.log(chalk.red("Error: ") + chalk.bold(error))
else
if(cli.verbose) console.log(chalk.bold("Response: ") + JSON.stringify(body))
var ret = "" var ret = ""
if(obj.forEach){ if(body.forEach){
obj.forEach(function(x){ body.forEach(function(x){
var date = x.date var date = x.date
if(date != false) date = moment.unix(x.date).format('DD/MM/YYYY') if(date != false) date = moment.unix(x.date).format('DD/MM/YYYY')
var str = '\n' + chalk.green('- ') + chalk.bold(x.title) var str = '\n' + chalk.green('- ') + chalk.bold(x.title)
if(date != false) str += ' (' + chalk.yellow("Due: ") + chalk.underline(date) + ')' if(date != false) str += ' (' + chalk.yellow("Due: ") + chalk.underline(date) + ')'
if(cli.id) str += ' (' + chalk.bold('ID: ') + chalk.underline(x._id) + ')' if(cli.id) str += ' (' + chalk.bold('ID: ') + chalk.underline(x._id) + ')'
if(archived) str += ' (' + chalk.green('archived') + ')'
if(x.content) str += '\n\t' + chalk.green(x.content) if(x.content) str += '\n\t' + chalk.green(x.content)
ret += str.substring(1) + '\n' ret += str.substring(1) + '\n'
}) })
} else callback(body) } else callback(body)
callback(ret) callback(ret.substring(0,ret.length-1))
}) })
} }
notes(false,function(x){console.log(x)}) function delNotes(list){
var deleted = 0
list.forEach(function(x){
request.del({ uri: baseurl+'/'+x, json: true },function(error,response,body){
if(error)
console.log(chalk.red("Error: ") + chalk.bold(error))
else
if(cli.verbose) console.log(chalk.bold("Response: ") + JSON.stringify(body))
if(body.error) console.log(chalk.red("Error:") + obj.error)
else deleted += body.deleted
})
})
console.log(chalk.green('Delete: ') + chalk.bold(deleted) + ' notes deleted from the database')
if(!cli.silent) console.log()
}
if(cli.delete) delNotes(cli.delete.split(' '))
if(!cli.silent) notes(cli.archived || false, console.log)