JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
be a little less verbose with stop/start/restart
[af-coffee.git] / client.coffee
index 84fff91..6a8d477 100755 (executable)
@@ -23,7 +23,7 @@ commands.app_publish = (token, app_name, zip_file, callback) ->
                (res, callback) => @api 'app_restart',      app_name, callback
        ], callback
 
-commands.app_set_state = (token, app_name, state, callback) ->
+app_set_state = (token, app_name, state, callback) ->
        async.waterfall [
                (callback) =>
                        @api 'app_info', app_name, callback
@@ -33,10 +33,10 @@ commands.app_set_state = (token, app_name, state, callback) ->
        ], callback
 
 commands.app_start = (token, app_name, callback) ->
-       @api 'app_set_state', app_name, 'STARTED', callback
+       app_set_state.call this, token, app_name, 'STARTED', callback
 
 commands.app_stop = (token, app_name, callback) ->
-       @api 'app_set_state', app_name, 'STOPPED', callback
+       app_set_state.call this, token, app_name, 'STOPPED', callback
 
 
 commands.app_restart = (token, app_name, callback) ->
@@ -52,6 +52,31 @@ commands.app_restart = (token, app_name, 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 [
@@ -60,13 +85,16 @@ class Session
                                        when 0
                                                get_token callback
                                        when -1
-                                               login callback
+                                               login.call this, callback
                                        else
                                                callback null, @token
                        (token, callback) =>
                                @token = token
+                               @log_start [call, args...].join ' '
                                # commands implemented in client.coffee need "this" pointing to the session
-                               commands[call].call this, @token, args..., callback
+                               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
@@ -75,17 +103,18 @@ class Session
                        else
                                callback err, result
 
-ask = (opts, callback) ->
-       process.stdout.write opts.prompt
-       process.stdin.setEncoding 'utf8'
-       process.stdin.resume()
-       process.stdin.once 'data', (line) ->
-               if opts.silent
-                       # send ^[[A^[[2K to move the cursor up one line, then clear that line
-                       process.stdout.write new Buffer [27, 91, 65, 27, 91, 50, 75]
-                       process.stdout.write opts.prompt + "***\n"
-               process.stdin.pause()
-               callback null, (line.substr 0, line.length - 1)
+       ask: (opts, callback) ->
+               process.stdout.write @log_whitespace() + opts.prompt
+               process.stdin.setEncoding 'utf8'
+               process.stdin.resume()
+               process.stdin.once 'data', (line) =>
+                       if opts.silent
+                               # send ^[[A^[[2K to move the cursor up one line, then clear that line
+                               process.stdout.write new Buffer [27, 91, 65, 27, 91, 50, 75]
+                               process.stdout.write @log_whitespace() + opts.prompt + "***\n"
+                       process.stdin.pause()
+                       @log_mid = false
+                       callback null, (line.substr 0, line.length - 1)
 
 get_token = (callback) ->
        fs.readFile token_file, 'utf8', (err, token) ->
@@ -96,9 +125,9 @@ get_token = (callback) ->
 
 login = (callback) ->
        async.waterfall [
-               (callback) -> async.series [
-                       (callback) -> ask prompt: 'username: ', callback
-                       (callback) -> ask prompt: 'password: ', silent: true, callback
+               (callback) => async.series [
+                       (callback) => @ask prompt: 'username: ', callback
+                       (callback) => @ask prompt: 'password: ', silent: true, callback
                ], callback
                ([username, password], callback) ->
                        af.login username, password, callback