2 async = require 'async'
3 af = require './api.coffee'
5 token_file = "#{process.env.HOME}/.af-coffee-token"
7 timeout = (ms, f) -> setTimeout f, ms
9 # a session caches the api access token and prompts for the username and
10 # password if it goes stale. It re-tries API calls that fail due to
11 # invalid/expired token
17 api: (call, args..., callback) ->
29 af[call] @token, args..., callback
31 # eg /app/xxx/stats sometimes returns 404 with wrong auth token
32 if err?.code is 403 or err?.code is 404
34 @api(call, args..., callback)
38 ask = (opts, callback) ->
39 process.stdout.write opts.prompt
40 process.stdin.setEncoding 'utf8'
41 process.stdin.resume()
42 process.stdin.once 'data', (line) ->
44 # send ^[[A^[[2K to move the cursor up one line, then clear that line
45 process.stdout.write new Buffer [27, 91, 65, 27, 91, 50, 75]
46 process.stdout.write opts.prompt + "***\n"
48 callback null, (line.substr 0, line.length - 1)
50 get_token = (callback) ->
51 fs.readFile token_file, 'utf8', (err, token) ->
59 (callback) -> async.series [
60 (callback) -> ask prompt: 'username: ', callback
61 (callback) -> ask prompt: 'password: ', silent: true, callback
63 ([username, password], callback) ->
64 af.login username, password, callback
66 # wait for file write so there's no race condition if get_token gets called soon
67 fs.writeFile token_file, token, (err) ->
69 console.log "Warning: couldn't cache auth token in #{token_file}: ", err
70 # don't pass on error, it's ok if we can't cache it
75 exports.new_session = ->