# rubocop:disable Metrics/BlockLength ASSETS.cli.config do |cnf| formats = { csv: { option: ["-csv"], extname: [".csv", ".txt"] }, xml: { option: ["-xml"], extname: [".xml"] }, xls: { option: ["-xls", "-xlsx", "-excel"], extname: [".xls", ".xlsx", ".xlsm"] }, 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, (frm, selectors)| used = selectors[:option].reduce(false) {|us, option| SCR.get_arg(option) || us} next matched if matched next frm if used end # File/Folder check file = File.expand_path(file) if File.directory?(file) folder = file file = Dir.glob("#{file}/*").reject {|f| File.directory?(f)} ext = (format && formats[format][:extname]) || [File.extname(file.first)] file = file.select {|f| ext.any? {|e| File.extname(f) == e}}.tap do |files| if files.empty? session.logger.error("Could not find any file with extension: #{ext} in folder '#{folder}'") exit(1) end end else ext = File.extname(file) end format ||= formats.reduce(nil) do |matched, (frm, selectors)| next matched if matched 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}}) encoding = options.dig(:input, :file, :encoding) case format when :xml [file].flatten.each {|f| session.config.files.validate(:xml, f)} kargs = {file: file, format: format} 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, 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