eco-helpers 1.4.2 → 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +146 -3
  3. data/LICENSE +21 -0
  4. data/eco-helpers.gemspec +11 -10
  5. data/lib/eco/api.rb +3 -0
  6. data/lib/eco/api/common.rb +5 -1
  7. data/lib/eco/api/common/base_loader.rb +54 -0
  8. data/lib/eco/api/common/class_auto_loader.rb +109 -0
  9. data/lib/eco/api/common/class_helpers.rb +33 -0
  10. data/lib/eco/api/common/class_hierarchy.rb +1 -1
  11. data/lib/eco/api/common/class_meta_basics.rb +16 -0
  12. data/lib/eco/api/common/loaders.rb +13 -0
  13. data/lib/eco/api/common/loaders/error_handler.rb +41 -0
  14. data/lib/eco/api/common/loaders/parser.rb +127 -0
  15. data/lib/eco/api/common/loaders/policy.rb +25 -0
  16. data/lib/eco/api/common/loaders/use_case.rb +40 -0
  17. data/lib/eco/api/common/people/default_parsers.rb +5 -10
  18. data/lib/eco/api/common/people/default_parsers/boolean_parser.rb +13 -23
  19. data/lib/eco/api/common/people/default_parsers/csv_parser.rb +20 -35
  20. data/lib/eco/api/common/people/default_parsers/date_parser.rb +15 -26
  21. data/lib/eco/api/common/people/default_parsers/freemium_parser.rb +20 -0
  22. data/lib/eco/api/common/people/default_parsers/login_providers_parser.rb +26 -0
  23. data/lib/eco/api/common/people/default_parsers/multi_parser.rb +15 -27
  24. data/lib/eco/api/common/people/default_parsers/numeric_parser.rb +14 -19
  25. data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +31 -0
  26. data/lib/eco/api/common/people/default_parsers/send_invites_parser.rb +15 -24
  27. data/lib/eco/api/common/people/entries.rb +54 -24
  28. data/lib/eco/api/common/people/entry_factory.rb +18 -15
  29. data/lib/eco/api/common/people/person_attribute_parser.rb +29 -12
  30. data/lib/eco/api/common/people/person_entry.rb +308 -216
  31. data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +5 -2
  32. data/lib/eco/api/common/people/person_parser.rb +52 -19
  33. data/lib/eco/api/common/session/base_session.rb +3 -6
  34. data/lib/eco/api/common/session/environment.rb +2 -23
  35. data/lib/eco/api/common/session/logger.rb +4 -0
  36. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +2 -0
  37. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +9 -1
  38. data/lib/eco/api/common/version_patches/exception.rb +22 -0
  39. data/lib/eco/api/custom.rb +13 -0
  40. data/lib/eco/api/custom/error_handler.rb +20 -0
  41. data/lib/eco/api/custom/namespace.rb +7 -0
  42. data/lib/eco/api/custom/parser.rb +50 -0
  43. data/lib/eco/api/custom/policy.rb +28 -0
  44. data/lib/eco/api/custom/use_case.rb +16 -0
  45. data/lib/eco/api/error.rb +1 -0
  46. data/lib/eco/api/error/handlers.rb +10 -3
  47. data/lib/eco/api/microcases.rb +35 -0
  48. data/lib/eco/api/microcases/account_excluded.rb +24 -0
  49. data/lib/eco/api/microcases/append_usergroups.rb +19 -0
  50. data/lib/eco/api/microcases/core_excluded.rb +20 -0
  51. data/lib/eco/api/microcases/fix_default_group.rb +34 -0
  52. data/lib/eco/api/microcases/fix_filter_tags.rb +42 -0
  53. data/lib/eco/api/microcases/people_cache.rb +17 -0
  54. data/lib/eco/api/microcases/people_load.rb +59 -0
  55. data/lib/eco/api/microcases/people_refresh.rb +31 -0
  56. data/lib/eco/api/microcases/people_search.rb +65 -0
  57. data/lib/eco/api/microcases/refresh_abilities.rb +19 -0
  58. data/lib/eco/api/microcases/refresh_default_tag.rb +27 -0
  59. data/lib/eco/api/microcases/s3upload_targets.rb +39 -0
  60. data/lib/eco/api/microcases/set_account.rb +20 -0
  61. data/lib/eco/api/microcases/set_core.rb +18 -0
  62. data/lib/eco/api/microcases/set_core_with_supervisor.rb +23 -0
  63. data/lib/eco/api/microcases/set_supervisor.rb +30 -0
  64. data/lib/eco/api/microcases/strict_search.rb +19 -0
  65. data/lib/eco/api/microcases/with_each.rb +27 -0
  66. data/lib/eco/api/microcases/with_each_leaver.rb +24 -0
  67. data/lib/eco/api/microcases/with_each_present.rb +30 -0
  68. data/lib/eco/api/microcases/with_each_starter.rb +30 -0
  69. data/lib/eco/api/microcases/with_each_subordinate.rb +34 -0
  70. data/lib/eco/api/microcases/with_supervisor.rb +36 -0
  71. data/lib/eco/api/organization/people.rb +72 -35
  72. data/lib/eco/api/organization/presets_factory.rb +13 -4
  73. data/lib/eco/api/organization/presets_reference.json +9 -1
  74. data/lib/eco/api/organization/presets_values.json +4 -1
  75. data/lib/eco/api/policies.rb +11 -7
  76. data/lib/eco/api/session.rb +62 -29
  77. data/lib/eco/api/session/batch.rb +2 -45
  78. data/lib/eco/api/session/batch/base_policy.rb +7 -6
  79. data/lib/eco/api/session/batch/errors.rb +28 -4
  80. data/lib/eco/api/session/batch/feedback.rb +7 -1
  81. data/lib/eco/api/session/batch/job.rb +40 -23
  82. data/lib/eco/api/session/batch/jobs.rb +9 -4
  83. data/lib/eco/api/session/batch/jobs_groups.rb +1 -1
  84. data/lib/eco/api/session/batch/request_stats.rb +95 -58
  85. data/lib/eco/api/session/batch/status.rb +35 -31
  86. data/lib/eco/api/session/config.rb +106 -44
  87. data/lib/eco/api/session/config/api.rb +132 -7
  88. data/lib/eco/api/session/config/apis.rb +24 -25
  89. data/lib/eco/api/session/config/logger.rb +2 -2
  90. data/lib/eco/api/session/config/post_launch.rb +1 -1
  91. data/lib/eco/api/session/config/workflow.rb +8 -7
  92. data/lib/eco/api/usecases.rb +47 -33
  93. data/lib/eco/api/usecases/backup/append_usergroups_case.rb +36 -0
  94. data/lib/eco/api/usecases/backup/create_case.rb +104 -0
  95. data/lib/eco/api/usecases/backup/create_details_case.rb +31 -0
  96. data/lib/eco/api/usecases/backup/create_details_with_supervisor_case.rb +48 -0
  97. data/lib/eco/api/usecases/backup/hris_case.rb +124 -0
  98. data/lib/eco/api/usecases/backup/set_default_tag_case.rb +49 -0
  99. data/lib/eco/api/usecases/backup/set_supervisor_case.rb +41 -0
  100. data/lib/eco/api/usecases/backup/transfer_account_case.rb +90 -0
  101. data/lib/eco/api/usecases/backup/update_case.rb +112 -0
  102. data/lib/eco/api/usecases/backup/update_details_case.rb +64 -0
  103. data/lib/eco/api/usecases/backup/upsert_case.rb +114 -0
  104. data/lib/eco/api/usecases/base_case.rb +2 -0
  105. data/lib/eco/api/usecases/base_io.rb +3 -3
  106. data/lib/eco/api/usecases/default_cases.rb +23 -53
  107. data/lib/eco/api/usecases/default_cases/append_usergroups_case.rb +10 -31
  108. data/lib/eco/api/usecases/default_cases/change_email_case.rb +23 -47
  109. data/lib/eco/api/usecases/default_cases/codes_to_tags_case.rb +56 -43
  110. data/lib/eco/api/usecases/default_cases/create_case.rb +15 -101
  111. data/lib/eco/api/usecases/default_cases/create_details_case.rb +11 -26
  112. data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +12 -43
  113. data/lib/eco/api/usecases/default_cases/delete_sync_case.rb +11 -0
  114. data/lib/eco/api/usecases/default_cases/delete_trans_case.rb +14 -0
  115. data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +10 -21
  116. data/lib/eco/api/usecases/default_cases/hris_case.rb +23 -120
  117. data/lib/eco/api/usecases/default_cases/new_email_case.rb +10 -23
  118. data/lib/eco/api/usecases/default_cases/new_id_case.rb +11 -25
  119. data/lib/eco/api/usecases/default_cases/new_id_case0.rb +14 -0
  120. data/lib/eco/api/usecases/default_cases/org_data_convert_case.rb +83 -0
  121. data/lib/eco/api/usecases/default_cases/refresh_abilities_case.rb +30 -0
  122. data/lib/eco/api/usecases/default_cases/refresh_case.rb +7 -20
  123. data/lib/eco/api/usecases/default_cases/reinvite_sync_case.rb +11 -0
  124. data/lib/eco/api/usecases/default_cases/reinvite_trans_case.rb +17 -0
  125. data/lib/eco/api/usecases/default_cases/remove_account_sync_case.rb +11 -0
  126. data/lib/eco/api/usecases/default_cases/remove_account_trans_case.rb +17 -0
  127. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +9 -19
  128. data/lib/eco/api/usecases/default_cases/restore_db_case.rb +92 -0
  129. data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +32 -40
  130. data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +15 -33
  131. data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +66 -57
  132. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +36 -44
  133. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +40 -55
  134. data/lib/eco/api/usecases/default_cases/transfer_account_case.rb +264 -84
  135. data/lib/eco/api/usecases/default_cases/update_case.rb +15 -109
  136. data/lib/eco/api/usecases/default_cases/update_details_case.rb +14 -61
  137. data/lib/eco/api/usecases/default_cases/upsert_case.rb +16 -111
  138. data/lib/eco/api/usecases/use_case_io.rb +9 -9
  139. data/lib/eco/cli/config.rb +10 -2
  140. data/lib/eco/cli/config/default.rb +2 -1
  141. data/lib/eco/cli/config/default/input_filters.rb +58 -0
  142. data/lib/eco/cli/config/default/options.rb +60 -25
  143. data/lib/eco/cli/config/default/people.rb +4 -4
  144. data/lib/eco/cli/config/default/people_filters.rb +108 -0
  145. data/lib/eco/cli/config/default/usecases.rb +69 -32
  146. data/lib/eco/cli/config/default/workflow.rb +37 -27
  147. data/lib/eco/cli/config/filters.rb +50 -0
  148. data/lib/eco/cli/config/filters/input_filters.rb +29 -0
  149. data/lib/eco/cli/config/filters/people_filters.rb +29 -0
  150. data/lib/eco/cli/config/help.rb +49 -0
  151. data/lib/eco/cli/config/options_set.rb +17 -1
  152. data/lib/eco/cli/config/use_cases.rb +79 -53
  153. data/lib/eco/cli/scripting.rb +10 -2
  154. data/lib/eco/cli/scripting/args_helpers.rb +25 -15
  155. data/lib/eco/cli/scripting/argument.rb +1 -0
  156. data/lib/eco/cli/scripting/arguments.rb +1 -1
  157. data/lib/eco/csv.rb +8 -3
  158. data/lib/eco/csv/table.rb +1 -1
  159. data/lib/eco/data/crypto/encryption.rb +3 -0
  160. data/lib/eco/data/files/helpers.rb +6 -1
  161. data/lib/eco/language/match.rb +19 -9
  162. data/lib/eco/language/match_modifier.rb +13 -5
  163. data/lib/eco/language/models/collection.rb +77 -56
  164. data/lib/eco/language/models/parser_serializer.rb +39 -15
  165. data/lib/eco/version.rb +1 -1
  166. metadata +149 -63
  167. data/lib/eco/api/session/task.rb +0 -175
  168. data/lib/eco/api/usecases/default_case.rb +0 -19
  169. data/lib/eco/api/usecases/default_cases/delete_case.rb +0 -32
  170. data/lib/eco/api/usecases/default_cases/recover_db_case.rb +0 -98
  171. data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +0 -26
  172. data/lib/eco/api/usecases/default_cases/reinvite_case.rb +0 -41
  173. data/lib/eco/api/usecases/default_cases/remove_account_case.rb +0 -38
  174. data/lib/eco/cli/config/default/filters.rb +0 -70
  175. data/lib/eco/cli/config/people_filters.rb +0 -38
