Skip to content

Instantly share code, notes, and snippets.

@kolja
Last active December 18, 2015 09:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kolja/5765437 to your computer and use it in GitHub Desktop.
Save kolja/5765437 to your computer and use it in GitHub Desktop.
cli to read and write "diary entries" to CouchDB
#!/usr/local/bin/coffee
nopt = require 'nopt'
nano = require('nano')('http://localhost:5984/dailies')
optionDef =
"u": String # user
"m": String # Message
"l": Number
"t": [String, Array] # Tags
opt = nopt optionDef
message = opt.m || opt.argv.remain[0]
date = new Date()
class Cal
constructor: ->
@days = {}
@array2date: (ary) ->
month = [ "Jan", "Feb", "März", "Apr", "Mai", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" ]
date = new Date "#{ary[0]}.#{ary[1]}.#{ary[2]}"
"#{date.getDate()}. #{month[date.getMonth()]} #{date.getFullYear()}"
@date2array: (date) ->
[date.getFullYear(), date.getMonth()+1, date.getDate(), 0,0,0]
addEntry: (data) =>
dateString = Cal.array2date data.date
@days[dateString] ?= {}
currentDay = @days[dateString]
category = data.tags?[0] || 'other'
currentDay[category] ?= []
currentDay[category].push data.entry
print: =>
for day, tags of @days
console.log day
for tag, entries of tags
if (tag=="other")
for entry in entries
console.log "\t#{entry}"
else
console.log "\t#{tag}"
for entry in entries
console.log "\t\t#{entry}"
if (message)
hash =
date: Cal.date2array date
name: opt.u || "Joe"
entry: message
if opt.t then hash['tags'] = opt.t
nano.insert hash
if (opt.h)
console.log """
Usage:
daily -u Username -t tag -m "Message"
-u : Username (defaults to 'Joe')
-t : Tag (can be supplied multiple times)
-m : Message (a Message is required but the -m flag can be ommitted)
-l : list <days> (list the last days entries)
"""
else if (opt.l)
c = new Cal
endDate = Cal.date2array date
date.setDate(date.getDate() - (opt.l - 1)); # date - opt.l * days
startDate = Cal.date2array date
nano.view 'entries', 'by_date', {startkey:startDate,endkey:endDate}, (err, body) ->
if body.rows.length
body.rows.forEach (row) ->
c.addEntry row.value
c.print()
else
console.log """
----------------------------
no entries yet
----------------------------
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment