hammer_cli 0.4.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (213) hide show
  1. checksums.yaml +4 -4
  2. data/bin/hammer +1 -1
  3. data/doc/release_notes.md +9 -0
  4. data/lib/hammer_cli/abstract.rb +14 -4
  5. data/lib/hammer_cli/context.rb +13 -0
  6. data/lib/hammer_cli/defaults.rb +83 -0
  7. data/lib/hammer_cli/defaults_commands.rb +160 -0
  8. data/lib/hammer_cli/options/option_definition.rb +3 -3
  9. data/lib/hammer_cli/output/adapter/json.rb +13 -0
  10. data/lib/hammer_cli/output/adapter/yaml.rb +13 -0
  11. data/lib/hammer_cli/settings.rb +1 -0
  12. data/lib/hammer_cli/version.rb +1 -1
  13. data/lib/hammer_cli.rb +2 -0
  14. data/locale/de/hammer-cli.edit.po +251 -139
  15. data/locale/de/hammer-cli.po +81 -7
  16. data/locale/en/hammer-cli.edit.po +95 -7
  17. data/locale/en/hammer-cli.po +79 -5
  18. data/locale/en_GB/hammer-cli.edit.po +248 -136
  19. data/locale/en_GB/hammer-cli.po +81 -7
  20. data/locale/es/hammer-cli.edit.po +250 -138
  21. data/locale/es/hammer-cli.po +81 -7
  22. data/locale/fr/hammer-cli.edit.po +250 -138
  23. data/locale/fr/hammer-cli.po +81 -7
  24. data/locale/hammer-cli.pot +104 -8
  25. data/locale/it/hammer-cli.edit.po +249 -137
  26. data/locale/it/hammer-cli.po +81 -7
  27. data/locale/ja/hammer-cli.edit.po +248 -136
  28. data/locale/ja/hammer-cli.po +81 -7
  29. data/locale/ko/hammer-cli.edit.po +248 -136
  30. data/locale/ko/hammer-cli.po +81 -7
  31. data/locale/pt_BR/hammer-cli.edit.po +250 -137
  32. data/locale/pt_BR/hammer-cli.po +81 -7
  33. data/locale/ru/hammer-cli.edit.po +249 -137
  34. data/locale/ru/hammer-cli.po +81 -7
  35. data/locale/zh_CN/hammer-cli.edit.po +248 -136
  36. data/locale/zh_CN/hammer-cli.po +82 -8
  37. data/locale/zh_TW/hammer-cli.edit.po +248 -136
  38. data/locale/zh_TW/hammer-cli.po +81 -7
  39. data/test/functional/defaults_test.rb +177 -0
  40. data/test/reports/TEST-Fields-ContainerField-display-.xml +1 -1
  41. data/test/reports/TEST-Fields-ContainerField-display-blank-is-allowed.xml +5 -5
  42. data/test/reports/TEST-Fields-ContainerField-display-blank-is-not-allowed.xml +5 -5
  43. data/test/reports/TEST-Fields-ContainerField.xml +1 -1
  44. data/test/reports/TEST-Fields-Field-display-.xml +1 -1
  45. data/test/reports/TEST-Fields-Field-display-blank-is-allowed.xml +3 -3
  46. data/test/reports/TEST-Fields-Field-display-blank-is-not-allowed.xml +3 -3
  47. data/test/reports/TEST-Fields-Field-hide-blank-.xml +3 -3
  48. data/test/reports/TEST-Fields-Field-parameters.xml +2 -2
  49. data/test/reports/TEST-Fields-Field.xml +4 -4
  50. data/test/reports/TEST-Fields-Label-display-.xml +7 -0
  51. data/test/reports/TEST-Fields-Label-display-blank-is-allowed.xml +15 -0
  52. data/test/reports/TEST-Fields-Label-display-blank-is-not-allowed.xml +15 -0
  53. data/test/reports/TEST-Fields-Label.xml +7 -0
  54. data/test/reports/TEST-HammerCLI-AbstractCommand-build-options.xml +5 -5
  55. data/test/reports/TEST-HammerCLI-AbstractCommand-exception-handler.xml +4 -4
  56. data/test/reports/TEST-HammerCLI-AbstractCommand-logging.xml +10 -8
  57. data/test/reports/TEST-HammerCLI-AbstractCommand-option-builder.xml +3 -3
  58. data/test/reports/TEST-HammerCLI-AbstractCommand-options.xml +3 -3
  59. data/test/reports/TEST-HammerCLI-AbstractCommand-output.xml +7 -7
  60. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-remove-subcommand.xml +3 -3
  61. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-subcommand-.xml +4 -4
  62. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior-subcommand.xml +3 -3
  63. data/test/reports/TEST-HammerCLI-AbstractCommand-subcommand-behavior.xml +1 -1
  64. data/test/reports/TEST-HammerCLI-AbstractCommand.xml +3 -3
  65. data/test/reports/TEST-HammerCLI-Apipie-Command-options.xml +3 -3
  66. data/test/reports/TEST-HammerCLI-Apipie-Command-reload-apipie-cache.xml +11 -0
  67. data/test/reports/TEST-HammerCLI-Apipie-Command-resource-defined.xml +2 -2
  68. data/test/reports/TEST-HammerCLI-Apipie-Command-setting-resources.xml +7 -7
  69. data/test/reports/TEST-HammerCLI-Apipie-Command.xml +2 -2
  70. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-aliasing-resources.xml +4 -4
  71. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-filtering-options.xml +5 -5
  72. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-required-options.xml +3 -3
  73. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-setting-correct-normalizers.xml +8 -2
  74. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-setting-referenced-resources.xml +9 -0
  75. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-with-hash-params.xml +3 -3
  76. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder-with-one-simple-param.xml +5 -5
  77. data/test/reports/TEST-HammerCLI-Apipie-OptionBuilder.xml +1 -1
  78. data/test/reports/TEST-HammerCLI-Apipie-OptionDefinition-referenced-resource.xml +13 -0
  79. data/test/reports/TEST-HammerCLI-Apipie-OptionDefinition.xml +7 -0
  80. data/test/reports/TEST-HammerCLI-Completer-command-completion.xml +12 -12
  81. data/test/reports/TEST-HammerCLI-Completer-option-value-completion.xml +6 -6
  82. data/test/reports/TEST-HammerCLI-Completer-subcommand-completion.xml +7 -7
  83. data/test/reports/TEST-HammerCLI-Completer.xml +1 -1
  84. data/test/reports/TEST-HammerCLI-CompleterLine-line-complete.xml +10 -10
  85. data/test/reports/TEST-HammerCLI-CompleterLine-splitting-words.xml +12 -12
  86. data/test/reports/TEST-HammerCLI-CompleterLine.xml +1 -1
  87. data/test/reports/TEST-HammerCLI-CompleterWord-complete-.xml +9 -9
  88. data/test/reports/TEST-HammerCLI-CompleterWord-quote.xml +5 -5
  89. data/test/reports/TEST-HammerCLI-CompleterWord-quoted-.xml +4 -4
  90. data/test/reports/TEST-HammerCLI-CompleterWord.xml +1 -1
  91. data/test/reports/TEST-HammerCLI-Connection.xml +8 -8
  92. data/test/reports/TEST-HammerCLI-Defaults.xml +19 -0
  93. data/test/reports/TEST-HammerCLI-ExceptionHandler.xml +8 -8
  94. data/test/reports/TEST-HammerCLI-I18n.xml +3 -3
  95. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-password.xml +3 -3
  96. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-username.xml +3 -3
  97. data/test/reports/TEST-HammerCLI-MainCommand-loading-context-verbose.xml +2 -2
  98. data/test/reports/TEST-HammerCLI-MainCommand-loading-context.xml +1 -1
  99. data/test/reports/TEST-HammerCLI-MainCommand.xml +1 -1
  100. data/test/reports/TEST-HammerCLI-Modules-find-by-name.xml +4 -4
  101. data/test/reports/TEST-HammerCLI-Modules-load-a-module-module-not-found.xml +4 -4
  102. data/test/reports/TEST-HammerCLI-Modules-load-a-module-module-runtime-exception.xml +4 -4
  103. data/test/reports/TEST-HammerCLI-Modules-load-a-module-success.xml +5 -5
  104. data/test/reports/TEST-HammerCLI-Modules-load-a-module.xml +1 -1
  105. data/test/reports/TEST-HammerCLI-Modules-load-all-modules.xml +4 -2
  106. data/test/reports/TEST-HammerCLI-Modules-names.xml +4 -4
  107. data/test/reports/TEST-HammerCLI-Modules.xml +1 -1
  108. data/test/reports/TEST-HammerCLI-OptionBuilderContainer.0.xml +5 -1
  109. data/test/reports/TEST-HammerCLI-OptionBuilderContainer.xml +1 -5
  110. data/test/reports/TEST-HammerCLI-Options-Matcher.xml +17 -0
  111. data/test/reports/TEST-HammerCLI-Options-Normalizers-abstract.xml +2 -2
  112. data/test/reports/TEST-HammerCLI-Options-Normalizers-bool.xml +13 -13
  113. data/test/reports/TEST-HammerCLI-Options-Normalizers-datetime.xml +6 -6
  114. data/test/reports/TEST-HammerCLI-Options-Normalizers-enum.xml +5 -5
  115. data/test/reports/TEST-HammerCLI-Options-Normalizers-enumlist.xml +8 -8
  116. data/test/reports/TEST-HammerCLI-Options-Normalizers-json-input.xml +5 -5
  117. data/test/reports/TEST-HammerCLI-Options-Normalizers-key-value-list.xml +26 -6
  118. data/test/reports/TEST-HammerCLI-Options-Normalizers-list.xml +5 -5
  119. data/test/reports/TEST-HammerCLI-Options-Normalizers-number.xml +11 -0
  120. data/test/reports/TEST-HammerCLI-Options-Normalizers.xml +1 -1
  121. data/test/reports/TEST-HammerCLI-Options-OptionDefinition-context.xml +2 -2
  122. data/test/reports/TEST-HammerCLI-Options-OptionDefinition-deprecated-options.xml +11 -0
  123. data/test/reports/TEST-HammerCLI-Options-OptionDefinition-formatters.xml +3 -3
  124. data/test/reports/TEST-HammerCLI-Options-OptionDefinition.xml +1 -1
  125. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-error-messages.xml +5 -5
  126. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-messages.xml +3 -3
  127. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract-test-data-for-field.xml +5 -5
  128. data/test/reports/TEST-HammerCLI-Output-Adapter-Abstract.xml +6 -6
  129. data/test/reports/TEST-HammerCLI-Output-Adapter-Base-print-collection-show-ids.xml +2 -2
  130. data/test/reports/TEST-HammerCLI-Output-Adapter-Base-print-collection.xml +11 -11
  131. data/test/reports/TEST-HammerCLI-Output-Adapter-Base.xml +1 -1
  132. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-formatters.xml +3 -3
  133. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-fields-with-collections.xml +4 -4
  134. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-fields-with-containers.xml +3 -3
  135. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection-handle-ids.xml +3 -3
  136. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-collection.xml +3 -3
  137. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues-print-message.xml +3 -3
  138. data/test/reports/TEST-HammerCLI-Output-Adapter-CSValues.xml +1 -1
  139. data/test/reports/TEST-HammerCLI-Output-Adapter-Json-print-collection-show-ids.xml +9 -0
  140. data/test/reports/TEST-HammerCLI-Output-Adapter-Json-print-collection.xml +27 -0
  141. data/test/reports/TEST-HammerCLI-Output-Adapter-Json-print-message.xml +11 -0
  142. data/test/reports/TEST-HammerCLI-Output-Adapter-Json.xml +7 -0
  143. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-column-width.xml +11 -5
  144. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-formatters.xml +3 -3
  145. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-handle-ids.xml +7 -3
  146. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection-sort-columns.xml +2 -2
  147. data/test/reports/TEST-HammerCLI-Output-Adapter-Table-print-collection.xml +3 -3
  148. data/test/reports/TEST-HammerCLI-Output-Adapter-Table.xml +1 -1
  149. data/test/reports/TEST-HammerCLI-Output-Adapter-Yaml-print-collection-show-ids.xml +9 -0
  150. data/test/reports/TEST-HammerCLI-Output-Adapter-Yaml-print-collection.xml +27 -0
  151. data/test/reports/TEST-HammerCLI-Output-Adapter-Yaml-print-message.xml +11 -0
  152. data/test/reports/TEST-HammerCLI-Output-Adapter-Yaml.xml +7 -0
  153. data/test/reports/TEST-HammerCLI-Output-Definition-empty-.xml +3 -3
  154. data/test/reports/TEST-HammerCLI-Output-Definition.xml +3 -3
  155. data/test/reports/TEST-HammerCLI-Output-Dsl-collection.xml +4 -4
  156. data/test/reports/TEST-HammerCLI-Output-Dsl-custom-fields.xml +3 -3
  157. data/test/reports/TEST-HammerCLI-Output-Dsl-fields.xml +5 -5
  158. data/test/reports/TEST-HammerCLI-Output-Dsl-label.xml +4 -4
  159. data/test/reports/TEST-HammerCLI-Output-Dsl-path-definition.xml +4 -4
  160. data/test/reports/TEST-HammerCLI-Output-Dsl.xml +2 -2
  161. data/test/reports/TEST-HammerCLI-Output-FieldFilter.xml +4 -4
  162. data/test/reports/TEST-HammerCLI-Output-Formatters-BooleanFormatter.xml +3 -3
  163. data/test/reports/TEST-HammerCLI-Output-Formatters-ColorFormatter.xml +2 -2
  164. data/test/reports/TEST-HammerCLI-Output-Formatters-DateFormatter.xml +3 -3
  165. data/test/reports/TEST-HammerCLI-Output-Formatters-FieldFormatter.xml +4 -4
  166. data/test/reports/TEST-HammerCLI-Output-Formatters-FormatterContainer.xml +4 -4
  167. data/test/reports/TEST-HammerCLI-Output-Formatters-FormatterLibrary.xml +3 -3
  168. data/test/reports/TEST-HammerCLI-Output-Formatters-KeyValueFormatter.xml +4 -4
  169. data/test/reports/TEST-HammerCLI-Output-Formatters-ListFormatter.xml +4 -4
  170. data/test/reports/TEST-HammerCLI-Output-Formatters-LongTextFormatter.xml +4 -4
  171. data/test/reports/TEST-HammerCLI-Output-Output-adapters.xml +6 -6
  172. data/test/reports/TEST-HammerCLI-Output-Output-data.xml +5 -5
  173. data/test/reports/TEST-HammerCLI-Output-Output-formatters.xml +2 -2
  174. data/test/reports/TEST-HammerCLI-Output-Output-messages.xml +7 -7
  175. data/test/reports/TEST-HammerCLI-Output-Output.xml +1 -1
  176. data/test/reports/TEST-HammerCLI-Output-RecordCollection.xml +4 -4
  177. data/test/reports/TEST-HammerCLI-Settings-load-from-paths.xml +5 -5
  178. data/test/reports/TEST-HammerCLI-Settings.xml +10 -10
  179. data/test/reports/TEST-HammerCLI-ShellHistory-loading-old-history.xml +3 -3
  180. data/test/reports/TEST-HammerCLI-ShellHistory-saving-history.xml +5 -5
  181. data/test/reports/TEST-HammerCLI-ShellHistory.xml +1 -1
  182. data/test/reports/TEST-HammerCLI-constant-path.xml +15 -0
  183. data/test/reports/{TEST-String-interactive-.xml → TEST-HammerCLI-interactive-.xml} +4 -4
  184. data/test/reports/TEST-HammerCLI.xml +7 -0
  185. data/test/reports/TEST-Logging-LogEvent-filtering.xml +9 -0
  186. data/test/reports/TEST-Logging-LogEvent.xml +7 -0
  187. data/test/reports/TEST-MiniTest-Spec.xml +1 -1
  188. data/test/reports/TEST-String-camelize.xml +3 -3
  189. data/test/reports/TEST-String-constantize.xml +15 -0
  190. data/test/reports/TEST-String-formatting.xml +6 -6
  191. data/test/reports/TEST-String-indent.xml +3 -3
  192. data/test/reports/TEST-String-underscore.xml +13 -0
  193. data/test/reports/TEST-String.xml +1 -1
  194. data/test/reports/TEST-commands-defaults-add.xml +21 -0
  195. data/test/reports/TEST-commands-defaults-delete.xml +11 -0
  196. data/test/reports/TEST-commands-defaults-list.xml +11 -0
  197. data/test/reports/TEST-commands-defaults-providers.xml +11 -0
  198. data/test/reports/TEST-commands.xml +7 -0
  199. data/test/reports/TEST-constraints-HammerCLI-Validator-AllConstraint-exist-.xml +4 -4
  200. data/test/reports/TEST-constraints-HammerCLI-Validator-AllConstraint.xml +1 -1
  201. data/test/reports/TEST-constraints-HammerCLI-Validator-AnyConstraint-exist-.xml +4 -4
  202. data/test/reports/TEST-constraints-HammerCLI-Validator-AnyConstraint.xml +1 -1
  203. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-exist-.xml +2 -2
  204. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-rejected.xml +4 -4
  205. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint-required.xml +4 -4
  206. data/test/reports/TEST-constraints-HammerCLI-Validator-BaseConstraint.xml +1 -1
  207. data/test/reports/TEST-constraints.xml +1 -1
  208. data/test/unit/apipie/command_test.rb +1 -0
  209. data/test/unit/defaults_test.rb +45 -0
  210. data/test/unit/fixtures/defaults/defaults.yml +6 -0
  211. data/test/unit/output/adapter/json_test.rb +30 -0
  212. data/test/unit/output/adapter/yaml_test.rb +28 -0
  213. metadata +258 -186
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c856eea7fbf8e0734d0d499654229d0eae1ecd69
4
- data.tar.gz: 5c86eebfc85d5855f88770833fa4e95c98ee3660
3
+ metadata.gz: ed76c22f08106cd7634ad1fed540dcf36fd1c5df
4
+ data.tar.gz: 9940232f3680440d7e8b86d6f102d1d03c87c8ba
5
5
  SHA512:
