cf_script 0.0.1.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (205) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/CHANGELOG.md +0 -0
  4. data/Gemfile +12 -0
  5. data/LICENSE +22 -0
  6. data/README.md +84 -0
  7. data/Rakefile +33 -0
  8. data/cf_script.gemspec +34 -0
  9. data/lib/cf_script/callbacks.rb +43 -0
  10. data/lib/cf_script/command/base.rb +57 -0
  11. data/lib/cf_script/command/cf/apps/app.rb +19 -0
  12. data/lib/cf_script/command/cf/apps/apps.rb +44 -0
  13. data/lib/cf_script/command/cf/apps/env.rb +20 -0
  14. data/lib/cf_script/command/cf/apps/push.rb +47 -0
  15. data/lib/cf_script/command/cf/apps/restage.rb +19 -0
  16. data/lib/cf_script/command/cf/apps/restart.rb +19 -0
  17. data/lib/cf_script/command/cf/apps/set_env.rb +18 -0
  18. data/lib/cf_script/command/cf/apps/start.rb +19 -0
  19. data/lib/cf_script/command/cf/apps/stop.rb +19 -0
  20. data/lib/cf_script/command/cf/apps/unset_env.rb +18 -0
  21. data/lib/cf_script/command/cf/apps.rb +55 -0
  22. data/lib/cf_script/command/cf/general/api.rb +36 -0
  23. data/lib/cf_script/command/cf/general/auth.rb +28 -0
  24. data/lib/cf_script/command/cf/general/login.rb +42 -0
  25. data/lib/cf_script/command/cf/general/logout.rb +15 -0
  26. data/lib/cf_script/command/cf/general/target.rb +64 -0
  27. data/lib/cf_script/command/cf/general.rb +15 -0
  28. data/lib/cf_script/command/cf/routes/check_route.rb +17 -0
  29. data/lib/cf_script/command/cf/routes/create_route.rb +19 -0
  30. data/lib/cf_script/command/cf/routes/delete_route.rb +19 -0
  31. data/lib/cf_script/command/cf/routes/map_route.rb +30 -0
  32. data/lib/cf_script/command/cf/routes/routes.rb +40 -0
  33. data/lib/cf_script/command/cf/routes/unmap_route.rb +30 -0
  34. data/lib/cf_script/command/cf/routes.rb +17 -0
  35. data/lib/cf_script/command/cf/spaces/space.rb +36 -0
  36. data/lib/cf_script/command/cf/spaces/spaces.rb +23 -0
  37. data/lib/cf_script/command/cf/spaces.rb +9 -0
  38. data/lib/cf_script/command/error.rb +5 -0
  39. data/lib/cf_script/command/line.rb +105 -0
  40. data/lib/cf_script/command/registry.rb +50 -0
  41. data/lib/cf_script/command/runner.rb +97 -0
  42. data/lib/cf_script/command.rb +44 -0
  43. data/lib/cf_script/config.rb +60 -0
  44. data/lib/cf_script/executor/non_blocking.rb +107 -0
  45. data/lib/cf_script/executor/recorder.rb +16 -0
  46. data/lib/cf_script/executor/simple.rb +17 -0
  47. data/lib/cf_script/executor.rb +5 -0
  48. data/lib/cf_script/manifest.rb +38 -0
  49. data/lib/cf_script/object/api_endpoint.rb +10 -0
  50. data/lib/cf_script/object/app_info.rb +69 -0
  51. data/lib/cf_script/object/app_list.rb +24 -0
  52. data/lib/cf_script/object/app_spec.rb +55 -0
  53. data/lib/cf_script/object/attribute.rb +18 -0
  54. data/lib/cf_script/object/attribute_list.rb +40 -0
  55. data/lib/cf_script/object/instance_status.rb +29 -0
  56. data/lib/cf_script/object/route_info.rb +14 -0
  57. data/lib/cf_script/object/space.rb +17 -0
  58. data/lib/cf_script/object/target.rb +23 -0
  59. data/lib/cf_script/object.rb +24 -0
  60. data/lib/cf_script/output/buffer.rb +74 -0
  61. data/lib/cf_script/output/parser/attributes.rb +44 -0
  62. data/lib/cf_script/output/parser/section.rb +15 -0
  63. data/lib/cf_script/output/parser/table.rb +46 -0
  64. data/lib/cf_script/output/parser.rb +15 -0
  65. data/lib/cf_script/output/tests.rb +51 -0
  66. data/lib/cf_script/output/utils.rb +13 -0
  67. data/lib/cf_script/output.rb +62 -0
  68. data/lib/cf_script/scope/app/env.rb +15 -0
  69. data/lib/cf_script/scope/app/routes.rb +21 -0
  70. data/lib/cf_script/scope/app/state.rb +33 -0
  71. data/lib/cf_script/scope/app/utils.rb +32 -0
  72. data/lib/cf_script/scope/app.rb +94 -0
  73. data/lib/cf_script/scope/proxy.rb +36 -0
  74. data/lib/cf_script/scope/script.rb +59 -0
  75. data/lib/cf_script/scope/space.rb +9 -0
  76. data/lib/cf_script/scope/target.rb +90 -0
  77. data/lib/cf_script/scope.rb +68 -0
  78. data/lib/cf_script/ui.rb +261 -0
  79. data/lib/cf_script/utils.rb +19 -0
  80. data/lib/cf_script/version.rb +3 -0
  81. data/lib/cf_script.rb +105 -0
  82. data/test/fixtures/commands/apps/app/good.yml +18 -0
  83. data/test/fixtures/commands/apps/app/not_found.yml +8 -0
  84. data/test/fixtures/commands/apps/app/running.yml +19 -0
  85. data/test/fixtures/commands/apps/apps/good.yml +14 -0
  86. data/test/fixtures/commands/apps/apps/no_apps.yml +10 -0
  87. data/test/fixtures/commands/apps/env/empty.yml +37 -0
  88. data/test/fixtures/commands/apps/env/good.yml +41 -0
  89. data/test/fixtures/commands/apps/push/good.yml +60 -0
  90. data/test/fixtures/commands/apps/restage/good.yml +48 -0
  91. data/test/fixtures/commands/apps/restage/not_found.yml +8 -0
  92. data/test/fixtures/commands/apps/restart/good.yml +35 -0
  93. data/test/fixtures/commands/apps/restart/not_found.yml +8 -0
  94. data/test/fixtures/commands/apps/set_env/empty.yml +5 -0
  95. data/test/fixtures/commands/apps/set_env/good.yml +9 -0
  96. data/test/fixtures/commands/apps/set_env/not_found.yml +8 -0
  97. data/test/fixtures/commands/apps/start/good.yml +32 -0
  98. data/test/fixtures/commands/apps/start/not_found.yml +8 -0
  99. data/test/fixtures/commands/apps/start/with_container.yml +98 -0
  100. data/test/fixtures/commands/apps/stop/good.yml +8 -0
  101. data/test/fixtures/commands/apps/stop/not_found.yml +8 -0
  102. data/test/fixtures/commands/apps/unset_env/empty.yml +5 -0
  103. data/test/fixtures/commands/apps/unset_env/good.yml +9 -0
  104. data/test/fixtures/commands/apps/unset_env/not_found.yml +8 -0
  105. data/test/fixtures/commands/common/fail.yml +5 -0
  106. data/test/fixtures/commands/common/no_endpoint.yml +11 -0
  107. data/test/fixtures/commands/common/no_login.yml +8 -0
  108. data/test/fixtures/commands/general/api/bad_argument.yml +9 -0
  109. data/test/fixtures/commands/general/api/bad_endpoint.yml +9 -0
  110. data/test/fixtures/commands/general/api/good.yml +7 -0
  111. data/test/fixtures/commands/general/api/not_set.yml +7 -0
  112. data/test/fixtures/commands/general/api/set.yml +7 -0
  113. data/test/fixtures/commands/general/auth/empty.yml +5 -0
  114. data/test/fixtures/commands/general/auth/good.yml +10 -0
  115. data/test/fixtures/commands/general/auth/rejected.yml +10 -0
  116. data/test/fixtures/commands/general/login/good.yml +8 -0
  117. data/test/fixtures/commands/general/login/rejected.yml +23 -0
  118. data/test/fixtures/commands/general/logout/good.yml +8 -0
  119. data/test/fixtures/commands/general/target/empty.yml +5 -0
  120. data/test/fixtures/commands/general/target/good.yml +11 -0
  121. data/test/fixtures/commands/general/target/no_space.yml +10 -0
  122. data/test/fixtures/commands/general/target/not_found.yml +9 -0
  123. data/test/fixtures/commands/general/target/production.yml +11 -0
  124. data/test/fixtures/commands/general/target/staging.yml +11 -0
  125. data/test/fixtures/commands/general/target/staging_org.yml +11 -0
  126. data/test/fixtures/commands/general/target/test.yml +11 -0
  127. data/test/fixtures/commands/routes/check_route/good.yml +9 -0
  128. data/test/fixtures/commands/routes/check_route/no_domain.yml +9 -0
  129. data/test/fixtures/commands/routes/check_route/no_route.yml +9 -0
  130. data/test/fixtures/commands/routes/create_route/exists.yml +9 -0
  131. data/test/fixtures/commands/routes/create_route/good.yml +8 -0
  132. data/test/fixtures/commands/routes/delete_route/good.yml +8 -0
  133. data/test/fixtures/commands/routes/delete_route/not_exist.yml +8 -0
  134. data/test/fixtures/commands/routes/map_route/good.yml +10 -0
  135. data/test/fixtures/commands/routes/map_route/host_taken.yml +10 -0
  136. data/test/fixtures/commands/routes/map_route/invalid.yml +9 -0
  137. data/test/fixtures/commands/routes/map_route/no_app.yml +8 -0
  138. data/test/fixtures/commands/routes/map_route/no_domain.yml +9 -0
  139. data/test/fixtures/commands/routes/routes/good.yml +14 -0
  140. data/test/fixtures/commands/routes/unmap_route/good.yml +8 -0
  141. data/test/fixtures/commands/routes/unmap_route/no_app.yml +8 -0
  142. data/test/fixtures/commands/routes/unmap_route/no_domain.yml +9 -0
  143. data/test/fixtures/commands/spaces/space/empty.yml +5 -0
  144. data/test/fixtures/commands/spaces/space/good.yml +16 -0
  145. data/test/fixtures/commands/spaces/space/staging.yml +16 -0
  146. data/test/fixtures/commands/spaces/spaces/empty.yml +5 -0
  147. data/test/fixtures/commands/spaces/spaces/good.yml +12 -0
  148. data/test/lib/cf_script/command/base_test.rb +21 -0
  149. data/test/lib/cf_script/command/cf/apps/app_test.rb +56 -0
  150. data/test/lib/cf_script/command/cf/apps/apps_test.rb +81 -0
  151. data/test/lib/cf_script/command/cf/apps/env_test.rb +55 -0
  152. data/test/lib/cf_script/command/cf/apps/push_test.rb +46 -0
  153. data/test/lib/cf_script/command/cf/apps/restage_test.rb +47 -0
  154. data/test/lib/cf_script/command/cf/apps/restart_test.rb +47 -0
  155. data/test/lib/cf_script/command/cf/apps/set_env_test.rb +38 -0
  156. data/test/lib/cf_script/command/cf/apps/start_test.rb +55 -0
  157. data/test/lib/cf_script/command/cf/apps/stop_test.rb +35 -0
  158. data/test/lib/cf_script/command/cf/apps/unset_env_test.rb +38 -0
  159. data/test/lib/cf_script/command/cf/general/api_test.rb +55 -0
  160. data/test/lib/cf_script/command/cf/general/auth_test.rb +76 -0
  161. data/test/lib/cf_script/command/cf/general/login_test.rb +80 -0
  162. data/test/lib/cf_script/command/cf/general/logout_test.rb +34 -0
  163. data/test/lib/cf_script/command/cf/general/target_test.rb +110 -0
  164. data/test/lib/cf_script/command/cf/routes/check_route_test.rb +31 -0
  165. data/test/lib/cf_script/command/cf/routes/create_route_test.rb +19 -0
  166. data/test/lib/cf_script/command/cf/routes/delete_route_test.rb +19 -0
  167. data/test/lib/cf_script/command/cf/routes/map_route_test.rb +56 -0
  168. data/test/lib/cf_script/command/cf/routes/routes_test.rb +24 -0
  169. data/test/lib/cf_script/command/cf/routes/unmap_route_test.rb +36 -0
  170. data/test/lib/cf_script/command/cf/spaces/space_test.rb +55 -0
  171. data/test/lib/cf_script/command/cf/spaces/spaces_test.rb +47 -0
  172. data/test/lib/cf_script/command/line_test.rb +59 -0
  173. data/test/lib/cf_script/object/api_endpoint_test.rb +19 -0
  174. data/test/lib/cf_script/object/app_info_test.rb +127 -0
  175. data/test/lib/cf_script/object/app_list_test.rb +51 -0
  176. data/test/lib/cf_script/object/attribute_list_test.rb +42 -0
  177. data/test/lib/cf_script/object/attribute_test.rb +29 -0
  178. data/test/lib/cf_script/object/instance_status_test.rb +37 -0
  179. data/test/lib/cf_script/object/space_test.rb +31 -0
  180. data/test/lib/cf_script/object/target_test.rb +41 -0
  181. data/test/lib/cf_script/output/buffer_test.rb +73 -0
  182. data/test/lib/cf_script/output/parser/attributes_test.rb +102 -0
  183. data/test/lib/cf_script/output/parser/section_test.rb +60 -0
  184. data/test/lib/cf_script/output/parser/table_test.rb +108 -0
  185. data/test/lib/cf_script/output/tests_test.rb +91 -0
  186. data/test/lib/cf_script/output_test.rb +134 -0
  187. data/test/lib/cf_script/scope/app/state_test.rb +61 -0
  188. data/test/lib/cf_script/scope/app_test.rb +101 -0
  189. data/test/lib/cf_script/scope/script_test.rb +88 -0
  190. data/test/lib/cf_script/scope/space_test.rb +27 -0
  191. data/test/lib/cf_script/scope/target_test.rb +158 -0
  192. data/test/lib/cf_script/scope_test.rb +41 -0
  193. data/test/lib/cf_script/utils_test.rb +20 -0
  194. data/test/lib/cf_script_test.rb +73 -0
  195. data/test/support/assertions.rb +14 -0
  196. data/test/support/helpers/config_context.rb +19 -0
  197. data/test/support/helpers/fixture_executor.rb +79 -0
  198. data/test/support/helpers/mock_execution.rb +58 -0
  199. data/test/support/helpers/object_helpers.rb +29 -0
  200. data/test/support/helpers/output_sink.rb +25 -0
  201. data/test/support/helpers.rb +3 -0
  202. data/test/support/shared_examples/command_object.rb +38 -0
  203. data/test/support/shared_examples.rb +30 -0
  204. data/test/test_helper.rb +34 -0
  205. metadata +321 -0
