eco-helpers 2.0.16 → 2.0.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +125 -6
  3. data/eco-helpers.gemspec +10 -5
  4. data/lib/eco-helpers.rb +2 -0
  5. data/lib/eco/api/common/base_loader.rb +18 -0
  6. data/lib/eco/api/common/loaders/parser.rb +1 -0
  7. data/lib/eco/api/common/people/default_parsers.rb +1 -0
  8. data/lib/eco/api/common/people/default_parsers/date_parser.rb +11 -1
  9. data/lib/eco/api/common/people/default_parsers/login_providers_parser.rb +1 -1
  10. data/lib/eco/api/common/people/default_parsers/policy_groups_parser.rb +11 -11
  11. data/lib/eco/api/common/people/default_parsers/xls_parser.rb +53 -0
  12. data/lib/eco/api/common/people/entries.rb +1 -0
  13. data/lib/eco/api/common/people/entry_factory.rb +88 -23
  14. data/lib/eco/api/common/people/person_entry.rb +5 -2
  15. data/lib/eco/api/common/people/person_parser.rb +1 -1
  16. data/lib/eco/api/common/session.rb +1 -0
  17. data/lib/eco/api/common/session/base_session.rb +2 -0
  18. data/lib/eco/api/common/session/helpers.rb +30 -0
  19. data/lib/eco/api/common/session/helpers/prompt_user.rb +34 -0
  20. data/lib/eco/api/common/session/mailer.rb +0 -1
  21. data/lib/eco/api/common/session/s3_uploader.rb +0 -1
  22. data/lib/eco/api/common/session/sftp.rb +0 -1
  23. data/lib/eco/api/common/version_patches/ecoportal_api/external_person.rb +1 -1
  24. data/lib/eco/api/common/version_patches/ecoportal_api/internal_person.rb +7 -4
  25. data/lib/eco/api/common/version_patches/exception.rb +11 -4
  26. data/lib/eco/api/microcases.rb +3 -1
  27. data/lib/eco/api/microcases/append_usergroups.rb +0 -1
  28. data/lib/eco/api/microcases/people_cache.rb +2 -2
  29. data/lib/eco/api/microcases/people_load.rb +2 -2
  30. data/lib/eco/api/microcases/people_refresh.rb +2 -2
  31. data/lib/eco/api/microcases/people_search.rb +6 -6
  32. data/lib/eco/api/microcases/preserve_default_tag.rb +23 -0
  33. data/lib/eco/api/microcases/preserve_filter_tags.rb +28 -0
  34. data/lib/eco/api/microcases/preserve_policy_groups.rb +30 -0
  35. data/lib/eco/api/microcases/set_account.rb +0 -1
  36. data/lib/eco/api/microcases/with_each.rb +67 -6
  37. data/lib/eco/api/microcases/with_each_present.rb +4 -2
  38. data/lib/eco/api/microcases/with_each_starter.rb +4 -2
  39. data/lib/eco/api/organization.rb +1 -0
  40. data/lib/eco/api/organization/people.rb +98 -22
  41. data/lib/eco/api/organization/people_similarity.rb +272 -0
  42. data/lib/eco/api/organization/person_schemas.rb +5 -1
  43. data/lib/eco/api/organization/policy_groups.rb +5 -1
  44. data/lib/eco/api/organization/presets_factory.rb +22 -83
  45. data/lib/eco/api/organization/presets_integrity.json +6 -0
  46. data/lib/eco/api/organization/presets_values.json +5 -4
  47. data/lib/eco/api/organization/tag_tree.rb +33 -0
  48. data/lib/eco/api/policies/default_policies/99_user_access_policy.rb +0 -30
  49. data/lib/eco/api/session.rb +20 -28
  50. data/lib/eco/api/session/batch.rb +25 -7
  51. data/lib/eco/api/session/config.rb +0 -10
  52. data/lib/eco/api/session/config/apis.rb +80 -14
  53. data/lib/eco/api/session/config/people.rb +1 -17
  54. data/lib/eco/api/usecases.rb +2 -2
  55. data/lib/eco/api/usecases/base_case.rb +2 -2
  56. data/lib/eco/api/usecases/base_io.rb +17 -4
  57. data/lib/eco/api/usecases/default_cases.rb +2 -1
  58. data/lib/eco/api/usecases/default_cases/abstract_policygroup_abilities_case.rb +4 -4
  59. data/lib/eco/api/usecases/default_cases/analyse_people_case.rb +223 -0
  60. data/lib/eco/api/usecases/default_cases/clean_unknown_tags_case.rb +37 -0
  61. data/lib/eco/api/usecases/default_cases/codes_to_tags_case.rb +2 -3
  62. data/lib/eco/api/usecases/default_cases/reset_landing_page_case.rb +11 -1
  63. data/lib/eco/api/usecases/default_cases/restore_db_case.rb +1 -2
  64. data/lib/eco/api/usecases/default_cases/supers_cyclic_identify_case.rb +1 -1
  65. data/lib/eco/api/usecases/default_cases/supers_hierarchy_case.rb +1 -1
  66. data/lib/eco/api/usecases/default_cases/to_csv_case.rb +132 -29
  67. data/lib/eco/api/usecases/default_cases/to_csv_detailed_case.rb +61 -36
  68. data/lib/eco/api/usecases/ooze_samples/ooze_update_case.rb +3 -2
  69. data/lib/eco/cli/config/default/input.rb +61 -8
  70. data/lib/eco/cli/config/default/options.rb +48 -17
  71. data/lib/eco/cli/config/default/people.rb +18 -24
  72. data/lib/eco/cli/config/default/people_filters.rb +3 -3
  73. data/lib/eco/cli/config/default/usecases.rb +97 -32
  74. data/lib/eco/cli/config/default/workflow.rb +22 -13
  75. data/lib/eco/cli/config/help.rb +1 -0
  76. data/lib/eco/cli/config/options_set.rb +106 -13
  77. data/lib/eco/cli/config/use_cases.rb +33 -33
  78. data/lib/eco/cli/scripting/args_helpers.rb +32 -5
  79. data/lib/eco/csv.rb +4 -2
  80. data/lib/eco/csv/table.rb +121 -21
  81. data/lib/eco/data.rb +1 -0
  82. data/lib/eco/data/crypto/encryption.rb +3 -3
  83. data/lib/eco/data/files/helpers.rb +6 -4
  84. data/lib/eco/data/fuzzy_match.rb +201 -0
  85. data/lib/eco/data/fuzzy_match/array_helpers.rb +75 -0
  86. data/lib/eco/data/fuzzy_match/chars_position_score.rb +38 -0
  87. data/lib/eco/data/fuzzy_match/ngrams_score.rb +82 -0
  88. data/lib/eco/data/fuzzy_match/pairing.rb +95 -0
  89. data/lib/eco/data/fuzzy_match/result.rb +87 -0
  90. data/lib/eco/data/fuzzy_match/results.rb +77 -0
  91. data/lib/eco/data/fuzzy_match/score.rb +49 -0
  92. data/lib/eco/data/fuzzy_match/stop_words.rb +35 -0
  93. data/lib/eco/data/fuzzy_match/string_helpers.rb +82 -0
  94. data/lib/eco/version.rb +1 -1
  95. metadata +147 -11
  96. data/lib/eco/api/microcases/refresh_abilities.rb +0 -19
  97. data/lib/eco/api/organization/presets_reference.json +0 -59
  98. data/lib/eco/api/usecases/default_cases/refresh_abilities_case.rb +0 -30