6
- metadata.gz: 11cf2b3b33444a8dabbd84e206b5eb6e57843aeea22a8bc210d6c6276682f5621e285561ba18eacd7be56201f9816dee467635db73c065bf628c405a033bbd8c
7
- data.tar.gz: 4d6a8dc7c47be4b247cffb105dc21b36de4e5258385bac46b67d7eee9cfade4c5393be44197f948da0da1ed673af2109d965ef462cc6dc0c6cdddfabd0597b2a
6
+ metadata.gz: 5d48b14bfa16dc4ecbf0cc41e6acd97984597ac2ffca027220b9f01dd7139f35bb1c040b99a44669ecca0e229c90a4017513779150d9019c6d8e8d6383c8386e
7
+ data.tar.gz: d57b562879af67ef096b18e5010ca0ac158b6ee692275aeb4e26aaebfbdb5ec70ee46e82ec28f1e2f2e76754189d9946b032c4ce82211e8207c27d40e2870fc7
data/bin/hammer CHANGED
@@ -122,4 +122,4 @@ HammerCLI::I18n.domains.each do |domain|
122
122
  logger.debug "'#{domain.type}' files for locale domain '#{domain.domain_name}' loaded from '#{File.expand_path(domain.locale_dir)}'"
123
123
  end
124
124
 
