4 async = require 'async'
5 af = require './api.coffee'
7 token_file = "#{process.env.HOME}/.af-coffee-token"
9 timeout = (ms, f) -> setTimeout f, ms
11 # a session caches the api access token and prompts for the username and
12 # password if it goes stale. It re-tries API calls that fail due to
13 # invalid/expired token
17 commands[k] = v unless k is 'login'
19 # this is the friendly one that updates the files then restarts the app
20 commands.app_publish = (token, app_name, zip_file, callback) ->
22 (callback) => @api 'app_update_files', app_name, zip_file, callback
23 (res, callback) => @api 'app_restart', app_name, callback
26 commands.app_set_state = (token, app_name, state, callback) ->
29 @api 'app_info', app_name, callback
32 @api 'app_set_info', app_name, info, callback
35 commands.app_start = (token, app_name, callback) ->
36 @api 'app_set_state', app_name, 'STARTED', callback
38 commands.app_stop = (token, app_name, callback) ->
39 @api 'app_set_state', app_name, 'STOPPED', callback
42 commands.app_restart = (token, app_name, callback) ->
43 # Server requires you to fetch the app state before each call to change
44 # it, so there's no quicker way than just calling app_stop then app_start
46 (callback) => @api 'app_stop', app_name, callback
47 (res, callback) => @api 'app_start', app_name, callback
56 api: (call, args..., callback) ->
68 # commands implemented in client.coffee need "this" pointing to the session
69 commands[call].call this, @token, args..., callback
71 # eg /app/xxx/stats sometimes returns 404 with wrong auth token
72 if err?.code is 403 or err?.code is 404
74 @api(call, args..., callback)
78 ask = (opts, callback) ->
79 process.stdout.write opts.prompt
80 process.stdin.setEncoding 'utf8'
81 process.stdin.resume()
82 process.stdin.once 'data', (line) ->
84 # send ^[[A^[[2K to move the cursor up one line, then clear that line
85 process.stdout.write new Buffer [27, 91, 65, 27, 91, 50, 75]
86 process.stdout.write opts.prompt + "***\n"
88 callback null, (line.substr 0, line.length - 1)
90 get_token = (callback) ->
91 fs.readFile token_file, 'utf8', (err, token) ->
99 (callback) -> async.series [
100 (callback) -> ask prompt: 'username: ', callback
101 (callback) -> ask prompt: 'password: ', silent: true, callback
103 ([username, password], callback) ->
104 af.login username, password, callback
106 # wait for file write so there's no race condition if get_token gets called soon
107 fs.writeFile token_file, token, (err) ->
109 process.stderr.write "Warning: couldn't cache auth token in #{token_file}: #{err}\n"
110 # don't pass on error, it's ok if we can't cache it
115 exports.new_session = ->
119 process.stderr.write "usage: #{process.argv[0]} #{process.argv[1]} command [args...]\n"
120 process.stderr.write "valid commands are:\n\t#{(k for k, v of commands).join '\n\t'}\n"
122 # parse and act on commandline arguments unless we were require()d as a module
123 if require.main is module
124 args = process.argv[2..]
127 else if not commands[args[0]]
128 process.stderr.write "unknown command \"#{args[0]}\"\n"
131 session = new Session()
132 session.api args[0], args[1..]..., (err, result) ->
134 process.stderr.write "Error: #{JSON.stringify err}\n"
136 if typeof result is 'string'
137 process.stdout.write result
139 process.stdout.write JSON.stringify result