Task

Write a simple tool to track a small set of data.

The tool should have a command-line interface to enter at least two different values.

The entered data should be stored in a structured format and saved to disk.

It does not matter what kind of data is being tracked.   It could be a collection (CDs, coins, baseball cards, books), a diary, an electronic organizer (birthdays/anniversaries/phone numbers/addresses), etc.



You should track the following details:

 A description of the item. (e.g., title, name)
 A category or tag (genre, topic, relationship such as “friend” or “family”)
 A date (either the date when the entry was made or some other date that is meaningful, like the birthday); the date may be generated or entered manually
 Other optional fields


The command should support the following Command-line arguments to run:

 Add a new entry
 Print the latest entry
 Print the latest entry for each category
 Print all entries sorted by a date


The category may be realized as a tag or as structure (by making all entries in that category subitems)

The file format on disk should be human readable, but it need not be standardized.   A natively available format that doesn't need an external library is preferred.   Avoid developing your own format if you can use an already existing one.   If there is no existing format available, pick one of:

   JSON
   S-Expressions
   YAML
   others


Related task

   Take notes on the command line 


#!/usr/local/bin/toffee
 
prog = require 'commander'
fs = require 'fs-extra'
 
if not fs.exists! 'data.json'
  fs.outputJson! 'data.json', {}
 
prog
  .command('add <name> <category> [date]')
  .description('Add a new entry')
  .option('-n <text>', 'notes')
  .option('-t <tags>', 'tags')
  .action addentry
 
prog
  .command('latest')
  .description('Print the latest entry')
  .action latest
 
prog
  .command('catlatest')
  .description('Print the latest entry for each category')
  .action catlatestout
 
prog
  .command('list')
  .description('Print all entries sorted by date')
  .action bydate
 
 
addentry = (name, category, dt, options) ->
  if dt? then dat = new Date(dt) else dat = new Date()
  update =
    name: name
    category: category
    tags: options?.T
    notes: options?.N
    date: dat.getTime()
  e, data = fs.readJson! 'data.json'
  if not data[category]?
    data[category] = []
  data[category].push update
  fs.outputJson 'data.json', data
 
byDateNew = (a, b) ->
  if a.date<b.date then return 1
  if b.date>a.date then return -1
  return 0
 
catlatest = (cb) ->
  e, data = fs.readJson! 'data.json'
  ret = []
  for cat, list of data
    list.sort byDateNew
    ret.push list[0]
  cb ret
 
printFormatted = (entry) ->
  tmp = entry.date
  entry.date = new Date(entry.date).toDateString()
  console.log entry
  entry.date = tmp
 
latest = ->
  newpercat = catlatest!
  newpercat.sort byDateNew
  printFormatted newpercat[0]
 
catlatestout = ->
  items = catlatest!
  for item in items
    printFormatted item
 
bydate = ->
  e, data = fs.readJson! 'data.json'
  entries = []
  for cat, list of data
    for item in list
      entries.push item
  entries.sort byDateNew
  for entry in entries
    printFormatted entry
 
prog.parse process.argv