@@ -0,0 +1,37 @@
1
+ class Eco::API::UseCases::DefaultCases::CleanUnknownTags < Eco::API::Common::Loaders::UseCase
2
+ name "clean-unknown-tags"
3
+ type :transform
4
+
5
+ REGISTER_TAGS = [
6
+ "EVENT", "INJURY", "RISK", "CONTRACTOR", "PERMIT",
7
+ "AUDIT", "JSEA",
8
+ "TRAINING", "INDUCTION",
9
+ "MEETING", "PPE", "CHEMICAL",
10
+ "PLANT", "ASSET",
11
+ "POLICY", "IDEA", "REPORTS"
12
+ ]
13
+
14
+ attr_reader :session, :options
15
+
16
+ def main(people, session, options, usecase)
17
+ @session = session; @options = options
18
+
19
+ update = session.new_job("main", "update", :update, usecase)
20
+ people.each do |person|
21
+ unknown_tags = person.filter_tags.select {|tag| !tag?(tag)}
22
+ person.filter_tags -= unknown_tags
23
+ update.add(person)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def tag?(value)
30
+ tagtree.tag?(value) || REGISTER_TAGS.any? {|reg| value == reg}
31
+ end
32
+
33
+ def tagtree
34
+ @tagtree ||= ASSETS.config.tagtree
35
+ end
36
+
37
+ end
@@ -16,10 +16,9 @@ class Eco::API::UseCases::DefaultCases::CodesToTagsCase < Eco::API::Common::Load
16
16
  end
