eco-helpers 1.5.1 → 1.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +114 -2
- data/LICENSE +21 -0
- data/eco-helpers.gemspec +1 -1
- data/lib/eco/api.rb +2 -0
- data/lib/eco/api/common.rb +4 -0
- data/lib/eco/api/common/base_loader.rb +54 -0
- data/lib/eco/api/common/class_auto_loader.rb +109 -0
- data/lib/eco/api/common/class_helpers.rb +33 -0
- data/lib/eco/api/common/class_hierarchy.rb +1 -1
- data/lib/eco/api/common/class_meta_basics.rb +16 -0
- data/lib/eco/api/common/loaders.rb +13 -0
- data/lib/eco/api/common/loaders/error_handler.rb +41 -0
- data/lib/eco/api/common/loaders/parser.rb +127 -0
- data/lib/eco/api/common/loaders/policy.rb +25 -0
- data/lib/eco/api/common/loaders/use_case.rb +40 -0
- data/lib/eco/api/common/people/default_parsers.rb +3 -12
- data/lib/eco/api/common/people/default_parsers/boolean_parser.rb +13 -23
- data/lib/eco/api/common/people/default_parsers/csv_parser.rb +20 -35
- data/lib/eco/api/common/people/default_parsers/date_parser.rb +15 -26
- data/lib/eco/api/common/people/default_parsers/freemium_parser.rb +15 -25
- data/lib/eco/api/common/people/default_parsers/login_providers_parser.rb +26 -0
- data/lib/eco/api/common/people/default_parsers/multi_parser.rb +15 -27
- data/lib/eco/api/common/people/default_parsers/numeric_parser.rb +14 -19
- data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +24 -35
- data/lib/eco/api/common/people/default_parsers/send_invites_parser.rb +15 -25
- data/lib/eco/api/common/people/entries.rb +54 -24
- data/lib/eco/api/common/people/entry_factory.rb +10 -8
- data/lib/eco/api/common/people/person_attribute_parser.rb +29 -12
- data/lib/eco/api/common/people/person_entry.rb +308 -216
- data/lib/eco/api/common/people/person_entry_attribute_mapper.rb +3 -2
- data/lib/eco/api/common/people/person_parser.rb +51 -18
- data/lib/eco/api/common/session/logger.rb +4 -0
- data/lib/eco/api/common/version_patches.rb +1 -0
- data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +2 -0
- data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +1 -1
- data/lib/eco/api/common/version_patches/exception.rb +22 -0
- data/lib/eco/api/common/version_patches/object.rb +10 -0
- data/lib/eco/api/custom.rb +13 -0
- data/lib/eco/api/custom/error_handler.rb +20 -0
- data/lib/eco/api/custom/namespace.rb +7 -0
- data/lib/eco/api/custom/parser.rb +50 -0
- data/lib/eco/api/custom/policy.rb +28 -0
- data/lib/eco/api/custom/use_case.rb +16 -0
- data/lib/eco/api/error.rb +1 -0
- data/lib/eco/api/error/handlers.rb +10 -3
- data/lib/eco/api/microcases.rb +17 -13
- data/lib/eco/api/microcases/account_excluded.rb +24 -0
- data/lib/eco/api/microcases/append_usergroups.rb +19 -0
- data/lib/eco/api/microcases/core_excluded.rb +4 -4
- data/lib/eco/api/microcases/{set_default_group.rb → fix_default_group.rb} +10 -9
- data/lib/eco/api/microcases/fix_filter_tags.rb +26 -6
- data/lib/eco/api/microcases/people_cache.rb +17 -0
- data/lib/eco/api/microcases/people_load.rb +59 -0
- data/lib/eco/api/microcases/people_refresh.rb +31 -0
- data/lib/eco/api/microcases/people_search.rb +65 -0
- data/lib/eco/api/microcases/refresh_abilities.rb +19 -0
- data/lib/eco/api/microcases/refresh_default_tag.rb +27 -0
- data/lib/eco/api/microcases/s3upload_targets.rb +39 -0
- data/lib/eco/api/microcases/set_account.rb +7 -19
- data/lib/eco/api/microcases/set_core.rb +5 -5
- data/lib/eco/api/microcases/set_core_with_supervisor.rb +23 -0
- data/lib/eco/api/microcases/set_supervisor.rb +17 -13
- data/lib/eco/api/microcases/strict_search.rb +12 -7
- data/lib/eco/api/microcases/with_each.rb +27 -0
- data/lib/eco/api/microcases/with_each_leaver.rb +24 -0
- data/lib/eco/api/microcases/with_each_present.rb +30 -0
- data/lib/eco/api/microcases/with_each_starter.rb +30 -0
- data/lib/eco/api/microcases/with_each_subordinate.rb +34 -0
- data/lib/eco/api/microcases/with_supervisor.rb +36 -0
- data/lib/eco/api/organization/people.rb +72 -35
- data/lib/eco/api/organization/presets_factory.rb +13 -4
- data/lib/eco/api/policies.rb +11 -7
- data/lib/eco/api/session.rb +54 -24
- data/lib/eco/api/session/batch.rb +1 -1
- data/lib/eco/api/session/batch/base_policy.rb +7 -6
- data/lib/eco/api/session/batch/errors.rb +28 -4
- data/lib/eco/api/session/batch/feedback.rb +7 -1
- data/lib/eco/api/session/batch/job.rb +40 -23
- data/lib/eco/api/session/batch/jobs.rb +9 -4
- data/lib/eco/api/session/batch/jobs_groups.rb +1 -1
- data/lib/eco/api/session/batch/request_stats.rb +95 -58
- data/lib/eco/api/session/batch/status.rb +35 -31
- data/lib/eco/api/session/config.rb +104 -42
- data/lib/eco/api/session/config/api.rb +17 -6
- data/lib/eco/api/session/config/logger.rb +2 -2
- data/lib/eco/api/session/config/post_launch.rb +1 -1
- data/lib/eco/api/session/config/workflow.rb +8 -7
- data/lib/eco/api/usecases.rb +47 -33
- data/lib/eco/api/usecases/backup/append_usergroups_case.rb +36 -0
- data/lib/eco/api/usecases/backup/create_case.rb +104 -0
- data/lib/eco/api/usecases/backup/create_details_case.rb +31 -0
- data/lib/eco/api/usecases/backup/create_details_with_supervisor_case.rb +48 -0
- data/lib/eco/api/usecases/backup/hris_case.rb +124 -0
- data/lib/eco/api/usecases/backup/set_default_tag_case.rb +49 -0
- data/lib/eco/api/usecases/backup/set_supervisor_case.rb +41 -0
- data/lib/eco/api/usecases/backup/transfer_account_case.rb +90 -0
- data/lib/eco/api/usecases/backup/update_case.rb +112 -0
- data/lib/eco/api/usecases/backup/update_details_case.rb +64 -0
- data/lib/eco/api/usecases/backup/upsert_case.rb +114 -0
- data/lib/eco/api/usecases/base_case.rb +2 -0
- data/lib/eco/api/usecases/base_io.rb +3 -3
- data/lib/eco/api/usecases/default_cases.rb +23 -53
- data/lib/eco/api/usecases/default_cases/append_usergroups_case.rb +10 -31
- data/lib/eco/api/usecases/default_cases/change_email_case.rb +23 -47
- data/lib/eco/api/usecases/default_cases/codes_to_tags_case.rb +56 -43
- data/lib/eco/api/usecases/default_cases/create_case.rb +15 -101
- data/lib/eco/api/usecases/default_cases/create_details_case.rb +11 -26
- data/lib/eco/api/usecases/default_cases/create_details_with_supervisor_case.rb +12 -43
- data/lib/eco/api/usecases/default_cases/delete_sync_case.rb +11 -0
- data/lib/eco/api/usecases/default_cases/delete_trans_case.rb +14 -0
- data/lib/eco/api/usecases/default_cases/email_as_id_case.rb +10 -21
- data/lib/eco/api/usecases/default_cases/hris_case.rb +23 -120
- data/lib/eco/api/usecases/default_cases/new_email_case.rb +10 -23
- data/lib/eco/api/usecases/default_cases/new_id_case.rb +11 -25
- data/lib/eco/api/usecases/default_cases/new_id_case0.rb +14 -0
- data/lib/eco/api/usecases/default_cases/org_data_convert_case.rb +101 -0
- data/lib/eco/api/usecases/default_cases/refresh_abilities_case.rb +30 -0
- data/lib/eco/api/usecases/default_cases/refresh_case.rb +7 -20
- data/lib/eco/api/usecases/default_cases/reinvite_sync_case.rb +11 -0
- data/lib/eco/api/usecases/default_cases/reinvite_trans_case.rb +17 -0
- data/lib/eco/api/usecases/default_cases/remove_account_sync_case.rb +11 -0
- data/lib/eco/api/usecases/default_cases/remove_account_trans_case.rb +17 -0
- data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +9 -19
- data/lib/eco/api/usecases/default_cases/restore_db_case.rb +104 -0
- data/lib/eco/api/usecases/default_cases/set_default_tag_case.rb +32 -40
- data/lib/eco/api/usecases/default_cases/set_supervisor_case.rb +15 -33
- data/lib/eco/api/usecases/default_cases/switch_supervisor_case.rb +66 -57
- data/lib/eco/api/usecases/default_cases/to_csv_case.rb +36 -44
- data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +40 -55
- data/lib/eco/api/usecases/default_cases/transfer_account_case.rb +264 -84
- data/lib/eco/api/usecases/default_cases/update_case.rb +15 -109
- data/lib/eco/api/usecases/default_cases/update_details_case.rb +14 -61
- data/lib/eco/api/usecases/default_cases/upsert_case.rb +16 -111
- data/lib/eco/api/usecases/use_case.rb +1 -1
- data/lib/eco/api/usecases/use_case_io.rb +9 -10
- data/lib/eco/cli/config.rb +10 -2
- data/lib/eco/cli/config/default.rb +2 -1
- data/lib/eco/cli/config/default/input_filters.rb +58 -0
- data/lib/eco/cli/config/default/options.rb +60 -25
- data/lib/eco/cli/config/default/people.rb +4 -4
- data/lib/eco/cli/config/default/people_filters.rb +108 -0
- data/lib/eco/cli/config/default/usecases.rb +89 -31
- data/lib/eco/cli/config/default/workflow.rb +37 -27
- data/lib/eco/cli/config/filters.rb +50 -0
- data/lib/eco/cli/config/filters/input_filters.rb +29 -0
- data/lib/eco/cli/config/filters/people_filters.rb +29 -0
- data/lib/eco/cli/config/help.rb +49 -0
- data/lib/eco/cli/config/options_set.rb +17 -1
- data/lib/eco/cli/config/use_cases.rb +79 -53
- data/lib/eco/cli/scripting.rb +10 -2
- data/lib/eco/cli/scripting/args_helpers.rb +25 -15
- data/lib/eco/cli/scripting/argument.rb +1 -0
- data/lib/eco/cli/scripting/arguments.rb +1 -1
- data/lib/eco/csv/table.rb +1 -1
- data/lib/eco/data/crypto/encryption.rb +3 -0
- data/lib/eco/language/match.rb +19 -9
- data/lib/eco/language/match_modifier.rb +13 -5
- data/lib/eco/language/models/collection.rb +77 -56
- data/lib/eco/language/models/parser_serializer.rb +39 -15
- data/lib/eco/version.rb +1 -1
- metadata +64 -18
- data/lib/eco/api/microcases/set_default_tag.rb +0 -23
- data/lib/eco/api/session/task.rb +0 -175
- data/lib/eco/api/usecases/default_case.rb +0 -19
- data/lib/eco/api/usecases/default_cases/delete_case.rb +0 -32
- data/lib/eco/api/usecases/default_cases/recover_db_case.rb +0 -99
- data/lib/eco/api/usecases/default_cases/refresh_presets_case.rb +0 -26
- data/lib/eco/api/usecases/default_cases/reinvite_case.rb +0 -41
- data/lib/eco/api/usecases/default_cases/remove_account_case.rb +0 -38
- data/lib/eco/api/usecases/microed_cases/hris_case.rb +0 -53
- data/lib/eco/api/usecases/microed_cases/update_case.rb +0 -33
- data/lib/eco/api/usecases/microed_cases/update_details_case.rb +0 -30
- data/lib/eco/api/usecases/microed_cases/upsert_case.rb +0 -36
- data/lib/eco/cli/config/default/filters.rb +0 -70
- data/lib/eco/cli/config/people_filters.rb +0 -38
@@ -4,7 +4,7 @@ ASSETS.cli.config do |config|
|
|
4
4
|
io = nil
|
5
5
|
# default rescue
|
6
6
|
wf.rescue do |exception, io|
|
7
|
-
io.session.logger.
|
7
|
+
io.session.logger.debug(exception.patch_full_message)
|
8
8
|
wf.run(:close, io: io)
|
9
9
|
io
|
10
10
|
end
|
@@ -14,38 +14,42 @@ ASSETS.cli.config do |config|
|
|
14
14
|
end
|
15
15
|
|
16
16
|
wf.for(:load) do |wf_load|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
wf_load.for(:input) do |wf_input|
|
18
|
+
wf_input.on(:get) do |wf_input_get, io|
|
19
|
+
cases_with_input = config.usecases.active(io: io).select do |usecase, data|
|
20
|
+
io.class.input_required?(usecase.type)
|
21
|
+
end
|
22
|
+
next io unless (!io.input || io.input.empty?) && !cases_with_input.empty?
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
+
if io.options.dig(:input, :entries_from)
|
25
|
+
io = io.new(input: config.input.get(io: io))
|
26
|
+
else
|
27
|
+
opt_case = cases_with_input.values.first[:option]
|
28
|
+
io = io.new(input: config.input.get(io: io, option: opt_case))
|
29
|
+
end
|
30
|
+
io
|
24
31
|
end
|
25
32
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
io = io.new(input: config.input.get(io: io))
|
30
|
-
else
|
31
|
-
opt_case = cases_with_input.values.first[:option]
|
32
|
-
io = io.new(input: config.input.get(io: io, option: opt_case))
|
33
|
+
wf_input.on(:filter) do |wf_input_filter, io|
|
34
|
+
next io unless io.input && !io.input.empty?
|
35
|
+
io = io.new(input: config.input_filters.process(io: io))
|
33
36
|
end
|
34
|
-
io
|
35
37
|
end
|
36
38
|
|
37
|
-
wf_load.
|
38
|
-
|
39
|
-
|
39
|
+
wf_load.for(:people) do |wf_people|
|
40
|
+
wf_people.on(:get) do |wf_people_get, io|
|
41
|
+
cases_with_people = config.usecases.active(io: io).select do |usecase, data|
|
42
|
+
io.class.people_required?(usecase.type)
|
43
|
+
end
|
44
|
+
get_people = io.options.dig(:people, :get, :from) == :remote
|
45
|
+
next io unless !cases_with_people.empty? || get_people
|
46
|
+
io = io.new(people: config.people(io: io))
|
40
47
|
end
|
41
|
-
get_people = io.options.dig(:people, :get, :from) == :remote
|
42
|
-
next io unless !cases_with_people.empty? || get_people
|
43
|
-
io = io.new(people: config.people(io: io))
|
44
|
-
end
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
+
wf_people.on(:filter) do |wf_people_filter, io|
|
50
|
+
next io unless io.people && !io.people.empty?
|
51
|
+
io = io.new(people: config.people_filters.process(io: io))
|
52
|
+
end
|
49
53
|
end
|
50
54
|
end
|
51
55
|
|
@@ -85,7 +89,7 @@ ASSETS.cli.config do |config|
|
|
85
89
|
partial_update = io.options.dig(:people, :get, :type) == :partial
|
86
90
|
if !io.options[:dry_run] && partial_update
|
87
91
|
# get target people afresh
|
88
|
-
people = io.session.
|
92
|
+
people = io.session.micro.people_refresh(people: io.people, include_created: true)
|
89
93
|
io = io.new(people: people)
|
90
94
|
else
|
91
95
|
wf_post.skip!
|
@@ -116,10 +120,16 @@ ASSETS.cli.config do |config|
|
|
116
120
|
end
|
117
121
|
end
|
118
122
|
|
123
|
+
wf.on(:report) do |wf_report, io|
|
124
|
+
#config.reports.active(io: io)
|
125
|
+
#io.session.reports
|
126
|
+
io
|
127
|
+
end
|
128
|
+
|
119
129
|
wf.on(:end) do |wf_end, io|
|
120
130
|
partial_update = io.options.dig(:people, :get, :type) == :partial
|
121
131
|
unless !io.options[:end_get] || io.options[:dry_run] || partial_update
|
122
|
-
people = io.session.
|
132
|
+
people = io.session.micro.people_cache
|
123
133
|
io = io.new(people: people)
|
124
134
|
end
|
125
135
|
io
|
@@ -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
|
-
|
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
|
-
@
|
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
|
17
|
-
|
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
|
-
@
|
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
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
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
|
-
|
90
|
-
|
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, all: true).merge(usecase: usecase)
|
98
|
+
Eco::API::UseCases::UseCaseIO.new(**params)
|
99
|
+
end
|
100
|
+
end
|
91
101
|
|
92
|
-
|
93
|
-
|
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
|
-
|
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
|