lib/eco/cli_default/input.rb in eco-helpers-2.7.24 vs lib/eco/cli_default/input.rb in eco-helpers-2.7.25

- old
+ new

@@ -1,35 +1,36 @@ +# rubocop:disable Metrics/BlockLength ASSETS.cli.config do |cnf| formats = { - csv: { - option: ["-csv"], + csv: { + option: ["-csv"], extname: [".csv", ".txt"] }, - xml: { - option: ["-xml"], + xml: { + option: ["-xml"], extname: [".xml"] }, xls: { - option: ["-xls", "-xlsx", "-excel"], + option: ["-xls", "-xlsx", "-excel"], extname: [".xls", ".xlsx", ".xlsm"] }, json: { - option: ["-json"], + option: ["-json"], extname: [".json"] } } cnf.input(default_option: "-entries-from") do |session, str_opt, options| input = [] if SCR.get_arg(str_opt) file = SCR.get_file(str_opt, required: true) # Command line check - format = formats.reduce(nil) do |matched, (format, selectors)| - used = selectors[:option].reduce(false) {|used, option| SCR.get_arg(option) || used} + format = formats.reduce(nil) do |matched, (frm, selectors)| + used = selectors[:option].reduce(false) {|us, option| SCR.get_arg(option) || us} next matched if matched - next format if used + next frm if used end # File/Folder check file = File.expand_path(file) if File.directory?(file) @@ -41,16 +42,16 @@ session.logger.error("Could not find any file with extension: #{ext} in folder '#{folder}'") exit(1) end end else - ext = File.extname(file) + ext = File.extname(file) end - format ||= formats.reduce(nil) do |matched, (format, selectors)| + format ||= formats.reduce(nil) do |matched, (frm, selectors)| next matched if matched - next format if selectors[:extname].any? {|e| ext == e} + next frm if selectors[:extname].any? {|e| ext == e} end format ||= :csv options.deep_merge!(input: {file: {name: file}}) options.deep_merge!(input: {file: {format: format}}) @@ -64,17 +65,19 @@ kargs.merge!(encoding: encoding) if encoding input = session.entries(**kargs) when :xls input = session.entries(file: file, format: format) when :json - input = [file].flatten.reduce(Eco::API::Organization::People.new([])) do |people, file| - people.merge(JSON.parse(File.read(file))) + input = [file].flatten.reduce(Eco::API::Organization::People.new([])) do |people, filename| + people.merge(JSON.parse(File.read(filename))) end else # :csv kargs = {check_headers: true} kargs.merge!(encoding: encoding) if encoding input = session.csv_entries(file, **kargs) end end input end end + +# rubocop:enable Metrics/BlockLength