17
17
 
18
18
  def main(session, options, usecase)
19
- @session = session
20
- @options = options
19
+ options[:end_get] = false
20
+ @session = session; @options = options
21
21
  codes_to_tags
22
- exit
23
22
  end
24
23
 
25
24
  private
@@ -2,13 +2,23 @@ class Eco::API::UseCases::DefaultCases::ResetLandingPageCase < Eco::API::Common:
2
2
  name "reset-landing-page"
3
3
  type :transform
4
4
 
5
+ attr_reader :session, :options
6
+
5
7
  def main(people, session, options, usecase)
8
+ @session = session; @options = options
6
9
  update = session.new_job("main", "update", :update, usecase, :core)
7
10
 
8
11
  people.users.each do |user|
9
- user.account.landing_page_id = nil
12
+ user.account.landing_page_id = page_id
10
13
  update.add(user)
11
14
  end
12
15
  end
13
16
 
17
+ private
18
+
19
+ def page_id
20
+ options[:page_id]
21
+ end
22
+
23
+
14
24
  end
@@ -59,7 +59,6 @@ class Eco::API::UseCases::DefaultCases::RestoreDBCase < Eco::API::Common::Loader
59
59
  dst.policy_group_ids = src.policy_group_ids unless options.dig(:exclude, :policy_groups)
60
60
 
61
61
  unless options.dig(:exclude, :abilities)
62
- dst.permissions_preset = src.permissions_preset
63
62
  dst.permissions_custom = src.permissions_custom
64
63
  end
65
64
 
@@ -75,7 +74,7 @@ class Eco::API::UseCases::DefaultCases::RestoreDBCase < Eco::API::Common::Loader
75
74
  dst.starred_ids = src.starred_ids
76
75
  dst.landing_page_id = src.landing_page_id
77
76
  end
78
-
77
+
79
78
  dst&.send_invites = options[:send_invites] if options.key?(:send_invites)
80
79
  end
81
80
  end
@@ -5,10 +5,10 @@ class Eco::API::UseCases::DefaultCases::SupersCyclicIdentify < Eco::API::Common:
5
5
  attr_reader :session, :people, :options
6
6
 
7
7
  def main(people, session, options, usecase)
8
+ options[:end_get] = false
8
9
  @session = session; @options = options; @people = people
9
10
 
10
11
  save!(cyclic_sets)
11
- exit(0)
12
12
  end
13
13
 
14
14
  private
@@ -5,10 +5,10 @@ class Eco::API::UseCases::DefaultCases::SupersHierarchy < Eco::API::Common::Load
5
5
  attr_reader :session, :people, :options
6
6
 
7
7
  def main(people, session, options, usecase)
8
+ options[:end_get] = false
8
9
  @session = session; @options = options; @people = people
9
10
 
10
11
  save!(hierarchy)
11
- exit(0)
12
12
  end
13
13
 
14
14
  private
@@ -5,6 +5,7 @@ class Eco::API::UseCases::DefaultCases::ToCsvCase < Eco::API::Common::Loaders::U
5
5
  attr_reader :session, :options, :people