@@ -0,0 +1,50 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ class Filters
5
+ include Eco::CLI::Config::Help
6
+ attr_reader :core_config
7
+
8
+ def initialize(core_config:)
9
+ @core_config = core_config
10
+ @filters = {}
11
+ @description = {}
12
+ end
13
+
14
+ # @return [String] summary of the filters.
15
+ def help(msg = "The following are the available filters:")
16
+ [msg].yield_self do |lines|
17
+ max_len = keys_max_len(@filters.keys)
18
+ @filters.keys.sort.each do |key|
19
+ lines << help_line(key, @description[key], max_len)
20
+ end
21
+ lines
22
+ end.join("\n")
23
+ end
24
+
25
+ # @param option [String] the command line option that activates this filter.
26
+ # @param desc [String] description of the filter.
27
+ def add(option, desc = nil)
28
+ raise "Missing block to define the filters builder" unless block_given?
29
+ callback = Proc.new
30
+ [option].flatten.compact.each do |opt|
31
+ @filters[opt] = callback
32
+ @description[opt] = desc
33
+ end
34
+ self
35
+ end
36
+
37
+ def process(io:)
38
+ raise "You need to override this method in child classes"
39
+ end
40
+
41
+ private
42
+
43
+
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ require_relative 'filters/people_filters'
50
+ require_relative 'filters/input_filters'
@@ -0,0 +1,29 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ class Filters
5
+ class InputFilters < Eco::CLI::Config::Filters
6
+
7
+ def help
8
+ super("The following are the available filters on the input entries:")
9
+ end
10
+
11
+ def process(io:)
12
+ unless io && io.is_a?(Eco::API::UseCases::BaseIO)
13
+ raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
14
+ end
15
+
16
+ io = io.new(type: :import)
17
+ @filters.each do |arg, callback|
18
+ if SCR.get_arg(arg)
19
+ io = io.new(input: callback.call(*io.params))
20
+ end
21
+ end
22
+ io.input
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ class Filters
5
+ class PeopleFilters < Eco::CLI::Config::Filters
6
+
7
+ def help
8
+ super("The following are the available filters on people:")
9
+ end
10
+
11
+ def process(io:)
12
+ unless io && io.is_a?(Eco::API::UseCases::BaseIO)
13
+ raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
14
+ end
15
+
16
+ io = io.new(type: :transform)
17
+ @filters.each do |arg, callback|
18
+ if SCR.get_arg(arg)
19
+ io = io.new(people: callback.call(*io.params))
20
+ end
21
+ end
22
+ io.people
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,49 @@
1
+ module Eco
2
+ class CLI
3
+ class Config
4
+ module Help
5
+
6
+ def help
7
+ raise "this needs to be reimplemented in the child class and return a string"
8
+ end
9
+
10
+ private
11
+
12
+ def keys_max_len(keys)
13
+ keys.max {|a, b| a.length <=> b.length}.length
14
+ end
15
+
16
+ # Creatas a well aligned line
17
+ def help_line(key, desc, keys_max_len = key.length, line_len = 100)
18
+ blanks = keys_max_len + 3 - key.length
19
+ top_line = " #{key}#{" "*blanks} "
20
+ indent = top_line.length
21
+ first = true
22
+ each_slice_words(desc, line_len - indent).each_with_object([]) do |line, lines|
23
+ lines << (first ? "#{top_line}#{line}" : "#{" " * indent}#{line}")
24
+ first = false
25
+ end.join("\n")
26
+ end
27
+
28
+ def each_slice_words(str, max_len = 100)
29
+ liner = ""
30
+ str.to_s.scan(/[^\s]+|\s+/).each_with_object([]) do |part, out|
31
+ if "#{liner}#{part}".length <= max_len
32
+ liner << part
33
+ else
34
+ yield(liner) if block_given?
35
+ out << liner
36
+ liner = part.strip
37
+ end
38
+ end.tap do |out|
39
+ if out.empty? || !liner.empty?
40
+ yield(liner) if block_given?
41
+ out << liner if liner
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+ end
48
+ end
49
+ end
@@ -2,18 +2,34 @@ module Eco
2
2
  class CLI
