Module: Rmega::CLI::Helpers
- Defined in:
- lib/rmega/cli.rb
Instance Method Summary collapse
- #apply_cli_options ⇒ Object
- #apply_opt_parser_options(opts) ⇒ Object
- #cli_options ⇒ Object
- #cli_prompt_password ⇒ Object
- #configuration_filepath ⇒ Object
- #humanize_bytes(*args) ⇒ Object
- #mega_url?(url) ⇒ Boolean
- #read_configuration_file ⇒ Object
- #rescue_errors_and_inerrupt(&block) ⇒ Object
- #scan_mega_urls(text) ⇒ Object
- #write_configuration_file ⇒ Object
Instance Method Details
#apply_cli_options ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/rmega/cli.rb', line 54 def Rmega.logger.level = ::Logger::DEBUG if [:debug] [:options].each do |key, value| Rmega..__send__("#{key}=", value) end end |
#apply_opt_parser_options(opts) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/rmega/cli.rb', line 62 def (opts) opts.on("-t NUM", "--thread_pool_size", "Number of threads to use") { |n| [:options][:thread_pool_size] = n.to_i } opts.on("--proxy-addr ADDRESS", "Http proxy address") { |value| [:options][:http_proxy_address] = value } opts.on("--proxy-port PORT", "Http proxy port") { |value| [:options][:http_proxy_port] = value.to_i } opts.on("-u", "--user USER_EMAIL", "User email address") { |value| [:user] = value } opts.on("--pass [USER_PASSWORD]", "User password (if omitted will prompt for it)") { |value| [:pass] = value } opts.on("--write-cfg", "Write a configuration file with the given options") { [:write_cfg] = true } opts.on("--debug", "Debug mode") { [:debug] = true } opts.on("-v", "--version", "Print the version number") { puts Rmega::VERSION puts Rmega::HOMEPAGE exit } end |
#cli_options ⇒ Object
8 9 10 |
# File 'lib/rmega/cli.rb', line 8 def $cli_options ||= {options: {}} end |
#cli_prompt_password ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/rmega/cli.rb', line 12 def cli_prompt_password print("Enter password: ") password = STDIN.noecho(&:gets) password = password[0..-2] if password.end_with?("\n") puts return password end |
#configuration_filepath ⇒ Object
29 30 31 |
# File 'lib/rmega/cli.rb', line 29 def configuration_filepath File.('~/.rmega') end |
#humanize_bytes(*args) ⇒ Object
98 99 100 |
# File 'lib/rmega/cli.rb', line 98 def humanize_bytes(*args) Progress.humanize_bytes(*args) end |
#mega_url?(url) ⇒ Boolean
25 26 27 |
# File 'lib/rmega/cli.rb', line 25 def mega_url?(url) Nodes::Factory.url?(url) end |
#read_configuration_file ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/rmega/cli.rb', line 44 def read_configuration_file if File.exists?(configuration_filepath) opts = JSON.parse(File.read(configuration_filepath)) $cli_options = opts.deep_symbolize_keys.deep_merge() puts "Loaded configuration file #{configuration_filepath}" if [:debug] end rescue Exception => ex raise(ex) if [:debug] end |
#rescue_errors_and_inerrupt(&block) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/rmega/cli.rb', line 102 def rescue_errors_and_inerrupt(&block) if [:write_cfg] write_configuration_file else read_configuration_file yield end rescue Interrupt puts "\nInterrupted" rescue Exception => ex if [:debug] raise(ex) else puts "\nError: #{ex.}" end end |
#scan_mega_urls(text) ⇒ Object
21 22 23 |
# File 'lib/rmega/cli.rb', line 21 def scan_mega_urls(text) text.to_s.scan(Nodes::Factory::URL_REGEXP).flatten.map { |s| "https://mega.co.nz/##{s}" } end |
#write_configuration_file ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rmega/cli.rb', line 33 def write_configuration_file opts = {options: [:options]} if [:user] opts[:user] = [:user] opts[:pass] = [:pass] || cli_prompt_password end File.open(configuration_filepath, 'wb') { |file| file.write(opts.to_json) } FileUtils.chmod(0600, configuration_filepath) puts "Options saved into #{configuration_filepath}" end |