6
6
 
7
7
  def main(people, session, options, usecase)
8
+ options[:end_get] = false
8
9
  @session = session; @options = options; @people = people
9
10
 
10
11
  unless people && !people.empty?
@@ -12,25 +13,62 @@ class Eco::API::UseCases::DefaultCases::ToCsvCase < Eco::API::Common::Loaders::U
12
13
  return false
13
14
  end
14
15
 
15
- unless file = options[:file] || options.dig(:export, :file, :name)
16
- session.logger.error("Destination file not specified")
17
- return false
16
+ if options.dig(:export, :options, :split_schemas)
17
+ by_schema.each do |id, people|
18
+ sch_name = schemas.to_name(id)
19
+ prefix = sch_name ? sch_name.gsub(" ", "_") : "No_Schema"
20
+ create_file!("#{prefix}_#{file}", people)
21
+ end
22
+ else
23
+ create_file!(file, people)
18
24
  end
25
+ end
26
+
27
+ private
19
28
 
20
- session.logger.info("going to create file: #{file}")
29
+ def create_file!(filename = file, data = people)
30
+ session.logger.info("going to create file: #{filename}")
21
31
 
22
- CSV.open(file, "w") do |csv|
23
- csv << spot_header
24
- people.each do |person|
25
- csv << target_entry_type(person).values
32
+ CSV.open(filename, "w") do |csv|
33
+ csv << spot_header(data.first)
34
+ data.each do |person|
35
+ csv << to_row(person)
26
36
  end
27
37
  end
28
- exit(0)
29
38
  end
30
39
 
31
- private
40
+ def to_row(person)
41
+ entry = to_entry_type(person)
42
+ entry.values_at(*keys(entry)).tap do |row|
43
+ row << schemas.to_name(person.details&.schema_id) || "No Schema"
44
+ end
45
+ end
46
+
47
+ def spot_header(person = people.first)
48
+ header = keys(to_entry_type(person))
49
+ header << "Schema"
50
+ header = yield(header) if block_given?
51
+ header = nice_header_names(header, schema: schema(person.details)) if nice_header_names?
52
+ header
53
+ end
54
+
55
+ def keys(entry)
56
+ entry.keys - ["freemium", "send_invites"]
57
+ end
58
+
59
+ def nice_header_names?
60
+ options.dig(:nice_header) || options.dig(:export, :options, :nice_header)
61
+ end
32
62
 
33
- def target_entry_type(person)
63
+ def nice_header_names(header, schema: nil)
64
+ schema ||= session.schema
65
+ name_maps = schema.fields_by_alt_id.each_with_object({}) do |(alt_id, fld), mappings|
66
+ mappings[alt_id] = fld.name
67
+ end.merge(nice_header_maps)
68
+ header.map {|name| name_maps[name] ? name_maps[name] : name}
69
+ end
70
+
71
+ def to_entry_type(person)
34
72
  session.new_entry(person, dependencies: deps).yield_self do |person_entry|
35
73
  options.dig(:export, :options, :internal_names) ? person_entry.mapped_entry : person_entry.external_entry
36
74
  end
@@ -40,26 +78,91 @@ class Eco::API::UseCases::DefaultCases::ToCsvCase < Eco::API::Common::Loaders::U
40
78
  @deps ||= {"supervisor_id" => {people: people}}
41
79
  end
42
80
 
