JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
be verbose (print api calls)
[af-coffee.git] / client.coffee
old mode 100644 (file)
new mode 100755 (executable)
index a378480..a7dbca5
@@ -1,3 +1,5 @@
+#!/usr/bin/coffee
+
 fs = require 'fs'
 async = require 'async'
 af = require './api.coffee'
@@ -10,9 +12,71 @@ timeout = (ms, f) -> setTimeout f, ms
 # password if it goes stale. It re-tries API calls that fail due to
 # invalid/expired token
 
+commands = {}
+for k, v of af
+       commands[k] = v unless k is 'login'
+
+# this is the friendly one that updates the files then restarts the app
+commands.app_publish = (token, app_name, zip_file, callback) ->
+       async.waterfall [
+               (callback) =>      @api 'app_update_files', app_name, zip_file, callback
+               (res, callback) => @api 'app_restart',      app_name, callback
+       ], callback
+
+commands.app_set_state = (token, app_name, state, callback) ->
+       async.waterfall [
+               (callback) =>
+                       @api 'app_info', app_name, callback
+               (info, callback) =>
+                       info.state = state
+                       @api 'app_set_info', app_name, info, callback
+       ], callback
+
+commands.app_start = (token, app_name, callback) ->
+       @api 'app_set_state', app_name, 'STARTED', callback
+
+commands.app_stop = (token, app_name, callback) ->
+       @api 'app_set_state', app_name, 'STOPPED', callback
+
+
+commands.app_restart = (token, app_name, callback) ->
+       # Server requires you to fetch the app state before each call to change
+       # it, so there's no quicker way than just calling app_stop then app_start
+       async.waterfall [
+               (callback) =>      @api 'app_stop',  app_name, callback
+               (res, callback) => @api 'app_start', app_name, callback
+       ], callback
+
+
+
 class Session
        constructor: ->
                @token = 0
+               @verbose = true
+               @log_nest = 0
+               @log_mid = false
+
+       log_whitespace: ->
+               out = ''
+               out += '\n' if @log_mid
+               for i in [0...@log_nest]
+                       out += '\t'
+               return out
+
+       log_start: (msg) ->
+               return unless @verbose
+               process.stdout.write "#{@log_whitespace()}#{msg}"
+               @log_nest += 1
+               @log_mid = true
+
+       log_end: ->
+               return unless @verbose
+               @log_nest -= 1
+               if @log_mid
+                       process.stdout.write "... done\n"
+               else
+                       process.stdout.write "#{@log_whitespace()}done\n"
+               @log_mid = false
 
        api: (call, args..., callback) ->
                async.waterfall [
@@ -26,7 +90,11 @@ class Session
                                                callback null, @token
                        (token, callback) =>
                                @token = token
-                               af[call] @token, args..., callback
+                               @log_start [call, args...].join ' '
+                               # commands implemented in client.coffee need "this" pointing to the session
+                               commands[call].call this, @token, args..., (err, the_rest...) =>
+                                       @log_end() unless err?
+                                       callback err, the_rest...
                ], (err, result) =>
                        # eg /app/xxx/stats sometimes returns 404 with wrong auth token
                        if err?.code is 403 or err?.code is 404
@@ -34,7 +102,7 @@ class Session
                                @api(call, args..., callback)
                        else
                                callback err, result
-                               
+
 ask = (opts, callback) ->
        process.stdout.write opts.prompt
        process.stdin.setEncoding 'utf8'
@@ -66,7 +134,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 +142,26 @@ 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\t#{(k for k, v of commands).join '\n\t'}\n"
+
+# 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 commands[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