X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=client.coffee;h=ccb26b345fd2e36c44572bc78c0420d7431fdaa8;hb=36afa8a844f8dc9fc50bf6ee4da3fc55e900d9b1;hp=a378480a3b05cf13ba33389fbe24b06e05d04f98;hpb=d20d81a40aaf14801c8be6b94ff2e97dd54f15a1;p=af-coffee.git diff --git a/client.coffee b/client.coffee old mode 100644 new mode 100755 index a378480..ccb26b3 --- a/client.coffee +++ b/client.coffee @@ -1,3 +1,5 @@ +#!/usr/bin/coffee + fs = require 'fs' async = require 'async' af = require './api.coffee' @@ -66,7 +68,7 @@ login = (callback) -> # wait for file write so there's no race condition if get_token gets called soon fs.writeFile token_file, token, (err) -> if err - console.log "Warning: couldn't cache auth token in #{token_file}: ", err + process.stderr.write "Warning: couldn't cache auth token in #{token_file}: #{err}\n" # don't pass on error, it's ok if we can't cache it callback null, token ], callback @@ -74,3 +76,28 @@ login = (callback) -> exports.new_session = -> return new Session() + +usage = -> + process.stderr.write "usage: #{process.argv[0]} #{process.argv[1]} command [args...]\n" + process.stderr.write "valid commands are:\n" + for k, v of af + process.stderr.write "\t#{k}\n" unless k is 'login' + +# parse and act on commandline arguments unless we were require()d as a module +if require.main is module + args = process.argv[2..] + if args.length is 0 + usage() + else if not af[args[0]] + process.stderr.write "unknown command \"#{args[0]}\"\n" + usage() + else + session = new Session() + session.api args[0], args[1..], (err, result) -> + if err? + process.stderr.write "Error: #{JSON.stringify err}\n" + if result? + if typeof result is 'string' + process.stdout.write result + else + process.stdout.write JSON.stringify result