Class: Lhj::Command::OSS::List
Class Method Summary
collapse
Instance Method Summary
collapse
#auto_spin, #begin_title, #run, #stop
Constructor Details
#initialize(argv) ⇒ List
Returns a new instance of List.
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/lhj/command/oss/list.rb', line 37
def initialize(argv)
@save = argv.flag?('save', false)
@clear = argv.flag?('clear', false)
@all_folder = argv.flag?('all_folder', false)
@prefix = argv.option('prefix')
@output_suffix = argv.option('output_suffix')
@marker = argv.option('marker')
@current_path = argv.shift_argument || Dir.pwd
@cli = HighLine.new
super
end
|
Class Method Details
.options ⇒ Object
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/lhj/command/oss/list.rb', line 26
def self.options
[
%w[--save 保存文件],
%w[--clear 清空控制台输出],
%w[--output_suffix 添加后缀输出控制台],
%w[--all_folder 列出所有的子目录],
%w[--prefix 前缀过滤],
%w[--marker 指定从该位置开始list]
]
end
|
Instance Method Details
#handle ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/lhj/command/oss/list.rb', line 49
def handle
list_opts = {}
if @save
time = Time.now.strftime('%Y%m%d_%H%M')
oss_key_file = @cli.ask(HighLine.color('生成OSS_KEY的名称:', :green), String) do |q|
q.default = "oss_key_#{time}.yml"
end
oss_url_file = @cli.ask(HighLine.color('生成OSS_URL的名称:', :green), String) do |q|
q.default = "oss_url_#{time}.yml"
end
end
list_opts[:prefix] = @prefix if @prefix
list_opts[:marker] = @marker if @marker
list_opts[:delimiter] = '/' if @all_folder
objects = Lhj::OSS::Helper.instance.list(list_opts)
obj_keys = objects.map(&:key).sort { |a, b| a <=> b } unless @all_folder
obj_keys = objects.filter { |o| o.is_a?(String) } if @all_folder
save(obj_keys, oss_key_file, oss_url_file) if @save
print(obj_keys) unless @clear
end
|
#print(obj_keys) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/lhj/command/oss/list.rb', line 79
def print(obj_keys)
rows = []
obj_keys.each_with_index do |k, i|
if @all_folder
path = k
else
path = "#{i}.#{Lhj::OSS::Helper.instance.url_path}/#{k}"
path = "#{path}?#{@output_suffix}" if @output_suffix
end
rows << [path]
end
table = Terminal::Table.new title: 'OSS List', headings: ['URL'], rows: rows
puts table
end
|
#save(obj_keys, key_file, url_file) ⇒ Object
70
71
72
73
74
75
76
77
|
# File 'lib/lhj/command/oss/list.rb', line 70
def save(obj_keys, key_file, url_file)
obj_urls = obj_keys.sort { |a, b| a.slice(/\..*/) <=> b.slice(/\..*/) }.map { |k| "#{Lhj::OSS::Helper.instance.url_path}/#{k}" }
obj_urls = obj_urls.map { |k| "#{k}?#{@output_suffix}" } if @output_suffix
FileUtils.chdir(@current_path) do
File.write(key_file, obj_keys.to_yaml)
File.write(url_file, obj_urls.to_yaml) unless @all_folder
end
end
|