Class: Lhj::Command::Init
Overview
Class Method Summary
collapse
Instance Method Summary
collapse
#auto_spin, #begin_title, #run, #stop
Constructor Details
#initialize(argv) ⇒ Init
Returns a new instance of Init.
33
34
35
36
|
# File 'lib/lhj/command/init.rb', line 33
def initialize(argv)
@url = argv.option('url')
super
end
|
Class Method Details
.options ⇒ Object
19
20
21
22
23
|
# File 'lib/lhj/command/init.rb', line 19
def self.options
[
%w[--url 配置文件的url]
]
end
|
Instance Method Details
#download_file(urls) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/lhj/command/init.rb', line 67
def download_file(urls)
urls.each do |url|
file_path = file_name_from_url(url)
save_url_to_file(url, file_path)
end
puts "工具初始化完成 \n"
end
|
#file_name_from_url(url) ⇒ Object
79
80
81
82
|
# File 'lib/lhj/command/init.rb', line 79
def file_name_from_url(url)
file_name = file_name_with_url(url)
File.join(target_folder, file_name)
end
|
#file_name_with_url(url) ⇒ Object
63
64
65
|
# File 'lib/lhj/command/init.rb', line 63
def file_name_with_url(url)
url.scan(%r{/([^/]+)}).flatten.last
end
|
#handle ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/lhj/command/init.rb', line 38
def handle
FileUtils.mkdir_p(target_folder) unless File.exist?(target_folder)
if @url
file_path = file_name_from_url(@url)
File.delete(file_path) if File.exist?(file_path)
save_url_to_file(@url, file_path)
else
file_path = File.join(target_folder, 'config_file.yml')
end
content = YAML.load_file(file_path)
download_file(content) if content.is_a?(Array)
end
|
#save_file(url, target) ⇒ Object
55
56
57
58
59
60
61
|
# File 'lib/lhj/command/init.rb', line 55
def save_file(url, target)
http_client = Faraday.new do |c|
c.adapter Faraday.default_adapter
end
response = http_client.get(url)
File.open(target, 'wb') { |fp| fp.write(response.body) }
end
|
#save_url_to_file(url, file_path) ⇒ Object
75
76
77
|
# File 'lib/lhj/command/init.rb', line 75
def save_url_to_file(url, file_path)
save_file(url, file_path) unless File.exist?(file_path)
end
|
#target_folder ⇒ Object
51
52
53
|
# File 'lib/lhj/command/init.rb', line 51
def target_folder
Lhj::Config.instance.home_dir
end
|
#validate! ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/lhj/command/init.rb', line 25
def validate!
super
unless @url
path = File.join(target_folder, 'config_file.yml')
help! '配置url必须输入' unless File.exist?(path)
end
end
|