43
- def spot_header
44
- entry = target_entry_type(people.first)
45
- header = entry.keys
46
-
47
- if options.dig(:nice_header) || options.dig(:export, :options, :nice_header)
48
- name_maps = session.schema.fields_by_alt_id.transform_values do |fld|
49
- fld.name
50
- end.merge({
51
- "policy_group_ids" => "User Group(s)",
52
- "email" => "Email",
53
- "name" => "Name",
54
- "supervisor_id" => "Manager ID",
55
- "filter_tags" => "Locations",
56
- "default_tag" => "Default Location",
57
- "id" => "ecoPortal ID"
58
- })
59
- header = header.map {|name| name_maps[name] ? name_maps[name] : name}
81
+ def file
82
+ @file ||= (options[:file] || options.dig(:export, :file, :name)).tap do |filename|
83
+ unless filename
84
+ session.logger.error("Destination file not specified")
85
+ return false
86
+ end
87
+ end
88
+ end
89
+
90
+ def by_schema
91
+ people.group_by do |person|
92
+ if details = person.details
93
+ details.schema_id
94
+ end
95
+ end.transform_values do |persons|
96
+ people.newFrom persons
97
+ end
98
+ end
99
+
100
+ def schema(value)
101
+ case value
102
+ when Ecoportal::API::V1::Person
103
+ schema(value.details&.schema_id)
104
+ when String
105
+ schemas[value]
106
+ when Ecoportal::API::V1::PersonDetails
107
+ schema(value.schema_id)
108
+ when Ecoportal::API::V1::PersonSchema
109
+ value
60
110
  end
61
- header
62
111
  end
63
112
 
113
+ def schemas
114
+ session.schemas
115
+ end
116
+
117
+ def nice_header_maps
118
+ @nice_header_maps ||= {
119
+ "policy_group_ids" => "User Group(s)",
120
+ "email" => "Email",
121
+ "name" => "Name",
122
+ "supervisor_id" => "Manager ID",
123
+ "filter_tags" => "Locations",
124
+ "default_tag" => "Default Location",
125
+ "id" => "ecoPortal ID",
126
+ "external_id" => "Reference ID (ext_id)",
127
+ "login_provider_ids" => "Login Methods",
128
+ "landing_page_id" => "Landing Page ID",
129
+ "show_sidebar" => "(pref) Sidebar Open?",
130
+ "show_shortcuts" => "(pref) Link to Registers?",
131
+ "show_coming_soon" => "(pref) Coming Soon List?",
132
+ "show_recently_visited_forms" => "(pref) Recently Visited Forms List?",
133
+ "show_tasks" => "(pref) Tasks List?",
134
+ "show_task_bubbles" => "(pref) Task Count Bubbles",
135
+ "kiosk_enabled" => "Kiosk User?",
136
+ "freemium" => "Freemium User?",
137
+ "files" => "(able) on Files",
138
+ "reports" => "(able) on Report Structures",
139
+ "data" => "(able) on Data (hours, datasets)",
140
+ "organization" => "(able) on Organization Config",
141
+ "pages" => "(able) on Page/Entries",
142
+ "page_editor" => "(able) page Editor Level",
143
+ "registers" => "(able) on Registers",
144
+ "tasks" => "(able) on Tasks",
145
+ "person_core" => "(able) on People",
146
+ "person_core_create" => "(able) Create People?",
147
+ "person_core_edit" => "(able) Edit People?",
148
+ "person_details" => "(able) on People Schema Details",
149
+ "person_account" => "(able) on Users",
150
+ "person_abilities" => "(able) on Users' Abilities",
151
+ "custom_files" => "(min) on Files",
152
+ "custom_reports" => "(min) on Report Structures",
153
+ "custom_data" => "(min) on Data (hours, datasets)",
154
+ "custom_organization" => "(min) on Organization Config",
155
+ "custom_pages" => "(min) on Page/Entries",
156
+ "custom_page_editor" => "(min) page Editor Level",
157
+ "custom_registers" => "(min) on Registers",
158
+ "custom_tasks" => "(min) on Tasks",
159
+ "custom_person_core" => "(min) on People",
160
+ "custom_person_core_create" => "(min) Create People?",
161
+ "custom_person_core_edit" => "(min) Edit People?",
162
+ "custom_person_details" => "(min) on People Schema Details",
163
+ "custom_person_account" => "(min) on Users",
164
+ "custom_person_abilities" => "(min) on Users' Abilities"
165
+ }
166
+ end
64
167
 
65
168
  end
