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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 760492ef86eb59b8e5fdcb8275227fc8e8e884e4
4
+ data.tar.gz: 3d347022f374182c81c52cfaa69b24986a620406
5
+ SHA512:
6
+ metadata.gz: 1d430f863f77eebe4188a6891fb2e609abdc0e2e1a168920e23ac0f6ee50feee970ef3c9d16b7052011597977131a0c8c671b01df7e5d9a5f94036bbca9ae01a
7
+ data.tar.gz: 42a1de9c7fbd5d2cdee6affafa8c53862112057aaad50d0daaba59df433e7298b393ae0fd06b87e5c0f6ec301e417d96d8c02d6b5b5f4805f7cd797cd0f72bb1
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ *.DS_Store
2
+ *.swp
3
+
4
+ .env
5
+ .ruby-version
6
+ .editorconfig
7
+
8
+ Gemfile.lock
9
+
10
+ pkg/*
11
+ coverage/
12
+ .bundle/*
13
+ vendor/
14
+
15
+ sandbox/
data/CHANGELOG.md ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'pry'
7
+ gem 'rake'
8
+ gem 'minitest'
9
+ gem 'minitest-focus'
10
+ end
11
+
12
+ gem 'simplecov', require: false, group: :test
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2010, 2015-2016, Ammar Ali
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ # CfScript
2
+
3
+ CfScript is a DSL for scripting and automating the Cloud Foundry CLI.
4
+
5
+ ```ruby
6
+ require 'cf_script'
7
+
8
+ cf space: 'staging' do
9
+ workers = apps ending_with: 'worker'
10
+
11
+ workers.each do |worker|
12
+ app worker do
13
+ set_env 'SOME_NAME', 'SOME_VALUE'
14
+
15
+ restart if started?
16
+ end
17
+ end
18
+ end
19
+ ```
20
+
21
+ ## Requirements
22
+
23
+ * `ruby` >= 2.0
24
+ * `cf` >= 16.0
25
+
26
+ ---
27
+ ## Install
28
+
29
+ Install the gem with:
30
+
31
+ `gem install cf_script --pre`
32
+
33
+ Or, add it to your project's `Gemfile`:
34
+
35
+ ```gem 'cf_script', '0.1.0.pre'```
36
+
37
+ ---
38
+
39
+ ## Supported Commands
40
+
41
+ | Commands | CLI command | ⋯ |
42
+ | ------------------------------------- | ------------------------------------------------------- |:--------:|
43
+ | **General** | | ⋱ |
44
+ |   _**api**_ | `cf api URL` | ✓ |
45
+ |   _**auth**_ | `cf auth USER PASSWORD` | ✓ |
46
+ |   _**login**_ | `cf login -u USER -p PASSWORD [OPTIONS]` | ✓ |
47
+ |   _**logout**_ | `cf logout` | ✓ |
48
+ |   _**target**_ | `cf target [-s SPACE -o ORG]` | ✓ |
49
+ | **Applications** | | ⋱ |
50
+ |   _**app**_ | `cf app APP_NAME` | ✓ |
51
+ |   _**apps**_ | `cf apps` | ✓ |
52
+ |   _**start**_ | `cf start APP_NAME` | ✓ |
53
+ |   _**stop**_ | `cf stop APP_NAME` | ✓ |
54
+ |   _**restart**_ | `cf restart APP_NAME` | ✓ |
55
+ |   _**push**_ | `cf push [OPTIONS]` | ✓ |
56
+ |   _**restage**_ | `cf restage APP_NAME` | ✓ |
57
+ |   _**env**_ | `cf env APP_NAME` | ✓ |
58
+ |   _**set-env**_ | `cf set-env APP_NAME VAR_NAME VAR_VALUE` | ✓ |
59
+ |   _**unset-env**_ | `cf unset-env APP_NAME VAR_NAME` | ✓ |
60
+ | **Routes** | | ⋱ |
61
+ |   _**routes**_ | `cf routes` | ✓ |
62
+ |   _**check-route**_ | `cf check-route HOST DOMAIN` | ✓ |
63
+ |   _**create-route**_ | `cf create-route SPACE DOMAIN [-n HOSTNAME]` | ✓ |
64
+ |   _**map-route**_ | `cf map-route APP_NAME DOMAIN [-n HOSTNAME]` | ✓ |
65
+ |   _**unmap-route**_ | `cf unmap-route APP_NAME DOMAIN [-n HOSTNAME]` | ✓ |
66
+ |   _**delete-route**_ | `cf delete-route DOMAIN [-n HOSTNAME] [-f]` | ✓ |
67
+ | **Spaces** | | ⋱ |
68
+ |   _**space**_ | `cf space SPACE` | ✓ |
69
+ |   _**spaces**_ | `cf spaces` | ✓ |
70
+
71
+ ---
72
+
73
+ ## Building
74
+ The project uses the standard rubygems package tasks, so:
75
+
76
+ To build your cloned copy of the gem, run:
77
+ ```
78
+ rake build
79
+ ```
80
+
81
+ To install the gem from the cloned project, run:
82
+ ```
83
+ rake install
84
+ ```
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+
6
+ require 'bundler'
7
+ require 'rubygems/package_task'
8
+
9
+ Bundler::GemHelper.install_tasks
10
+
11
+ task :default => [:test]
12
+
13
+ Rake::TestTask.new('test') do |t|
14
+ if t.respond_to?(:description)
15
+ t.description = "Run all unit tests under the test directory"
16
+ end
17
+
18
+ t.libs << "test"
19
+ t.test_files = FileList['test/**/*.rb']
20
+ end
21
+
22
+ namespace :test do
23
+ %w{commands runner parser}.each do |component|
24
+ Rake::TestTask.new(component) do |t|
25
+ if t.respond_to?(:description)
26
+ t.description = "Run all #{component} unit tests under the test/#{component} directory"
27
+ end
28
+
29
+ t.libs << "test"
30
+ t.test_files = ["test/#{component}/**/*.rb"]
31
+ end
32
+ end
33
+ end
data/cf_script.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), 'lib')
2
+
3
+ require 'cf_script/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'cf_script'
7
+ gem.version = ::CfScript::VERSION
8
+
9
+ gem.summary = 'A DSL for scripting the Cloud Foundry CLI'
10
+ gem.description = ''
11
+ gem.homepage = 'http://github.com/ammar/cf_script'
12
+
13
+ if gem.respond_to?(:metadata)
14
+ gem.metadata = { 'issue_tracker' => 'https://github.com/ammar/cf_script/issues' }
15
+ end
16
+
17
+ gem.authors = ['Ammar Ali']
18
+ gem.email = ['ammar@syntax.ps']
19
+
20
+ gem.license = 'MIT'
21
+
22
+ gem.require_paths = ['lib']
23
+
24
+ gem.files = `git ls-files`.split("\n")
25
+ gem.test_files = Dir.glob('test/**/*.rb')
26
+
27
+ gem.rdoc_options = ["--inline-source", "--charset=UTF-8"]
28
+
29
+ gem.platform = Gem::Platform::RUBY
30
+
31
+ gem.required_ruby_version = '>= 2.0.0'
32
+
33
+ gem.add_dependency('colorize', '>= 0.7.7')
34
+ end
@@ -0,0 +1,43 @@
1
+ module CfScript::Callbacks
2
+ def callbacks
3
+ @callbacks ||= { before: {}, after: {} }
4
+ end
5
+
6
+ def before(step, &block)
7
+ callbacks[:before][step] ||= []
8
+ callbacks[:before][step] << block
9
+ end
10
+
11
+ def after(step, &block)
12
+ callbacks[:after][step] ||= []
13
+ callbacks[:after][step] << block
14
+ end
15
+
16
+ def callbacks_for(type, step)
17
+ return [] unless callbacks.key?(type) && callbacks[type].key?(step)
18
+
19
+ callbacks[type][step]
20
+ end
21
+
22
+ def run_callbacks(type, step)
23
+ return unless callbacks[type].key?(step)
24
+
25
+ callbacks[type][step].each do |callback|
26
+ instance_eval(&callback)
27
+ end
28
+ end
29
+
30
+ def run_before(step)
31
+ run_callbacks(:before, step)
32
+ end
33
+
34
+ def run_after(step)
35
+ run_callbacks(:after, step)
36
+ end
37
+
38
+ def run_around(step, &block)
39
+ run_before(step)
40
+ yield
41
+ run_after(step)
42
+ end
43
+ end
@@ -0,0 +1,57 @@
1
+ require 'singleton'
2
+
3
+ module CfScript::Command
4
+ class Base
5
+ include ::Singleton
6
+
7
+ attr_reader :type
8
+ attr_reader :name
9
+
10
+ include CfScript::Command::Runner
11
+
12
+ include CfScript::UI
13
+ include CfScript::UI::NameTag
14
+
15
+ def initialize(type, name)
16
+ @type = type
17
+ @name = name
18
+ end
19
+
20
+ def line(env, bin, args)
21
+ Line.new(env, bin, type, name, args)
22
+ end
23
+
24
+ def run(*args, &block)
25
+ raise "run called in base command class"
26
+ end
27
+
28
+ def can_run?(output, options = {})
29
+ options[:check_status] = options.key?(:check_status) ? options[:check_status] : true
30
+ options[:check_failed] = options.key?(:check_failed) ? options[:check_failed] : true
31
+
32
+ if options[:check_status] == true && !output.good?
33
+ error 'cf exited with error'
34
+ output.dump
35
+ return false
36
+ end
37
+
38
+ if output.no_api_endpoint?
39
+ error "No API endpoint set"
40
+ return false
41
+ end
42
+
43
+ if output.not_logged_in?
44
+ error 'Not logged in'
45
+ return false
46
+ end
47
+
48
+ if options[:check_failed] == true && output.failed?
49
+ error 'FAILED'
50
+ output.dump
51
+ return false
52
+ end
53
+
54
+ true
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,19 @@
1
+ module CfScript::Command
2
+ class Apps::AppCommand < CfScript::Command::Apps::Base
3
+ def initialize
4
+ super(:apps, :app)
5
+ end
6
+
7
+ def run(app_name, options = {}, &block)
8
+ run_cf self, app_name do |output|
9
+ return nil unless can_run?(output)
10
+
11
+ if app_info = build_app_info(app_name, output)
12
+ block_given? ? yield(app_info) : app_info
13
+ else
14
+ error 'attributes is nil'
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,44 @@
1
+ module CfScript::Command
2
+ class Apps::AppsCommand < CfScript::Command::Base
3
+ NO_APPS_FOUND = 'No apps found'
4
+
5
+ APPS_TABLE = [
6
+ 'name',
7
+ 'requested state',
8
+ 'instances',
9
+ 'memory',
10
+ 'disk',
11
+ 'urls'
12
+ ]
13
+
14
+ def initialize
15
+ super(:apps, :apps)
16
+ end
17
+
18
+ def run(options = {}, &block)
19
+ run_cf self do |output|
20
+ return [] unless can_run?(output)
21
+ return [] if output.contains? NO_APPS_FOUND
22
+
23
+ if rows = output.table(APPS_TABLE)
24
+ list = build_app_list(rows)
25
+
26
+ block_given? ? yield(list) : list
27
+ else
28
+ error 'rows is nil'
29
+ return []
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def build_app_list(rows)
37
+ apps = rows.map do |row|
38
+ CfScript::AppInfo.new(row[:name].value, row)
39
+ end
40
+
41
+ CfScript::AppList.new(apps)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,20 @@
1
+ module CfScript::Command
2
+ class Apps::EnvCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:apps, :env)
5
+ end
6
+
7
+ def run(app_name, &block)
8
+ run_cf self, app_name do |output|
9
+ return {} unless can_run?(output)
10
+
11
+ if vars = output.section_attributes('User-Provided')
12
+ block_given? ? yield(vars.to_h) : vars.to_h
13
+ else
14
+ error 'vars is nil'
15
+ return {}
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,47 @@
1
+ module CfScript::Command
2
+ class Apps::PushCommand < CfScript::Command::Apps::Base
3
+ LONG_OPTIONS_MAP = {
4
+ manifest: :f,
5
+ instances: :i,
6
+ memory: :m,
7
+ command: :c,
8
+ buildpack: :b,
9
+ hostname: :n,
10
+ domain: :d,
11
+ disk: :k,
12
+ path: :p,
13
+ stack: :s,
14
+ timeout: :t,
15
+ docker_image: :o,
16
+ health_check_type: :u,
17
+ }
18
+
19
+ def initialize
20
+ super(:apps, :push)
21
+ end
22
+
23
+ def run(app_name, options = {}, &block)
24
+ run_cf self, app_name, translate_options(options.dup) do |output|
25
+ return unless can_run?(output, check_failed: false)
26
+
27
+ if app = build_app_info(app_name, output)
28
+ block_given? ? yield(app) : app
29
+ else
30
+ error "app is nil"
31
+ end
32
+ end
33
+ end
34
+
35
+ def translate_options(options)
36
+ opts = options.dup
37
+
38
+ LONG_OPTIONS_MAP.each do |long, short|
39
+ if opts.key?(long)
40
+ opts[short] = opts.delete(long)
41
+ end
42
+ end
43
+
44
+ opts
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,19 @@
1
+ module CfScript::Command
2
+ class Apps::RestageCommand < CfScript::Command::Apps::Base
3
+ def initialize
4
+ super(:apps, :restage)
5
+ end
6
+
7
+ def run(app_name, &block)
8
+ run_cf self, app_name do |output|
9
+ return unless can_run?(output)
10
+
11
+ if app = build_app_info(app_name, output)
12
+ block_given? ? yield(app) : app
13
+ else
14
+ error "app is nil"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module CfScript::Command
2
+ class Apps::RestartCommand < CfScript::Command::Apps::Base
3
+ def initialize
4
+ super(:apps, :restart)
5
+ end
6
+
7
+ def run(app_name, &block)
8
+ run_cf self, app_name do |output|
9
+ return unless can_run?(output)
10
+
11
+ if app = build_app_info(app_name, output)
12
+ block_given? ? yield(app) : app
13
+ else
14
+ error "app is nil"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module CfScript::Command
2
+ class Apps::SetEnvCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:apps, :set_env)
5
+ end
6
+
7
+ def run(app_name, var_name, var_value, &block)
8
+ run_cf self, app_name, var_name, var_value do |output|
9
+ return false unless can_run?(output)
10
+
11
+ return true if output.contains?("\nOK\n")
12
+
13
+ error 'Could not set env variable'
14
+ return false
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ module CfScript::Command
2
+ class Apps::StartCommand < CfScript::Command::Apps::Base
3
+ def initialize
4
+ super(:apps, :start)
5
+ end
6
+
7
+ def run(app_name, &block)
8
+ run_cf self, app_name do |output|
9
+ return unless can_run?(output)
10
+
11
+ if app = build_app_info(app_name, output)
12
+ block_given? ? yield(app) : app
13
+ else
14
+ error "app is nil"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ module CfScript::Command
2
+ class Apps::StopCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:apps, :stop)
5
+ end
6
+
7
+ def run(app_name, &block)
8
+ run_cf self, app_name do |output|
9
+ return false unless can_run?(output, check_failed: false)
10
+
11
+ if output.ok?
12
+ block_given? ? yield(true) : true
13
+ else
14
+ error "failed to stop"
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ module CfScript::Command
2
+ class Apps::UnsetEnvCommand < CfScript::Command::Base
3
+ def initialize
4
+ super(:apps, :unset_env)
5
+ end
6
+
7
+ def run(app_name, var_name, &block)
8
+ run_cf self, app_name, var_name do |output|
9
+ return false unless can_run?(output)
10
+
11
+ return true if output.contains?("\nOK\n")
12
+
13
+ error 'Could not unset env variable'
14
+ return false
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,55 @@
1
+ CfScript::Command.const_set(:Apps, Module.new)
2
+
3
+ module CfScript::Command
4
+ class Apps::Base < CfScript::Command::Base
5
+ INSTANCE_STATUS_TABLE = [
6
+ '',
7
+ 'state',
8
+ 'since',
9
+ 'cpu',
10
+ 'memory',
11
+ 'disk',
12
+ 'details'
13
+ ]
14
+
15
+ def build_app_info(app_name, output)
16
+ attributes = output.attributes_from(
17
+ 'Showing health and status for app'
18
+ )
19
+
20
+ if attributes
21
+ app = CfScript::AppInfo.new(app_name, attributes)
22
+
23
+ if rows = output.table(INSTANCE_STATUS_TABLE)
24
+ app.set_instance_status rows
25
+ end
26
+
27
+ app
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ require 'cf_script/command/cf/apps/apps'
34
+ require 'cf_script/command/cf/apps/app'
35
+ require 'cf_script/command/cf/apps/start'
36
+ require 'cf_script/command/cf/apps/stop'
37
+ require 'cf_script/command/cf/apps/restart'
38
+ require 'cf_script/command/cf/apps/push'
39
+ require 'cf_script/command/cf/apps/restage'
40
+ require 'cf_script/command/cf/apps/env'
41
+ require 'cf_script/command/cf/apps/set_env'
42
+ require 'cf_script/command/cf/apps/unset_env'
43
+
44
+ module CfScript::Command::Apps
45
+ CfScript::Command.register AppsCommand
46
+ CfScript::Command.register AppCommand
47
+ CfScript::Command.register StartCommand
48
+ CfScript::Command.register StopCommand
49
+ CfScript::Command.register RestartCommand
50
+ CfScript::Command.register PushCommand
51
+ CfScript::Command.register RestageCommand
52
+ CfScript::Command.register EnvCommand
53
+ CfScript::Command.register SetEnvCommand
54
+ CfScript::Command.register UnsetEnvCommand
55
+ end
@@ -0,0 +1,36 @@
1
+ module CfScript::Command::General
2
+ class ApiCommand < CfScript::Command::Base
3
+ API_NOT_SET = 'No api endpoint set'
4
+
5
+ API_ENDPOINT_REGEX = %r|API endpoint:\s+(?<url>https?://[\S]+)\s+\(API version:\s+(?<version>[^)]+)\)|
6
+
7
+ def initialize
8
+ super(:general, :api)
9
+ end
10
+
11
+ def run(url = nil, &block)
12
+ run_cf self, url do |output|
13
+ return unless can_run?(output)
14
+
15
+ if output.contains? API_NOT_SET
16
+ error API_NOT_SET
17
+ else
18
+ build_endpoint(output)
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def build_endpoint(output)
26
+ attrs = output.line_attributes API_ENDPOINT_REGEX
27
+
28
+ unless attrs.nil? or attrs.empty?
29
+ CfScript::ApiEndpoint.new(
30
+ attrs[:url].value,
31
+ attrs[:version].value
32
+ )
33
+ end
34
+ end
35
+ end
36
+ end