JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
be a little less verbose with stop/start/restart
[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 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         app_set_state.call this, token, app_name, 'STARTED', callback
37
38 commands.app_stop = (token, app_name, callback) ->
39         app_set_state.call this, token, 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                 @verbose = true
56                 @log_nest = 0
57                 @log_mid = false
58
59         log_whitespace: ->
60                 out = ''
61                 out += '\n' if @log_mid
62                 for i in [0...@log_nest]
63                         out += '\t'
64                 return out
65
66         log_start: (msg) ->
67                 return unless @verbose
68                 process.stdout.write "#{@log_whitespace()}#{msg}"
69                 @log_nest += 1
70                 @log_mid = true
71
72         log_end: ->
73                 return unless @verbose
74                 @log_nest -= 1
75                 if @log_mid
76                         process.stdout.write "... done\n"
77                 else
78                         process.stdout.write "#{@log_whitespace()}done\n"
79                 @log_mid = false
80
81         api: (call, args..., callback) ->
82                 async.waterfall [
83                         (callback) =>
84                                 switch @token
85                                         when 0
86                                                 get_token callback
87                                         when -1
88                                                 login.call this, callback
89                                         else
90                                                 callback null, @token
91                         (token, callback) =>
92                                 @token = token
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...
98                 ], (err, result) =>
99                         # eg /app/xxx/stats sometimes returns 404 with wrong auth token
100                         if err?.code is 403 or err?.code is 404
101                                 @token = -1
102                                 @api(call, args..., callback)
103                         else
104                                 callback err, result
105
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) =>
111                         if opts.silent
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()
116                         @log_mid = false
117                         callback null, (line.substr 0, line.length - 1)
118
119 get_token = (callback) ->
120         fs.readFile token_file, 'utf8', (err, token) ->
121                 if err?
122                         login callback
123                 else
124                         callback null, token
125
126 login = (callback) ->
127         async.waterfall [
128                 (callback) => async.series [
129                         (callback) => @ask prompt: 'username: ', callback
130                         (callback) => @ask prompt: 'password: ', silent: true, callback
131                 ], callback
132                 ([username, password], callback) ->
133                         af.login username, password, callback
134                 (token, 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) ->
137                                 if 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
140                                 callback null, token
141         ], callback
142
143
144 exports.new_session = ->
145         return new Session()
146
147 usage = ->
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"
150
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..]
154         if args.length is 0
155                 usage()
156         else if not commands[args[0]]
157                 process.stderr.write "unknown command \"#{args[0]}\"\n"
158                 usage()
159         else
160                 session = new Session()
161                 session.api args[0], args[1..]..., (err, result) ->
162                         if err?
163                                 process.stderr.write "Error: #{JSON.stringify err}\n"
164                         if result?
165                                 if typeof result is 'string'
166                                         process.stdout.write result
167                                 else
168                                         process.stdout.write JSON.stringify result