3
3
  class Config
4
4
  class OptionsSet
5
+ include Eco::CLI::Config::Help
5
6
  attr_reader :core_config
6
7
 
7
8
  def initialize(core_config:)
8
9
  @core_config = core_config
9
10
  @options_set = {}
11
+ @description = {}
10
12
  end
11
13
 
12
- def add(option)
14
+ # @return [String] summary of the options.
15
+ def help
16
+ ["The following are the available options:"].yield_self do |lines|
17
+ max_len = keys_max_len(@options_set.keys)
18
+ @options_set.keys.each do |key|
19
+ lines << help_line(key, @description[key], max_len)
20
+ end
21
+ lines
22
+ end.join("\n")
23
+ end
24
+
25
+ # @param option [String] the command line option.
26
+ # @param desc [String] description of the option.
27
+ def add(option, desc = nil)
13
28
  raise "Missing block to define the options builder" unless block_given?
14
29
  callback = Proc.new
15
30
  [option].flatten.compact.each do |opt|
16
31
  @options_set[opt] = callback
32
+ @description[opt] = desc
17
33
  end
18
34
  self
19
35
  end
@@ -2,100 +2,126 @@ module Eco
2
2
  class CLI
3
3
  class Config
4
4
  class UseCases
5
-
5
+ include Eco::CLI::Config::Help
6
6
  attr_reader :core_config
