lib/rmega/cli.rb in rmega-0.2.0 vs lib/rmega/cli.rb in rmega-0.2.1

- old
+ new

@@ -1,14 +1,14 @@ require 'optparse' require 'io/console' -require 'active_support/core_ext/hash' +require 'yaml' module Rmega module CLI module Helpers def cli_options - $cli_options ||= {options: {}} + $cli_options ||= {} end def cli_prompt_password print("Enter password: ") password = STDIN.noecho(&:gets) @@ -16,105 +16,100 @@ puts return password end - def scan_mega_urls(text) - text.to_s.scan(Nodes::Factory::URL_REGEXP).flatten.map { |s| "https://mega.co.nz/##{s}" } - end - def mega_url?(url) Nodes::Factory.url?(url) end def configuration_filepath File.expand_path('~/.rmega') end - def write_configuration_file - opts = {options: cli_options[:options]} - if cli_options[:user] - opts[:user] = cli_options[:user] - opts[:pass] = cli_options[: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 - def read_configuration_file - if File.exists?(configuration_filepath) - opts = JSON.parse(File.read(configuration_filepath)) - $cli_options = opts.deep_symbolize_keys.deep_merge(cli_options) - puts "Loaded configuration file #{configuration_filepath}" if cli_options[:debug] - end + return unless File.exists?(configuration_filepath) + cli_options = YAML.load_file(configuration_filepath) + cli_options.keys.each { |k| cli_options[k.to_sym] = cli_options.delete(k) } + puts "Loaded configuration file #{configuration_filepath}" if cli_options[:debug] rescue Exception => ex raise(ex) if cli_options[:debug] end def apply_cli_options - Rmega.logger.level = ::Logger::DEBUG if cli_options[:debug] - - cli_options[:options].each do |key, value| + cli_options.each do |key, value| Rmega.options.__send__("#{key}=", value) end + Rmega.logger.level = ::Logger::DEBUG if cli_options[:debug] + Rmega.options.show_progress = true end def apply_opt_parser_options(opts) opts.on("-t NUM", "--thread_pool_size", "Number of threads to use") { |n| - cli_options[:options][:thread_pool_size] = n.to_i + cli_options[:thread_pool_size] = n.to_i } opts.on("--proxy-addr ADDRESS", "Http proxy address") { |value| - cli_options[:options][:http_proxy_address] = value + cli_options[:http_proxy_address] = value } opts.on("--proxy-port PORT", "Http proxy port") { |value| - cli_options[:options][:http_proxy_port] = value.to_i + cli_options[:http_proxy_port] = value.to_i } opts.on("-u", "--user USER_EMAIL", "User email address") { |value| cli_options[:user] = value } opts.on("--pass [USER_PASSWORD]", "User password (if omitted will prompt for it)") { |value| cli_options[:pass] = value } - opts.on("--write-cfg", "Write a configuration file with the given options") { - cli_options[:write_cfg] = true - } - opts.on("--debug", "Debug mode") { cli_options[:debug] = true } opts.on("-v", "--version", "Print the version number") { puts Rmega::VERSION puts Rmega::HOMEPAGE - exit + exit!(0) } end - def humanize_bytes(*args) - Progress.humanize_bytes(*args) - end + def traverse_storage(node, path, opts = {}) + path.gsub!(/^\/|\/$/, "") + curr_part = path.split("/")[0] || "" + last_part = (path.split("/")[1..-1] || []).join("/") - def rescue_errors_and_inerrupt(&block) - if cli_options[:write_cfg] - write_configuration_file + if curr_part.empty? + if node.type == :root or node.type == :folder + return node + else + return nil + end else - read_configuration_file - apply_cli_options - yield + n = node.folders.find { |n| n.name.casecmp(curr_part).zero? } + n ||= node.files.find { |n| n.name.casecmp(curr_part).zero? } unless opts[:only_folders] + + if last_part.empty? + return n + else + return traverse_storage(n, last_part) + end end + end + + def cli_rescue + read_configuration_file + apply_cli_options + yield rescue Interrupt puts "\nInterrupted" rescue Exception => ex if cli_options[:debug] raise(ex) else - puts "\nError: #{ex.message}" + $stderr.puts "\nERROR: #{ex.message}" end end end end end