@@ -1,53 +1,78 @@
1
- class Eco::API::UseCases::DefaultCases::ToCsvDetailedCase < Eco::API::Common::Loaders::UseCase
1
+ class Eco::API::UseCases::DefaultCases::ToCsvDetailedCase < Eco::API::UseCases::DefaultCases::ToCsvCase
2
2
  name "to-csv-detailed"
3
3
  type :export
4
4
 
5
- def main(people, session, options, usecase)
6
- unless people && !people.empty?
7
- session.logger.warn("No source people to create the file... aborting!")
8
- return false
9
- end
5
+ private
6
+
7
+ def to_row(person)
8
+ data = super(person)
9
+ data << person.subordinates
10
+ data << person_supervisor(person)
11
+ data += user_abilities(person)
12
+ data += user_permissions_custom(person) if options.dig(:export, :options, :permissions_custom)
13
+ account = person.account
14
+ data << account&.landing_page_id
15
+ data += user_preferences(person)
16
+ data
17
+ end
10
18
 
11
- unless file = options[:file] || options.dig(:export, :file, :name)
12
- session.logger.error("Destination file not specified")
13
- return false
19
+ def person_supervisor(person)
20
+ session.micro.with_supervisor(person.supervisor_id, people) do |supervisor|
21
+ return supervisor ? supervisor.name : ""
14
22
  end
23
+ end
15
24
 
16
- login_providers = session.login_providers
17
- abilities = session.presets_factory.keys
25
+ def user_abilities(person)
26
+ account_abilities = (person.account && person.account.permissions_merged) || {}
27
+ abilities.map {|key| account_abilities[key] || "no access"}
28
+ end
18
29
 
19
- session.logger.info("going to create file: #{file}")
20
- CSV.open(file, "w") do |csv|
21
- deps = {"supervisor_id" => {people: people}}
30
+ def user_permissions_custom(person)
31
+ account_abilities = (person.account && person.account.permissions_custom) || {}
32
+ abilities.map {|key| account_abilities[key] || "no access"}
33
+ end
22
34
 
23
- person_model = people.first
24
- person_entry = session.new_entry(person_model, dependencies: deps)
25
- header = person_entry.external_entry.keys
26
- header += ["Subordinates"]
27
- header += ["Supervisor Name"]
28
- header += abilities
29
- header += ["Landing Page"]
35
+ def user_preferences(person)
36
+ user_preferences = (person.account && person.account.preferences&.doc) || {}
37
+ preferences.map {|key| user_preferences[key] || false}
38
+ end
30
39
 
31
- csv << header
32
- people.each do |person|
33
- data = session.new_entry(person, dependencies: deps).external_entry.values
40
+ def spot_header(person = people.first)
41
+ super(person) do |header|
42
+ header << "Subordinates"
43
+ header << "Supervisor Name"
44
+ header += abilities_header
45
+ header += abilities_header("custom") if options.dig(:export, :options, :permissions_custom)
46
+ header << "Landing Page"
47
+ header += preferences
48
+ header
49
+ end
50
+ end
34
51
 
35
- data.push(person.subordinates)
36
- session.micro.with_supervisor(person.supervisor_id, people) do |supervisor|
37
- data.push supervisor ? supervisor.name : ""
38
- end
52
+ def keys(entry)
53
+ super(entry) + ["freemium"]
54
+ end
39
55
 
40
- person_abilities = (person.account && person.account.permissions_custom) || {}
41
- data += abilities.map {|key| person_abilities[key] || "no access"}
56
+ def login_providers
57
+ session.login_providers
58
+ end
42
59
 
43
- if person.account && landing_id = person.account.landing_page_id
44
- data.push(landing_id)
45
- end
60
+ def abilities
61
+ @abilities ||= session.presets_factory.keys
62
+ end
46
63
 
47
- csv << data
48
- end
64
+ def abilities_header(prefix = nil)
65
+ abilities.map do |key|
66
+ prefix ? "#{prefix}_#{key}" : key
49
67
  end