7
7
 
8
8
  def initialize(core_config:)
9
9
  @core_config = core_config
10
- @linked_opt_cases = {}
10
+ @linked_cases = {}
11
+ @description = {}
12
+ end
13
+
14
+ # @return [String] summary of the use cases.
15
+ def help
16
+ ["The following are the available use cases:"].yield_self do |lines|
17
+ max_len = keys_max_len(@linked_cases.keys)
18
+ @linked_cases.keys.sort.each do |key|
19
+ lines << help_line(key, @description[key], max_len)
20
+ end
21
+ lines
22
+ end.join("\n")
11
23
  end
12
24
 
13
- # Integrates a usecase to the command line
14
- # @param option_case [String] the command line option to invoke the usecase
15
- # @param type [Symbol] the type of usecase
16
- # @param case_name [String, nil] the name of the usecase as defined
17
- def add(option_case, type, case_name: nil)
25
+ # Integrates a usecase to the command line.
26
+ # @param option_case [String] the command line option to invoke the usecase.
27
+ # @param type [Symbol] the type of usecase.
28
+ # @param desc [String] description of the case.
29
+ # @param case_name [String, nil] the name of the usecase as defined.
30
+ def add(option_case, type, desc = nil, case_name: nil)
18
31
  Eco::API::UseCases::UseCase.validate_type(type)