@@ -0,0 +1,28 @@
1
+ module CfScript::Command::General
2
+ class AuthCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:general, :auth)
5
+ end
6
+
7
+ def run(username, password, &block)
8
+ run_cf self, username, password do |output|
9
+ unless can_run?(output, check_failed: false)
10
+ if output.credentials_rejected?
11
+ error 'Credentials were rejected'
12
+ else
13
+ output.dump
14
+ end
15
+
16
+ return false
17
+ else
18
+ if logged_in = output.authenticated?
19
+ block_given? ? yield(logged_in) : logged_in
20
+ else
21
+ error 'Not logged in'
22
+ return false
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,42 @@
1
+ module CfScript::Command::General
2
+ class LoginCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:general, :login)
5
+ end
6
+
7
+ def run(username, password, options = {}, &block)
8
+ args = [{ u: username, p: password }]
9
+
10
+ if options[:api]
11
+ args.last[:a] = options[:api]
12
+ end
13
+
14
+ if options[:org]
15
+ args.last[:o] = options[:org]
16
+ end
17
+
18
+ if options[:space]
19
+ args.last[:s] = options[:space]
20
+ end
21
+
22
+ run_cf self, *args do |output|
23
+ unless can_run?(output, check_failed: false)
24
+ if output.credentials_rejected?
25
+ error 'Credentials were rejected'
26
+ else
27
+ output.dump
28
+ end
29
+
30
+ return false
31
+ else
32
+ if logged_in = output.authenticated?
33
+ block_given? ? yield(logged_in) : logged_in
34
+ else
35
+ error 'Not logged in'
36
+ return false
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ module CfScript::Command::General
2
+ class LogoutCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:general, :logout)
5
+ end
6
+
7
+ def run(*args, &block)
8
+ run_cf self, *args do |output|
9
+ return false unless can_run?(output)
10
+
11
+ block_given? ? yield(output.ok?) : output.ok?
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,64 @@
1
+ module CfScript::Command
2
+ class General::TargetCommand < CfScript::Command::Base
3
+ NOT_LOGGED_IN = 'Not logged in'
4
+
5
+ def initialize
6
+ super(:general, :target)
7
+ end
8
+
9
+ def run(space_name_or_target = nil, &block)
10
+ args = build_args(space_name_or_target)
11
+ space = args.first && args.first.key?(:s) ? args.first[:s] : ''
12
+
13
+ run_cf self, *args do |output|
14
+ return nil unless can_run?(output, check_failed: false)
15
+
16
+ # TODO: check org not found too
17
+
18
+ if output.not_found?('Space', space)
19
+ error "Space #{space} not found"
20
+ return
21
+ end
22
+
23
+ if target = build_target(output)
24
+ block ? yield(target) : target
25
+ else
26
+ error 'object is nil'
27
+ end
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def build_args(name_or_target)
34
+ case name_or_target
35
+ when String, Symbol
36
+ # TODO: add org if present
37
+ [{ s: name_or_target }]
38
+ when CfScript::Target
39
+ [name_or_target.to_options]
40
+ else
41
+ []
42
+ end
43
+ end
44
+
45
+ def build_target(output)
46
+ attrs = output.attributes
47
+
48
+ unless attrs.empty?
49
+ target = CfScript::Target.new(
50
+ attrs[:api_endpoint].value,
51
+ attrs[:org].value,
52
+ attrs[:space].value,
53
+ attrs[:user].value
54
+ )
55
+
56
+ if target.space =~ /No space targeted/
57
+ target.space = ''
58
+ end
59
+
60
+ target
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,15 @@
1
+ CfScript::Command.const_set(:General, Module.new)
2
+
3
+ require 'cf_script/command/cf/general/api'
4
+ require 'cf_script/command/cf/general/auth'
5
+ require 'cf_script/command/cf/general/login'
6
+ require 'cf_script/command/cf/general/logout'
7
+ require 'cf_script/command/cf/general/target'
8
+
9
+ module CfScript::Command::General
10
+ CfScript::Command.register ApiCommand
11
+ CfScript::Command.register AuthCommand
12
+ CfScript::Command.register LoginCommand
13
+ CfScript::Command.register LogoutCommand
14
+ CfScript::Command.register TargetCommand
15
+ end
@@ -0,0 +1,17 @@
1
+ module CfScript::Command
2
+ class Routes::CheckRouteCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:routes, :check_route)
5
+ end
6
+
7
+ def run(host, domain, &block)
8
+ run_cf self, host, domain do |output|
9
+ return false unless can_run?(output)
10
+
11
+ exists = output.matches? /^Route #{host}.#{domain} does exist/
12
+
13
+ block_given? ? yield(exists) : exists
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module CfScript::Command
2
+ class Routes::CreateRouteCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:routes, :create_route)
5
+ end
6
+
7
+ def run(space, domain, host = nil, &block)
8
+ options = host ? { n: host } : {}
9
+
10
+ run_cf self, space, domain, options do |output|
11
+ return false unless can_run?(output)
12
+
13
+ if exists = output.contains?(/^OK$/)
14
+ block_given? ? yield(exists) : exists
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module CfScript::Command
2
+ class Routes::DeleteRouteCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:routes, :delete_route)
5
+ end
6
+
7
+ def run(domain, host = nil, force = true, &block)
8
+ options = host ? { n: host, flags: [:f] } : { flags: [:f] }
9
+
10
+ run_cf self, domain, options do |output|
11
+ return false unless can_run?(output)
12
+
13
+ if deleted = output.ok?
14
+ block_given? ? yield(deleted) : deleted
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ module CfScript::Command
2
+ class Routes::MapRouteCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:routes, :map_route)
5
+ end
6
+
7
+ def run(app_name, domain, host = nil, &block)
8
+ options = host ? { n: host } : {}
9
+
10
+ run_cf self, app_name, domain, options do |output|
11
+ return false unless can_run?(output,
12
+ check_status: false,
13
+ check_failed: false,
14
+ )
15
+
16
+ if output.good? and mapped = output.ok?
17
+ block_given? ? yield(mapped) : mapped
18
+ else
19
+ if output.failed?
20
+ error output.last_line
21
+ else
22
+ error 'failed to map route'
23
+ end
24
+
25
+ return false
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+ module CfScript::Command
2
+ class Routes::RoutesCommand < CfScript::Command::Base
3
+ ROUTES_TABLE = ['space', 'host', 'domain', 'apps']
4
+
5
+ def initialize
6
+ super(:routes, :routes)
7
+ end
8
+
9
+ def run(*args, &block)
10
+ run_cf self do |output|
11
+ return unless can_run?(output)
12
+
13
+ if rows = output.table(ROUTES_TABLE)
14
+ routes = build_route_info(rows)
15
+
16
+ block_given? ? yield(routes) : routes
17
+ else
18
+ error "Routes table was not found"
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def build_route_info(rows)
26
+ routes = []
27
+
28
+ rows.each do |row|
29
+ routes << CfScript::RouteInfo.new(
30
+ row[:space].value,
31
+ row[:host].value,
32
+ row[:domain].value,
33
+ row[:apps].to_a
34
+ )
35
+ end
36
+
37
+ routes
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ module CfScript::Command
2
+ class Routes::UnmapRouteCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:routes, :unmap_route)
5
+ end
6
+
7
+ def run(app_name, domain, host = nil, &block)
8
+ options = host ? { n: host } : {}
9
+
10
+ run_cf self, app_name, domain, options do |output|
11
+ return false unless can_run?(output,
12
+ check_status: false,
13
+ check_failed: false,
14
+ )
15
+
16
+ if output.good? and unmapped = output.ok?
17
+ block_given? ? yield(unmapped) : unmapped
18
+ else
19
+ if output.failed?
20
+ error output.last_line
21
+ else
22
+ error 'failed to unmap route'
23
+ end
24
+
25
+ return false
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ CfScript::Command.const_set(:Routes, Module.new)
2
+
3
+ require 'cf_script/command/cf/routes/routes'
4
+ require 'cf_script/command/cf/routes/check_route'
5
+ require 'cf_script/command/cf/routes/create_route'
6
+ require 'cf_script/command/cf/routes/delete_route'
7
+ require 'cf_script/command/cf/routes/map_route'
8
+ require 'cf_script/command/cf/routes/unmap_route'
9
+
10
+ module CfScript::Command::Routes
11
+ CfScript::Command.register RoutesCommand
12
+ CfScript::Command.register CheckRouteCommand
13
+ CfScript::Command.register CreateRouteCommand
14
+ CfScript::Command.register DeleteRouteCommand
15
+ CfScript::Command.register MapRouteCommand
16
+ CfScript::Command.register UnmapRouteCommand
17
+ end
@@ -0,0 +1,36 @@
1
+ module CfScript::Command
2
+ class Spaces::SpaceCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:spaces, :space)
5
+ end
6
+
7
+ def run(space_name, &block)
8
+ run_cf self, space_name do |output|
9
+ return unless can_run?(output)
10
+
11
+ if space = build_space(space_name, output)
12
+ block_given? ? yield(space) : space
13
+ else
14
+ error 'object is nil'
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def build_space(space_name, output)
22
+ attrs = output.attributes
23
+
24
+ unless attrs.empty?
25
+ space = CfScript::Space.new(
26
+ space_name,
27
+ attrs[:org].value,
28
+ attrs[:apps].to_a,
29
+ attrs[:domains].to_a,
30
+ attrs[:services].to_a,
31
+ attrs[:security_groups].to_a
32
+ )
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,23 @@
1
+ module CfScript::Command
2
+ class Spaces::SpacesCommand < CfScript::Command::Base
3
+ SPACES_TABLE = ['name']
4
+
5
+ def initialize
6
+ super(:spaces, :spaces)
7
+ end
8
+
9
+ def run(*args, &block)
10
+ run_cf self, *args do |output|
11
+ return unless can_run?(output)
12
+
13
+ if rows = output.table(SPACES_TABLE)
14
+ spaces = rows.map { |row| row[:name].value }
15
+
16
+ block_given? ? yield(spaces) : spaces
17
+ else
18
+ error "Spaces table was not found"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,9 @@
1
+ CfScript::Command.const_set(:Spaces, Module.new)
2
+
3
+ require 'cf_script/command/cf/spaces/spaces'
4
+ require 'cf_script/command/cf/spaces/space'
5
+
6
+ module CfScript::Command::Spaces
7
+ CfScript::Command.register SpacesCommand
8
+ CfScript::Command.register SpaceCommand
9
+ end
@@ -0,0 +1,5 @@
1
+ class CfScript::Command::Error < StandardError
2
+ def initialize(command_line, output)
3
+ super("running '#{command}' [pid: #{output.status.pid}, exit: #{output.status.exitstatus}]")
4
+ end
5
+ end
@@ -0,0 +1,105 @@
1
+ require 'shellwords'
2
+
3
+ class CfScript::Command::Line
4
+ attr_reader :env
5
+ attr_reader :bin
6
+ attr_reader :type
7
+ attr_reader :name
8
+ attr_reader :args
9
+ attr_reader :options
10
+ attr_reader :flags
11
+
12
+ def initialize(env, bin, type, name, args = [])
13
+ @env = env
14
+ @bin = bin
15
+
16
+ @type = type
17
+ @name = name
18
+ @options = args.last.is_a?(Hash) ? args.pop : {}
19
+ @flags = options.delete(:flags) || []
20
+ @args = args
21
+ end
22
+
23
+ def line
24
+ @line ||= format
25
+ end
26
+ alias :to_s :line
27
+
28
+ def hide_sensitive
29
+ case name
30
+ when :auth
31
+ hide_parts(-1, 'PASSWORD HIDDEN')
32
+ when :login
33
+ hide_option('-p', 'PASSWORD HIDDEN')
34
+ else
35
+ line
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def format
42
+ list = [bin]
43
+
44
+ list << format_name
45
+ list << format_args
46
+ list << format_options
47
+ list << format_flags
48
+
49
+ list.compact.reject(&:empty?).join(' ').gsub(/\s+/, ' ')
50
+ end
51
+
52
+ def format_name
53
+ name.to_s.gsub(/_/, '-')
54
+ end
55
+
56
+ def format_args
57
+ list = []
58
+
59
+ args.compact.each do |arg|
60
+ list << arg.to_s.shellescape
61
+ end
62
+
63
+ list.join(' ')
64
+ end
65
+
66
+ def format_options
67
+ list = []
68
+
69
+ options.each do |name, value|
70
+ list << "-#{name} #{value.to_s.shellescape}" unless value.nil?
71
+ end
72
+
73
+ list.join(' ')
74
+ end
75
+
76
+ def format_flags
77
+ list = []
78
+
79
+ flags.each do |name|
80
+ flag = name.to_s.gsub('_', '-')
81
+
82
+ list << unless flag =~ /\A(--|-[[:alnum:]])/
83
+ if flag.length == 1
84
+ "-#{flag}"
85
+ else
86
+ "--#{flag}"
87
+ end
88
+ else
89
+ flag
90
+ end
91
+ end
92
+
93
+ list.join(' ')
94
+ end
95
+
96
+ def hide_parts(parts, with = 'HIDDEN')
97
+ bits = line.split(/\s+(?=(?:[^"']|"[^"]*"|'[^']*')*$)/)
98
+ bits[parts] = "[#{with}]"
99
+ bits.join(' ')
100
+ end
101
+
102
+ def hide_option(option, with = 'HIDDEN')
103
+ line.gsub(/#{option}\s+(['"][^'"]+['"]|[\S]+)/, "#{option} [#{with}]")
104
+ end
105
+ end
@@ -0,0 +1,50 @@
1
+ module CfScript::Command
2
+ class Registry
3
+ attr_reader :commands
4
+
5
+ DuplicateCommand = Class.new(StandardError)
6
+ UnimplementedCommand = Class.new(StandardError)
7
+
8
+ def initialize
9
+ @commands = {}
10
+ end
11
+
12
+ def catalog
13
+ catalog = Hash.new{ |types, type| types[type] = [] }
14
+
15
+ commands.each do |name, command|
16
+ catalog[command.type] << name
17
+ end
18
+
19
+ catalog
20
+ end
21
+
22
+ def add!(command_class)
23
+ instance = command_class.instance
24
+
25
+ if known?(instance.name)
26
+ raise DuplicateCommand.new(
27
+ "The '#{instance.name}' command is already registered"
28
+ )
29
+ end
30
+
31
+ commands[instance.name] = instance
32
+ end
33
+
34
+ def known?(command_name)
35
+ commands.key?(command_name)
36
+ end
37
+
38
+ def check!(command_name)
39
+ unless known?(command_name)
40
+ raise UnimplementedCommand.new(
41
+ "The #{command_name} command is not implemented, yet"
42
+ )
43
+ end
44
+ end
45
+
46
+ def [](command_name)
47
+ commands[command_name]
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,97 @@
1
+ module CfScript::Command
2
+ module Runner
3
+ module_function
4
+
5
+ def run_cf(command, *args, &block)
6
+ run_command(command, *args, &block)
7
+ rescue CfScript::Command::Error => exception
8
+ error command.name, exception.message
9
+ end
10
+
11
+ def cf_id
12
+ "#{cf_version} (path: #{cf_path})"
13
+ end
14
+
15
+ def cf_version
16
+ `#{cf_bin} --version`.chomp.split(' ')[1..-1].join(': ')
17
+ end
18
+
19
+ def cf_bin
20
+ @cf_bin ||= cf_in_path? && !cf_in_env? ? 'cf' : cf_path
21
+ end
22
+
23
+ def which_cf
24
+ `which cf`.chomp
25
+ end
26
+
27
+ def cf_in_path?
28
+ !which_cf.empty?
29
+ end
30
+
31
+ def cf_in_env?
32
+ !ENV['CF_BINARY'].nil?
33
+ end
34
+
35
+ def cf_path
36
+ unless @cf_path
37
+ @cf_path = ENV['CF_BINARY'] || which_cf
38
+
39
+ if @cf_path.nil? || @cf_path.empty?
40
+ puts "Could not find the cf binary".colorize(:red)
41
+ puts
42
+ puts "If it isn't installed you need to install it first. You can find"
43
+ puts "the latest release at:"
44
+ puts
45
+ puts " https://github.com/cloudfoundry/cli/releases".colorize(:cyan)
46
+ puts
47
+ puts "If cf is already installed, make sure its location is included "
48
+ puts "in $PATH."
49
+
50
+ raise "Could not find the cf binary"
51
+ end
52
+ end
53
+
54
+ @cf_path
55
+ end
56
+
57
+ def cf_env
58
+ # When things go wrong dump the output of cf to the terminal, and the
59
+ # colors make it easier to read.
60
+ ENV['CF_COLOR'] ||= CfScript.config.runtime.color ? 'true' : 'false'
61
+ ENV['CF_TRACE'] ||= ENV['DEBUG'] ? 'true' : 'false'
62
+ ENV
63
+ end
64
+
65
+ private
66
+
67
+ def cf_executor
68
+ CfScript.config.runtime.executor
69
+ end
70
+
71
+ def run_command(command, *args, &block)
72
+ command_line = command.line(cf_env, cf_bin, args)
73
+
74
+ trace(:running, command_line.hide_sensitive, true)
75
+
76
+ stdout, stderr, status = cf_executor.execute(cf_env, command_line)
77
+
78
+ output = CfScript::Output.new(command_line, stdout, stderr, status)
79
+
80
+ unless output.good?
81
+ run_error!(command_line, output)
82
+ end
83
+
84
+ block_given? ? yield(output) : output
85
+ end
86
+
87
+ def run_error!(command_line, output)
88
+ unless CfScript.stdout.is_a?(StringIO)
89
+ CfScript.stdout.puts output.stdout unless output.stdout.empty?
90
+ end
91
+
92
+ unless CfScript.stderr.is_a?(StringIO)
93
+ CfScript.stderr.puts output.stderr unless output.stderr.empty?
94
+ end
95
+ end
96
+ end
97
+ end