50
- exit(0)
68
+ end
69
+
70
+ def preferences
71
+ @preferences ||= [
72
+ "show_sidebar", "show_shortcuts", "show_coming_soon", "show_recently_visited_forms",
73
+ "show_tasks", "show_task_bubbles",
74
+ "kiosk_enabled"
75
+ ]
51
76
  end
52
77
 
53
78
  end
@@ -124,8 +124,9 @@ class Eco::API::UseCases::OozeSamples::OozeUpdateCase < Eco::API::Common::Loader
124
124
  end
125
125
 
126
126
  def prompt_to_confirm!
127
- print "\nDo you want to proceed (y/N)? "
128
- exit(1) unless $stdin.gets.chomp.to_s.downcase == "y"
127
+ prompt_user("Do you want to proceed (y/N)?", default: "Y") do |response|
128
+ exit(1) unless response.upcase.start_with?("Y")
129
+ end
129
130
  end
130
131
 
131
132
  end
@@ -1,18 +1,71 @@
1
1
  ASSETS.cli.config do |cnf|
2
+ formats = {
3
+ csv: {
4
+ option: ["-csv"],
5
+ extname: [".csv", ".txt"]
6
+ },
7
+ xml: {
8
+ option: ["-xml"],
9
+ extname: [".xml"]
10
+ },
11
+ xls: {
12
+ option: ["-xls", "-xlsx", "-excel"],
13
+ extname: [".xls", ".xlsx", ".xlsm"]
14
+ },
15
+ json: {
16
+ option: ["-json"],
17
+ extname: [".json"]
18
+ }
19
+ }
20
+
2
21
  cnf.input(default_option: "-entries-from") do |session, str_opt, options|
3
22
  input = []
4
23
  if SCR.get_arg(str_opt)
5
24
  file = SCR.get_file(str_opt, required: true)
25
+
26
+ # Command line check
27
+ format = formats.reduce(nil) do |matched, (format, selectors)|
28
+ used = selectors[:option].reduce(false) {|used, option| SCR.get_arg(option) || used}
29
+ next matched if matched
30
+ next format if used
31
+ end
32
+
33
+ # File/Folder check
34
+ file = File.expand_path(file)
35
+ if File.directory?(file)
36
+ folder = file
37
+ file = Dir.glob("#{file}/*").reject {|f| File.directory?(f)}
38
+ ext = (format && formats[format][:extname]) || [File.extname(file.first)]
39
+ file = file.select {|f| ext.any? {|e| File.extname(f) == e}}.tap do |files|
40
+ if files.empty?
41
+ session.logger.error("Could not find any file with extension: #{ext} in folder '#{folder}'")
42
+ exit(1)
43
+ end
44
+ end
45
+ else
46
+ ext = File.extname(file)
47
+ end
48
+
49
+ format ||= formats.reduce(nil) do |matched, (format, selectors)|
50
+ next matched if matched
51
+ next format if selectors[:extname].any? {|e| ext == e}
52
+ end
53
+ format ||= :csv
54
+
6
55
  options.deep_merge!(input: {file: {name: file}})
7
- if SCR.get_arg("-xml")
8
- options.deep_merge!(input: {file: {format: :xml}})
9
- session.config.files.validate(:xml, file)
10
- input = session.entries(file: file, format: :xml)
11
- elsif SCR.get_arg("-json")
12
- options.deep_merge!(input: {file: {format: :json}})
13
- input = Eco::API::Organization::People.new(JSON.parse(File.read(file)))
56
+ options.deep_merge!(input: {file: {format: format}})
57
+
58
+ case format
59
+ when :xml
60
+ [file].flatten.each {|f| session.config.files.validate(:xml, f)}
61
+ input = session.entries(file: file, format: format)
62
+ when :xls
63
+ input = session.entries(file: file, format: format)
64
+ when :json
65
+ input = [file].flatten.reduce(Eco::API::Organization::People.new([])) do |people, file|
66
+ people.merge(JSON.parse(File.read(file)))
67
+ end
14
68
  else
15
- options.deep_merge!(input: {file: {format: :csv}})
16
69
  input = session.csv_entries(file)
17
70
  end
18
71
  end