Class: Lhj::Command::OSS::Download
Instance Method Summary
collapse
#auto_spin, #begin_title, #run, #stop
Constructor Details
#initialize(argv) ⇒ Download
Returns a new instance of Download.
19
20
21
22
23
|
# File 'lib/lhj/command/oss/download.rb', line 19
def initialize(argv)
@current_path = argv.shift_argument || Dir.pwd
@cli = HighLine.new
super
end
|
Instance Method Details
#download_file(url, file) ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/lhj/command/oss/download.rb', line 86
def download_file(url, file)
http_client = Faraday.new do |c|
c.adapter Faraday.default_adapter
end
response = http_client.get(url)
File.open(file, 'wb') { |fp| fp.write(response.body) }
end
|
#download_with_key(oss_key) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/lhj/command/oss/download.rb', line 73
def download_with_key(oss_key)
puts "下载的key为:#{oss_key}".yellow
file_key = oss_key.split(/\?/)[0].split('/').last
file = File.join(@current_path, file_key)
File.rename(file_key, "#{file_key}.bak") if File.exist?(file_key)
if %r{^https://} =~ oss_key || %r{^http://} =~ oss_key
download_file(oss_key, file)
else
Lhj::OSS::Helper.instance.down_load(oss_key, file)
end
puts "下载文件到目录: #{file}\n".yellow
end
|
#handle ⇒ Object
25
26
27
28
29
30
31
32
33
|
# File 'lib/lhj/command/oss/download.rb', line 25
def handle
type = @cli.choose do |m|
m.prompt = '选择下载方式?'
m.choice(:remote)
m.choice(:local)
end
handle_with_local if type == :local
handle_with_remote if type == :remote
end
|
#handle_with_local ⇒ Object
35
36
37
38
39
40
41
|
# File 'lib/lhj/command/oss/download.rb', line 35
def handle_with_local
@cli.say(HighLine.color('选择本地yaml文件:', :green))
path = File.join(@current_path, '**', '*.yml')
yml_file = @cli.choose(*Dir.glob(path))
oss_keys = YAML.load_file(yml_file) if File.exist?(yml_file)
handle_with_oss_keys(oss_keys)
end
|
#handle_with_oss_keys(oss_keys) ⇒ Object
48
49
50
51
52
|
# File 'lib/lhj/command/oss/download.rb', line 48
def handle_with_oss_keys(oss_keys)
Dir.chdir(@current_path) do
oss_keys.each { |k| download_with_key(k) }
end
end
|
#handle_with_remote ⇒ Object
43
44
45
46
|
# File 'lib/lhj/command/oss/download.rb', line 43
def handle_with_remote
oss_keys = remote_oss_keys
handle_with_oss_keys(oss_keys)
end
|
#remote_oss_keys ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/lhj/command/oss/download.rb', line 54
def remote_oss_keys
obj_keys = request_oss_keys
idx = @cli.ask('请选择下载的序号: '.green)
idx_arr = idx.split(',').map(&:strip).filter { |v| v.length.positive? }
result_keys = []
idx_arr.each do |i|
ma = /(?<begin>[0-9]*)\D*-\D*(?<end>[0-9]*)/.match(i)
result_keys << obj_keys[ma[:begin].to_i..ma[:end].to_i] if ma
result_keys << obj_keys[i.to_i] unless ma
end
result_keys.flatten
end
|
#request_oss_keys ⇒ Object
67
68
69
70
71
|
# File 'lib/lhj/command/oss/download.rb', line 67
def request_oss_keys
objects = Lhj::OSS::Helper.instance.list
obj_keys = objects.map(&:key).sort { |a, b| a <=> b }
obj_keys.each_with_index { |k, i| puts "#{i}.#{k}".yellow }
end
|