19
32
  unless callback = block_given?? Proc.new : nil
20
33
  raise "You must specify a valid 'case_name' when no block is provided" unless case_name
21
34
  raise "'case_name' expected to be a String. Given: #{case_name.class}" unless case_name.is_a?(String)
22
35
  end
23
36
 
24
- @linked_opt_cases[option_case] = {
37
+ @linked_cases[option_case] = {
25
38
  type => {
39
+ option: option_case,
40
+ type: type,
26
41
  casename: case_name,
27
42
  callback: callback
28
43
  }
29
44
  }
45
+ @description[option_case] = desc
30
46
 
31
47
  self
32
48
  end
33
49
 
34
50
  # Scopes/identifies which usecases are being invoked from the command line
51
+ # @note
52
+ # - this method will sort the active usecases by the position they hold in the command line
35
53
  # @param io [Eco::API::UseCases::BaseIO] the input/output object
36
54
  # @return [Hash] where keys are cases and values a `Hash` with `option` String and `callback`
37
55
  def active(io:)
56
+ validate_io!(io)
38
57
  return @active_cases unless !@active_cases
39
- @active_cases = {}.tap do |opt_cases|
40
- @linked_opt_cases.each do |option_case, types|
41
- types.each do |type, data|
42
- if SCR.get_arg(option_case)
43
- usecase = nil
44
- if case_name = data[:casename]
45
- usecase = io.session.usecases.case(case_name, type: type)
46
- end
47
-
48
- if callback = data[:callback]
49
- unless usecase
50
- # identify usecase
51
- params = io.params(keyed: true).merge(type: type)
52
- io = io.new(**params, validate: false)
53
-
54
- usecase = callback.call(*io.params)
55
- unless usecase.is_a?(Eco::API::UseCases::UseCase)
56
- msg = "When adding a usecase, without specifying 'case_name:', "
57
- msg += "the block that integrates usecase for cli option '#{option_case}'"
58
- msg += " must return an Eco::API::UseCases::UseCase object. It returns #{usecase.class}"
59
- raise msg
60
- end
61
- end
62
- end
63
-
64
- if usecase
65
- opt_cases[usecase] = {
66
- option: option_case,
67
- callback: data[:callback]
68
- }
69
- end
70
-
71
- end
58
+ active_cases = {}
59
+ @linked_cases.each do |option_case, types|
60
+ next nil unless SCR.get_arg(option_case)
61
+ types.each do |type, data|
62
+ if usecase = get_usecase(io: io, data: data)
63
+ active_cases[usecase] = {
64
+ index: SCR.get_arg_index(option_case),
65
+ option: option_case,
66
+ callback: data[:callback]
67
+ }
72
68
  end
73
69
  end
74
-
75
70
  end
71
+ @active_cases = active_cases.sort_by {|c, d| d[:index]}.to_h
76
72
  end
77
73
 
78
74
  def process(io:)
79
- unless io && io.is_a?(Eco::API::UseCases::BaseIO)
80
- raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
81
- end
82
-
75
+ validate_io!(io)
83
76
  processed = false
84
77
  active(io: io).each do |usecase, data|
85
78
  raise "Something went wrong when scoping active cases" unless data
86
-
87
79
  processed = true
80
+ io = case_io(io: io, usecase: usecase)
81
+ # some usecases have a callback to collect the parameters
82
+ data[:callback]&.call(*io.params)
83
+ io = usecase.launch(io: io)
84
+ end
85
+ processed
86
+ end
88
87
 
89
- params = io.params(keyed: true).merge(type: usecase.type)
90
- io = io.new(**params)
88
+ private
89
+
90
+ # Gets a `UseCaseIO`
91
+ def case_io(io:, usecase:)
92
+ validate_io!(io)
93
+ case io
94
+ when Eco::API::UseCases::UseCaseIO
95
+ io.chain(usecase: usecase)
96
+ when Eco::API::UseCases::BaseIO
97
+ params = io.params(keyed: true).merge(usecase: usecase)
98
+ Eco::API::UseCases::UseCaseIO.new(**params)
99
+ end
100
+ end
91
101
 
92
- if callback = data[:callback]
93
- callback.call(*io.params)
102
+ def get_usecase(io:, data:)
103
+ usecase = if case_name = data[:casename]
104
+ io.session.usecases.case(case_name, type: data[:type])
105
+ end
106
+ usecase ||= if callback = data[:callback]
107
+ # identify/retrieve usecase via callback
108
+ params = io.params(keyed: true).merge(type: data[:type])
109
+ io = io.new(**params, validate: false)
110
+ callback.call(*io.params).tap do |usecase|
111
+ unless usecase.is_a?(Eco::API::UseCases::UseCase)
112
+ msg = "When adding a usecase, without specifying 'case_name:', "
113
+ msg += "the block that integrates usecase for cli option '#{data[:option]}'"
114
+ msg += " must return an Eco::API::UseCases::UseCase object. It returns #{usecase.class}"
115
+ raise msg
116
+ end
94
117
  end
118
+ end
119
+ end
95
120
 
96
- usecase.launch(io: io)
121
+ def validate_io!(io)
122
+ unless io && io.is_a?(Eco::API::UseCases::BaseIO)
123
+ raise "You need to provide Eco::API::UseCases::BaseIO object. Given: #{io.class}"
97
124
  end
98
- processed
99
125
  end
100
126
 
101
127
  end
@@ -7,8 +7,16 @@ module Eco
7
7
  class Scripting
8
8
  include Scripting::ArgsHelpers
9
9
 
10
+ def initialize(argv = ARGV)
11
+ @argv = argv
12
+ end
13
+
14
+ def argv
15
+ @argv.dup
16
+ end
17
+
10
18
  def args_contain?(*values)
11
- match?(ARGV, patterns, [:any, :or, :insensitive, :pattern])
19
+ match?(argv, patterns, [:any, :or, :insensitive, :pattern])
12
20
  end
13
21
 
14
22
  def modifiers(*values)
@@ -20,4 +28,4 @@ module Eco
20
28
  end
21
29
 
22
30
 
23
- SCR = Eco::CLI::Scripting.new
31
+ SCR = Eco::CLI::Scripting.new(ARGV)
@@ -3,12 +3,16 @@ module Eco
3
3
  class Scripting
4
4
  module ArgsHelpers
5
5
 
6
+ def argv
7
+ @argv || ARGV
8
+ end
9
+
6
10
  def is_modifier?(value)
7
11
  Argument.is_modifier?(value)
8
12
  end
9
13
 
10
14
  def arguments
11
- @arguments ||= Arguments.new
15
+ @arguments ||= Arguments.new(argv)
12
16
  end
13
17
 
14
18
  def stop_on_unknown!(exclude: [], only_options: false)
@@ -23,38 +27,44 @@ module Eco
23
27
  end
24
28
  end
25
29
 
30
+ def get_arg_index(key)
31
+ return nil if !argv.include?(key)
32
+ argv.index(key)
33
+ end
34
+
26
35
  def get_arg(key, with_param: false, valid: true)
27
36
  # track what a known option looks like
28
37
  arguments.add(key, with_param: with_param)
29
- return nil if !ARGV.include?(key)
38
+ return nil unless index = get_arg_index(key)
30
39
  value = true
31
40
  if with_param
32
- next_i = ARGV.index(key) + 1
33
- value = ARGV[next_i]
41
+ value = argv[index + 1]
34
42
  #puts "modifier argument: #{value}"
35
- value = nil if valid && is_modifier?(value)
43
+ value = nil if valid && is_modifier?(value)
36
44
  end
37
45
  return value
38
46
  end
39
47
 
40
48
  def get_file(key, required: false, should_exist: true)
41
49
  filename = get_arg(key, with_param: true)
42
- if !filename
43
- if required
44
- puts "you need to specify a file '#{key} file'"
45
- exit(1)
46
- end
47
- elsif !(File.exists?(filename) || File.exists?(File.expand_path(filename)))
48
- if should_exist && required
49
- puts "file doesn't exist #{filename}"
50
- exit
51
- end
50
+ if !filename && required
51
+ puts "You need to specify a file '#{key} file'"
52
+ exit(1)
53
+ elsif !file_exists?(filename) && should_exist && required
54
+ puts "This file doesn't exist '#{filename}'"
55
+ exit(1)
52
56
  end
53
57
 
54
58
  filename = File.expand_path(filename) if filename && should_exist
55
59
  filename
56
60
  end
57
61
 
62
+ private
63
+
64
+ def file_exists?(filename)
65
+ File.exists?(filename) || File.exists?(File.expand_path(filename))
66
+ end
67
+
58
68
  end
59
69
  end
60
70
  end