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
61 out += '\n' if @log_mid
62 for i in [0...@log_nest]
67 return unless @verbose
68 process.stdout.write "#{@log_whitespace()}#{msg}"
73 return unless @verbose
76 process.stdout.write "... done\n"
78 process.stdout.write "#{@log_whitespace()}done\n"
81 api: (call, args..., callback) ->
88 login.call this, callback
93 @log_start [call, args...].join ' '
94 # commands implemented in client.coffee need "this" pointing to the session
95 commands[call].call this, @token, args..., (err, the_rest...) =>
96 @log_end() unless err?
97 callback err, the_rest...
99 # eg /app/xxx/stats sometimes returns 404 with wrong auth token
100 if err?.code is 403 or err?.code is 404
102 @api(call, args..., callback)
106 ask: (opts, callback) ->
107 process.stdout.write @log_whitespace() + opts.prompt
108 process.stdin.setEncoding 'utf8'
109 process.stdin.resume()
110 process.stdin.once 'data', (line) =>
112 # send ^[[A^[[2K to move the cursor up one line, then clear that line
113 process.stdout.write new Buffer [27, 91, 65, 27, 91, 50, 75]
114 process.stdout.write @log_whitespace() + opts.prompt + "***\n"
115 process.stdin.pause()
117 callback null, (line.substr 0, line.length - 1)
119 get_token = (callback) ->
120 fs.readFile token_file, 'utf8', (err, token) ->
126 login = (callback) ->
128 (callback) => async.series [
129 (callback) => @ask prompt: 'username: ', callback
130 (callback) => @ask prompt: 'password: ', silent: true, callback
132 ([username, password], callback) ->
133 af.login username, password, callback
135 # wait for file write so there's no race condition if get_token gets called soon
136 fs.writeFile token_file, token, (err) ->
138 process.stderr.write "Warning: couldn't cache auth token in #{token_file}: #{err}\n"
139 # don't pass on error, it's ok if we can't cache it
144 exports.new_session = ->
148 process.stderr.write "usage: #{process.argv[0]} #{process.argv[1]} command [args...]\n"
149 process.stderr.write "valid commands are:\n\t#{(k for k, v of commands).join '\n\t'}\n"
151 # parse and act on commandline arguments unless we were require()d as a module
152 if require.main is module
153 args = process.argv[2..]
156 else if not commands[args[0]]
157 process.stderr.write "unknown command \"#{args[0]}\"\n"
160 session = new Session()
161 session.api args[0], args[1..]..., (err, result) ->
163 process.stderr.write "Error: #{JSON.stringify err}\n"
165 if typeof result is 'string'
166 process.stdout.write result
168 process.stdout.write JSON.stringify result