1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | {argv} = process
|
11 | {resolve} = require "path"
|
12 | {read, write, remove} = require "fairmont"
|
13 | {parse} = require "c50n"
|
14 |
|
15 | {call} = require "when/generator"
|
16 |
|
17 |
|
18 | {pluck, where, flatten} = require "underscore"
|
19 |
|
20 |
|
21 | PC = require "./pbx"
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | is_integer = (value) -> parseInt(value, 10) != NaN
|
30 |
|
31 |
|
32 | usage = (entry, message) ->
|
33 | if message?
|
34 | throw "#{message}\n" + read( resolve( __dirname, "../", "docs", entry ) )
|
35 | else
|
36 | throw read( resolve( __dirname, "..", "docs", entry ) )
|
37 |
|
38 |
|
39 | allow_only = (allowed_values, value, flag) ->
|
40 | unless value in allowed_values
|
41 | throw "\nError: Only Allowed Values May Be Specified For Flag: #{flag}\n\n"
|
42 |
|
43 |
|
44 | allow_between = (min, max, value, flag) ->
|
45 | unless is_integer value
|
46 | throw "\nError: Value Must Be An Integer For Flag: #{flag}\n\n"
|
47 |
|
48 | unless min <= value <= max
|
49 | throw "\nError: Value Is Outside Allowed Range For Flag: #{flag}\n\n"
|
50 |
|
51 |
|
52 | parse_cli = (command, argv) ->
|
53 |
|
54 | usage command if argv[0] == "-h" || argv[0] == "help"
|
55 |
|
56 |
|
57 |
|
58 | options = parse( read( resolve("#{process.env.HOME}/.pandacluster.cson")))
|
59 |
|
60 |
|
61 | definitions = parse( read( resolve( __dirname, "arguments.cson")))
|
62 | cmd_def = definitions[command]
|
63 | flags = pluck cmd_def, "flag"
|
64 | required_flags = pluck( where( cmd_def, {required: true}), "flag" )
|
65 |
|
66 |
|
67 | while argv.length > 0
|
68 |
|
69 | if flags.indexOf(argv[0]) == -1
|
70 | usage command, "\nError: Unrecognized Flag Provided: #{argv[0]}\n"
|
71 |
|
72 | if argv.length == 1
|
73 | usage command, "\nError: Valid Flag Provided But Not Defined: #{argv[0]}\n"
|
74 |
|
75 |
|
76 | {name, type, required, allowed_values, min, max} = cmd_def[ flags.indexOf(argv[0]) ]
|
77 |
|
78 | allow_only( allowed_values, argv[1], argv[0]) if allowed_values?
|
79 | allow_between( min, max, argv[1], argv[0]) if min? and max?
|
80 | remove( required_flags, argv[0]) if required? == true
|
81 |
|
82 |
|
83 | options[name] = argv[1]
|
84 |
|
85 |
|
86 | argv = argv[2..]
|
87 |
|
88 | options
|
89 |
|
90 |
|
91 | unless required_flags.length == 0
|
92 | usage command, "\nError: Mandatory Flag(s) Remain Undefined: #{required_flags}\n"
|
93 |
|
94 |
|
95 | return options
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | argv = argv[2..]
|
103 |
|
104 |
|
105 | if argv.length == 0 || argv[0] == "-h" || argv[0] == "help"
|
106 | usage "main"
|
107 |
|
108 |
|
109 |
|
110 | call ->
|
111 | try
|
112 | switch argv[0]
|
113 |
|
114 | when "cluster"
|
115 | switch argv[1]
|
116 | when "create"
|
117 | options = parse_cli "create_cluster", argv[2..]
|
118 | console.log "*****cluster create args: ", options
|
119 | res = (yield PC.create_cluster options)
|
120 | console.log "*****cluster create results: ", res
|
121 | when "delete"
|
122 | options = parse_cli "delete_cluster", argv[2..]
|
123 | console.log "*****cluster delete args: ", options
|
124 | res = (yield PC.delete_cluster options)
|
125 | console.log "*****cluster delete results: ", res
|
126 | when "wait"
|
127 | options = parse_cli "wait_on_cluster", argv[2..]
|
128 | console.log "*****waiting on cluster args: ", options
|
129 | res = (yield PC.wait_on_cluster options)
|
130 | console.log "*****cluster wait results: ", res
|
131 | else
|
132 |
|
133 | usage "main", "\nError: Command Not Found: #{argv[1]} \n"
|
134 |
|
135 | when "user"
|
136 | switch argv[1]
|
137 | when "create"
|
138 | options = parse_cli "create_user", argv[2..]
|
139 | console.log "*****user create args: ", options
|
140 | res = (yield PC.create_user options)
|
141 | console.log "*****user create results: ", res
|
142 | else
|
143 |
|
144 | usage "main", "\nError: Command Not Found: #{argv[1]} \n"
|
145 |
|
146 | else
|
147 |
|
148 | usage "main", "\nError: Command Not Found: #{argv[0]} \n"
|
149 | catch error
|
150 | throw error
|