class EcoRake module Lib module Files class Sftp < EcoRake::Lib::BaseTask FORWARD_RULES = { enviro: ->(enviro) { "-#{enviro}" }, folder: ->(folder) { "-local-folder #{folder}"}, list: '-list', get_last: '-get-last', get: '-get', archive: '-archive', file_pattern: ->(str) { "-file-pattern-const #{str}"}, remote_subfolder: ->(sub) { "-remote-subfolder #{sub}"} }.freeze options_with_defaults true option '-e', '--enviro ENVIRO', String, desc: 'Target environment to run against (i.e. org, live)', required: true option '-d', '--folder NAME', default: '.', desc: 'Source local folder' option '-l', '--list', TrueClass, desc: 'Lists available CSV feed files' option '-g', '--get', TrueClass, desc: 'Gets all available CSV feed files' option '-t', '--get-last', TrueClass, desc: 'Gets the last available CSV feed file' option '-a', '--archive', TrueClass, desc: "Moves the remote CSV feed files to the '/Archive' folder" option '-p', '--file-pattern CONST', String, desc: "Specifies a const to use as the target file pattern" option '-r', '--remote-subfolder SUBFOLDER', String, desc: "Remote subfolder of the env (at the top of the enviro folder)" option_forwarding(**FORWARD_RULES) # attr_const :target_enviro, required: true # option_reopen :enviro, default_lookup: :target_enviro def task(*_args) sh_exit_on_fail sftp_command end private def sftp_command cmd = [base_command] cmd << forward_option(:folder) cmd << forward_options(:list, :get_last, :get, :archive).compact.first || '-list' cmd.push(*forward_options(:remote_subfolder, :file_pattern)) cmd << '-no-people' cmd = yield(cmd) if block_given? string_cmd(*cmd) end def base_command string_cmd(ruby_runner, forward_option(:enviro), '-sftp') end end end end end