Module: Rmega::CLI::Helpers

Defined in:
lib/rmega/cli.rb

Instance Method Summary collapse

Instance Method Details

#apply_cli_optionsObject



54
55
56
57
58
59
60
# File 'lib/rmega/cli.rb', line 54

def apply_cli_options
  Rmega.logger.level = ::Logger::DEBUG if cli_options[:debug]

  cli_options[:options].each do |key, value|
    Rmega.options.__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 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
  }

  opts.on("--proxy-addr ADDRESS", "Http proxy address") { |value|
    cli_options[:options][:http_proxy_address] = value
  }

  opts.on("--proxy-port PORT", "Http proxy port") { |value|
    cli_options[: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
  }
end

#cli_optionsObject



8
9
10
# File 'lib/rmega/cli.rb', line 8

def cli_options
  $cli_options ||= {options: {}}
end

#cli_prompt_passwordObject



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_filepathObject



29
30
31
# File 'lib/rmega/cli.rb', line 29

def configuration_filepath
  File.expand_path('~/.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

Returns:

  • (Boolean)


25
26
27
# File 'lib/rmega/cli.rb', line 25

def mega_url?(url)
  Nodes::Factory.url?(url)
end

#read_configuration_fileObject



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(cli_options)
    puts "Loaded configuration file #{configuration_filepath}" if cli_options[:debug]
  end
rescue Exception => ex
  raise(ex) if cli_options[: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 cli_options[:write_cfg]
    write_configuration_file
  else
    read_configuration_file
    apply_cli_options
    yield
  end
rescue Interrupt
  puts "\nInterrupted"
rescue Exception => ex
  if cli_options[:debug]
    raise(ex)
  else
    puts "\nError: #{ex.message}"
  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_fileObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/rmega/cli.rb', line 33

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