125
- exit HammerCLI::MainCommand.run || HammerCLI::EX_OK
125
+ exit HammerCLI::MainCommand.run(File.basename($0), ARGV, HammerCLI.context) || HammerCLI::EX_OK
data/doc/release_notes.md CHANGED
@@ -1,6 +1,15 @@
1
1
  Release notes
2
2
  =============
3
3
 
4
+ ### 0.5.1 (2015-12-15)
5
+ * Minor release to fix wrongly packaged gem
6
+
7
+ ### 0.5.0 (2015-12-14)
8
+ * Added defaults options in hammer cli ([#8015](http://projects.theforeman.org/issues/8015))
9
+ * Do not display hidden options in --help ([#12693](http://projects.theforeman.org/issues/12693))
10
+ * Refs #10564 - interpolate option in i18n string correctly ([#10564](http://projects.theforeman.org/issues/10564))
11
+ * Json and yaml formatting for messages ([#11355](http://projects.theforeman.org/issues/11355))
12
+
4
13
  ### 0.4.0 (2015-09-21)
5
14
  * Pull in the downstream translations ([#11184](http://projects.theforeman.org/issues/11184))
6
15
  * Abort when custom config file is not found ([#11158](http://projects.theforeman.org/issues/11158))
@@ -5,7 +5,6 @@ require 'hammer_cli/clamp'
5
5
  require 'hammer_cli/subcommand'
6
6
  require 'hammer_cli/options/matcher'
7
7
  require 'logging'
8
-
9
8
  module HammerCLI
10
9
 
11
10
  class AbstractCommand < Clamp::Command
@@ -71,6 +70,7 @@ module HammerCLI
71
70
  items.sort! do |a, b|
72
71
  a.help[0] <=> b.help[0]
73
72
  end
73
+ items.reject! {|item| item.respond_to?(:hidden?) && item.hidden?}
74
74
 
75
75
  puts "\n#{heading}:"
76
76
 
@@ -233,10 +233,12 @@ module HammerCLI
233
233
  end
234
234
 
235
235
  def all_options
236
- self.class.recognised_options.inject({}) do |h, opt|
237
- h[opt.attribute_name] = send(opt.read_method)
238
- h
236
+ @all_options ||= self.class.recognised_options.inject({}) do |hash, opt|
237
+ hash[opt.attribute_name] = send(opt.read_method)
238
+ hash[opt.attribute_name] = add_custom_defaults(opt.attribute_name) if hash[opt.attribute_name].nil?
239
+ hash
239
240
  end
241
+ @all_options
240
242
  end
241
243
 
242
244
  def options
@@ -245,6 +247,14 @@ module HammerCLI
245
247
 
246
248
  private
247
249
 
250
+ def add_custom_defaults(attr)
251
+ if context[:defaults]
252
+ value = context[:defaults].get_defaults(attr)
253
+ logger.info("Custom default value #{value} was used for attribute #{attr}") if value
254
+ value
255
+ end
256
+ end
257
+
248
258
  def self.inherited_output_definition
249
259
  od = nil
250
260
  if superclass.respond_to? :output_definition
@@ -0,0 +1,13 @@
1
+ require 'hammer_cli/defaults'
2
+
3
+ module HammerCLI
4
+
5
+ def self.context
6
+ {
7
+ :defaults => HammerCLI.defaults
8
+ }
9
+ end
10
+
11
+ end
12
+
13
+
@@ -0,0 +1,83 @@
1
+ require 'hammer_cli/defaults_commands'
2
+ module HammerCLI
3
+ DEFAULT_FILE = "#{Dir.home}/.hammer/defaults.yml"
4
+
5
+ class Defaults
6
+ class DefaultsError < StandardError; end
7
+ class DefaultsPathError < DefaultsError; end
8
+
9
+ attr_reader :defaults_settings
10
+
11
+ def initialize(settings, file_path = nil)
12
+
13
+ @defaults_settings = settings || {}
14
+ @path = file_path || DEFAULT_FILE
15
+ end
16
+
17
+ def register_provider(provider)
18
+ providers[provider.provider_name.to_s] = provider
19
+ end
20
+
21
+ def providers
22
+ @providers ||= {}
23
+ end
24
+
25
+ def delete_default_from_conf(param)
26
+ conf_file = YAML.load_file(path)
27
+ conf_file[:defaults].delete(param)
28
+ write_to_file conf_file
29
+ conf_file
30
+ end
31
+
32
+ def add_defaults_to_conf(default_options, provider)
33
+ create_default_file if defaults_settings.nil?
34
+ defaults = YAML.load_file(path)
35
+ defaults[:defaults] ||= {}
36
+ default_options.each do |key, value|
37
+ key = key.to_sym
38
+ defaults[:defaults][key] = value ? {:value => value,} : {:provider => provider}
39
+ end
40
+ write_to_file defaults
41
+ defaults
42
+ end
43
+
44
+ def get_defaults(opt)
45
+ option = opt
46
+ option = opt.gsub("option_",'') if opt.include? "option_"
47
+ unless defaults_settings.nil? || defaults_settings[option.to_sym].nil?
48
+ if defaults_settings[option.to_sym][:provider]
49
+ providers[defaults_settings[option.to_sym][:provider]].get_defaults(option.to_sym)
50
+ else
51
+ defaults_settings[option.to_sym][:value]
52
+ end
53
+ end
54
+ end
55
+
56
+ def write_to_file(defaults)
57
+ File.open(path,'w') do |h|
58
+ h.write defaults.to_yaml
59
+ end
60
+ end
61
+
62
+ protected
63
+
64
+ attr_reader :path
65
+
66
+ def create_default_file
67
+ if Dir.exist?(File.dirname(@path))
68
+ new_file = File.new(path, "w")
69
+ new_file.write ":defaults:"
70
+ new_file.close
71
+ else
72
+ raise DefaultsPathError.new(_("Couldn't create %s please create the path before defaults are enabled.") % path)
73
+ end
74
+ end
75
+ end
76
+
77
+ def self.defaults
78
+ @defaults ||= Defaults.new(HammerCLI::Settings.settings[:defaults])
79
+
80
+ end
81
+
82
+ HammerCLI::MainCommand.subcommand "defaults", _("Defaults management"), HammerCLI::DefaultsCommand
83
+ end
@@ -0,0 +1,160 @@
1
+ require 'hammer_cli'
2
+ require 'yaml'
3
+ module HammerCLI
4
+ class BaseDefaultsProvider
5
+ attr_reader :provider_name, :supported_defaults, :description
6
+
7
+ def initialize
8
+ @provider_name = nil
9
+ @supported_defaults = nil
10
+ @description = 'Abstract provider'
11
+ end
12
+
13
+ def param_supported?(param)
14
+ @supported_defaults.nil? || @supported_defaults.any? {|s| s.to_s == param}
15
+ end
16
+
17
+ def get_defaults
18
+ raise NotImplementedError
19
+ end
20
+ end
21
+
22
+ class DefaultsCommand < HammerCLI::AbstractCommand
23
+ class ProvidersDefaultsCommand < HammerCLI::DefaultsCommand
24
+ command_name 'providers'
25
+ desc _('List all the providers')
26
+
27
+ def execute
28
+ data = context[:defaults].providers.map do |key, val|
29
+ {
30
+ :provider => key.to_s,
31
+ :defaults => (val.supported_defaults || ['*']).map(&:to_s),
32
+ :description => val.description
33
+ }
34
+ end
35
+
36
+ fields = HammerCLI::Output::Dsl.new.build do
37
+ field :provider, _('Provider')
38
+ field :defaults, _('Supported defaults'), Fields::List
39
+ field :description, _('Description')
40
+ end
41
+
42
+ definition = HammerCLI::Output::Definition.new
43
+ definition.append(fields)
44
+
45
+ print_collection(definition, data)
46
+ HammerCLI::EX_OK
47
+ end
48
+
49
+ def adapter
50
+ @context[:adapter] || :table
51
+ end
52
+ end
53
+
54
+ class ListDefaultsCommand < HammerCLI::DefaultsCommand
55
+ command_name 'list'
56
+ desc _('List all the default parameters')
57
+
58
+ def execute
59
+ data = context[:defaults].defaults_settings.map do |key, val|
60
+ {
61
+ :parameter => key.to_s,
62
+ :value => val[:provider] ? "Provided by: " + val[:provider].to_s.capitalize : val[:value]
63
+ }
64
+ end
65
+
66
+ fields = HammerCLI::Output::Dsl.new.build do
67
+ field :parameter, _('Parameter')
68
+ field :value, _('Value'), Fields::List
69
+ end
70
+
71
+ definition = HammerCLI::Output::Definition.new
72
+ definition.append(fields)
73
+
74
+ print_collection(definition, data)
75
+ HammerCLI::EX_OK
76
+ end
77
+
78
+ def adapter
79
+ @context[:adapter] || :table
80
+ end
81
+ end
82
+
83
+ class DeleteDefaultsCommand < HammerCLI::DefaultsCommand
84
+ command_name 'delete'
85
+
86
+ desc _('Delete a default param')
87
+ option "--param-name", "OPTION_NAME", _("The name of the default option"), :required => true
88
+
89
+ def execute
90
+ if context[:defaults].defaults_settings && context[:defaults].defaults_settings[option_param_name.to_sym]
91
+ context[:defaults].delete_default_from_conf(option_param_name.to_sym)
92
+ param_deleted(option_param_name)
93
+ else
94
+ variable_not_found
95
+ end
96
+ HammerCLI::EX_OK
97
+ end
98
+ end
99
+
100
+ class AddDefaultsCommand < HammerCLI::DefaultsCommand
101
+ command_name 'add'
102
+
103
+ desc _('Add a default parameter to config')
104
+ option "--param-name", "OPTION_NAME", _("The name of the default option (e.g. organization_id)."), :required => true
105
+ option "--param-value", "OPTION_VALUE", _("The value for the default option")
106
+ option "--provider", "OPTION_PROVIDER", _("The name of the provider providing the value. For list available providers see `hammer defaults providers`.")
107
+
108
+ def execute
109
+ if option_provider.nil? && option_param_value.nil? || !option_provider.nil? && !option_param_value.nil?
110
+ bad_input
111
+ HammerCLI::EX_USAGE
112
+ else
113
+ if option_provider
114
+ namespace = option_provider
115
+ if !context[:defaults].providers.key?(namespace)
116
+ provider_prob_message(namespace)
117
+ return HammerCLI::EX_USAGE
118
+ elsif !context[:defaults].providers[namespace].param_supported?(option_param_name)
119
+ defaults_not_supported_by_provider
120
+ return HammerCLI::EX_CONFIG
121
+ end
122
+ end
123
+ context[:defaults].add_defaults_to_conf({option_param_name => option_param_value}, namespace)
124
+ added_default_message(option_param_name.to_s, option_param_value)
125
+ HammerCLI::EX_OK
126
+ end
127
+ rescue Defaults::DefaultsError, SystemCallError => e
128
+ print_message(e.message)
129
+ HammerCLI::EX_CONFIG
130
+ end
131
+ end
132
+
133
+ def added_default_message(key, value)
134
+ print_message(_("Added %{key_val} default-option with value that will be generated from the server.") % {:key_val => key.to_s}) if value.nil?
135
+ print_message(_("Added %{key_val} default-option with value %{val_val}.") % {:key_val => key.to_s, :val_val => value.to_s}) unless value.nil?
136
+ end
137
+
138
+ def provider_prob_message(namespace)
139
+ print_message(_("Provider #{namespace} was not found. See `hammer defaults providers` for available providers."))
140
+ end
141
+
142
+ def defaults_not_supported_by_provider
143
+ print_message(_("The param name is not supported by provider. See `hammer defaults providers` for supported params."))
144
+ end
145
+
146
+ def param_deleted(param)
147
+ print_message(_("%{param} was deleted successfully.") % {:param => param.to_s})
148
+ end
149
+
150
+ def bad_input
151
+ print_message(_("You must specify value or a provider name, cant specify both."))
152
+ end
153
+
154
+ def variable_not_found
155
+ print_message(_("Couldn't find the requested param in %s.") % context[:defaults].send(:path))
156
+ end
157
+
158
+ autoload_subcommands
159
+ end
160
+ end
@@ -53,11 +53,11 @@ module HammerCLI
53
53
  end
54
54
 
55
55
  def handles?(switch)
56
- message = _("Warning: Option #{switch} is deprecated. %s")
56
+ message = _("Warning: Option %{option} is deprecated. %{message}")
57
57
  if deprecated_switches.class <= String && switches.include?(switch)
58
- warn(message % deprecated_switches)
58
+ warn(message % { :option => switch, :message => deprecated_switches })
59
59
  elsif deprecated_switches.class <= Hash && deprecated_switches.keys.include?(switch)
60
- warn(message % deprecated_switches[switch])
60
+ warn(message % { :option => switch, :message => deprecated_switches[switch] })
61
61
  end
62
62
  super(switch)
63
63
  end
@@ -11,6 +11,19 @@ module HammerCLI::Output::Adapter
11
11
  puts JSON.pretty_generate(result)
12
12
  end
13
13
 
14
+ def print_message(msg, msg_params={})
15
+ id = msg_params["id"] || msg_params[:id]
16
+ name = msg_params["name"] || msg_params[:name]
17
+
18
+ data = {
19
+ :message => msg.format(msg_params)
20
+ }
21
+ data[:id] = id unless id.nil?
22
+ data[:name] = name unless name.nil?
23
+
24
+ puts JSON.pretty_generate(data)
25
+ end
26
+
14
27
  end
15
28
 
16
29
  HammerCLI::Output::Output.register_adapter(:json, Json)
@@ -11,6 +11,19 @@ module HammerCLI::Output::Adapter
11
11
  puts YAML.dump(result)
12
12
  end
13
13
 
14
+ def print_message(msg, msg_params={})
15
+ id = msg_params["id"] || msg_params[:id]
16
+ name = msg_params["name"] || msg_params[:name]
17
+
18
+ data = {
19
+ :message => msg.format(msg_params)
20
+ }
21
+ data[:id] = id unless id.nil?
22
+ data[:name] = name unless name.nil?
23
+
24
+ puts YAML.dump(data)
25
+ end
26
+
14
27
  end
15
28
  HammerCLI::Output::Output.register_adapter(:yaml, Yaml)
16
29
  end
@@ -18,6 +18,7 @@ module HammerCLI
18
18
  if File.directory? full_path
19
19
  # check for cli_config.yml
20
20
  load_from_file(File.join(full_path, 'cli_config.yml'))
21
+ load_from_file(File.join(full_path, 'defaults.yml'))
21
22
  # load config for modules
22
23
  Dir.glob(File.join(full_path, 'cli.modules.d/*.yml')).sort.each do |f|
23
24
  load_from_file(f)
@@ -1,5 +1,5 @@
1
1
  module HammerCLI
2
2
  def self.version
3
- @version ||= Gem::Version.new '0.4.0'
3
+ @version ||= Gem::Version.new '0.5.1'
4
4
  end
5
5
  end
data/lib/hammer_cli.rb CHANGED
@@ -13,9 +13,11 @@ require 'hammer_cli/completer'
13
13
  require 'hammer_cli/option_builder'
14
14
  require 'hammer_cli/abstract'
15
15
  require 'hammer_cli/main'
16
+ require 'hammer_cli/context'
16
17
 
17
18
  require 'hammer_cli/apipie'
18
19
 
19
20
  # extend MainCommand
20
21
  require 'hammer_cli/shell'
22
+ require 'hammer_cli/defaults'
21
23