From 3f07c319c4ba7091ce7ef1c2df9aa0623e74ae49 Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Tue, 12 Feb 2013 08:35:49 -0500 Subject: [PATCH] make multi-api call funcs session-aware --- api.coffee | 29 ----------------------------- client.coffee | 50 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 36 deletions(-) diff --git a/api.coffee b/api.coffee index 156e91c..60b9ad8 100644 --- a/api.coffee +++ b/api.coffee @@ -167,12 +167,6 @@ exports.login = login = (username, password, callback) -> else callback "login: couldn't find the token in the server response: #{JSON.stringify response}" -# this is the friendly one that updates the files then restarts the app -exports.app_publish = (token, app_name, zip_file, callback) -> - exports.app_update_files token, app_name, zip_file, (err) -> - return callback err if err? - exports.app_restart token, app_name, callback - exports.app_update_files = (token, app_name, zip_file, callback) -> request( 'POST', @@ -192,29 +186,6 @@ exports.app_update_files = (token, app_name, zip_file, callback) -> exports.app_set_info = (token, app_name, info, callback) -> json_put "/apps/#{app_name}", info, token, callback -app_set_state = (token, app_name, state, callback) -> - async.waterfall [ - (callback) -> - exports.app_info token, app_name, callback - (info, callback) -> - info.state = state - exports.app_set_info token, app_name, info, callback - ], callback - -exports.app_start = (token, app_name, callback) -> - app_set_state token, app_name, 'STARTED', callback - -exports.app_stop = (token, app_name, callback) -> - app_set_state token, app_name, 'STOPPED', callback - -exports.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) -> exports.app_stop token, app_name, callback - (callback) -> exports.app_start token, app_name, callback - ], callback - app_get = (path) -> return (token, app_name, callback) -> json_get "/apps/#{app_name}#{path}", token, callback diff --git a/client.coffee b/client.coffee index ccb26b3..84fff91 100755 --- a/client.coffee +++ b/client.coffee @@ -12,6 +12,43 @@ 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 @@ -28,7 +65,8 @@ class Session callback null, @token (token, callback) => @token = token - af[call] @token, args..., callback + # commands implemented in client.coffee need "this" pointing to the session + commands[call].call this, @token, args..., callback ], (err, result) => # eg /app/xxx/stats sometimes returns 404 with wrong auth token if err?.code is 403 or err?.code is 404 @@ -36,7 +74,7 @@ class Session @api(call, args..., callback) else callback err, result - + ask = (opts, callback) -> process.stdout.write opts.prompt process.stdin.setEncoding 'utf8' @@ -79,21 +117,19 @@ exports.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' + 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 af[args[0]] + 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) -> + session.api args[0], args[1..]..., (err, result) -> if err? process.stderr.write "Error: #{JSON.stringify err}\n" if result? -- 1.7.10.4