JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
make multi-api call funcs session-aware
[af-coffee.git] / client.coffee
1 #!/usr/bin/coffee
2
3 fs = require 'fs'
4 async = require 'async'
5 af = require './api.coffee'
6
7 token_file = "#{process.env.HOME}/.af-coffee-token"
8
9 timeout = (ms, f) -> setTimeout f, ms
10
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
14
15 commands = {}
16 for k, v of af
17         commands[k] = v unless k is 'login'
18
19 # this is the friendly one that updates the files then restarts the app
20 commands.app_publish = (token, app_name, zip_file, callback) ->
21         async.waterfall [
22                 (callback) =>      @api 'app_update_files', app_name, zip_file, callback
23                 (res, callback) => @api 'app_restart',      app_name, callback
24         ], callback
25
26 commands.app_set_state = (token, app_name, state, callback) ->
27         async.waterfall [
28                 (callback) =>
29                         @api 'app_info', app_name, callback
30                 (info, callback) =>
31                         info.state = state
32                         @api 'app_set_info', app_name, info, callback
33         ], callback
34
35 commands.app_start = (token, app_name, callback) ->
36         @api 'app_set_state', app_name, 'STARTED', callback
37
38 commands.app_stop = (token, app_name, callback) ->
39         @api 'app_set_state', app_name, 'STOPPED', callback
40
41
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
45         async.waterfall [
46                 (callback) =>      @api 'app_stop',  app_name, callback
47                 (res, callback) => @api 'app_start', app_name, callback
48         ], callback
49
50
51
52 class Session
53         constructor: ->
54                 @token = 0
55
56         api: (call, args..., callback) ->
57                 async.waterfall [
58                         (callback) =>
59                                 switch @token
60                                         when 0
61                                                 get_token callback
62                                         when -1
63                                                 login callback
64                                         else
65                                                 callback null, @token
66                         (token, callback) =>
67                                 @token = token
68                                 # commands implemented in client.coffee need "this" pointing to the session
69                                 commands[call].call this, @token, args..., callback
70                 ], (err, result) =>
71                         # eg /app/xxx/stats sometimes returns 404 with wrong auth token
72                         if err?.code is 403 or err?.code is 404
73                                 @token = -1
74                                 @api(call, args..., callback)
75                         else
76                                 callback err, result
77
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) ->
83                 if opts.silent
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"
87                 process.stdin.pause()
88                 callback null, (line.substr 0, line.length - 1)
89
90 get_token = (callback) ->
91         fs.readFile token_file, 'utf8', (err, token) ->
92                 if err?
93                         login callback
94                 else
95                         callback null, token
96
97 login = (callback) ->
98         async.waterfall [
99                 (callback) -> async.series [
100                         (callback) -> ask prompt: 'username: ', callback
101                         (callback) -> ask prompt: 'password: ', silent: true, callback
102                 ], callback
103                 ([username, password], callback) ->
104                         af.login username, password, callback
105                 (token, 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) ->
108                                 if 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
111                                 callback null, token
112         ], callback
113
114
115 exports.new_session = ->
116         return new Session()
117
118 usage = ->
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"
121
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..]
125         if args.length is 0
126                 usage()
127         else if not commands[args[0]]
128                 process.stderr.write "unknown command \"#{args[0]}\"\n"
129                 usage()
130         else
131                 session = new Session()
132                 session.api args[0], args[1..]..., (err, result) ->
133                         if err?
134                                 process.stderr.write "Error: #{JSON.stringify err}\n"
135                         if result?
136                                 if typeof result is 'string'
137                                         process.stdout.write result
138                                 else
139                                         process.stdout.write JSON.stringify result