[root@h102 ~]# cat /usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/forklift
#!/usr/bin/env ruby
require 'rubygems'
require 'fileutils'
begin
require 'forklift'
rescue LoadError
require "#{File.expand_path(File.dirname(__FILE__))}/../lib/forklift.rb"
end
def generate
p = Dir.pwd
Dir.mkdir "#{p}/config"
Dir.mkdir "#{p}/config/connections"
Dir.mkdir "#{p}/config/connections/mysql"
Dir.mkdir "#{p}/config/connections/elasticsearch"
Dir.mkdir "#{p}/config/connections/csv"
Dir.mkdir "#{p}/log"
Dir.mkdir "#{p}/pid"
Dir.mkdir "#{p}/template"
Dir.mkdir "#{p}/transformations"
Dir.mkdir "#{p}/transports"
Dir.mkdir "#{p}/patterns"
template('source.yml', "#{p}/config/connections/mysql/source.yml")
template('destination.yml', "#{p}/config/connections/mysql/destination.yml")
template('email.yml', "#{p}/config/email.yml")
template('email.erb', "#{p}/template/email.erb")
template('plan.rb', "#{p}/plan.rb")
end
def template(source, destination)
t = "#{File.expand_path(File.dirname(__FILE__))}/../template"
FileUtils.copy("#{t}/#{source}", destination)
puts "Example plan generated"
end
def run_plan
file = "#{Dir.pwd}/#{ARGV[0]}"
if ARGV[0].nil?
puts "[error] Please provide a plan.rb as the first argument"
exit(1)
end
Dir.chdir File.expand_path(File.dirname(ARGV[0]))
begin
require 'bundler'
Bundler.require(:default)
rescue Exception => e
puts "cannot load bundler: #{e}"
end
require file
end
############
if ['--generate', '-generate'].include?(ARGV[0])
generate
else
run_plan
end
[root@h102 ~]#
这个脚本做了三件事:
加载了这个文件
[root@h102 bin]# ls
forklift x
[root@h102 bin]# pwd
/usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin
[root@h102 bin]# cat x
puts "#{File.expand_path(File.dirname(__FILE__))}/../lib/forklift.rb"
[root@h102 bin]# ruby x
/usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/../lib/forklift.rb
[root@h102 bin]# ll /usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/../lib/forklift.rb
-rwxr-xr-x 1 root rvm 651 Aug 9 15:12 /usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/../lib/forklift.rb
[root@h102 bin]#
def generate
p = Dir.pwd
Dir.mkdir "#{p}/config"
Dir.mkdir "#{p}/config/connections"
Dir.mkdir "#{p}/config/connections/mysql"
Dir.mkdir "#{p}/config/connections/elasticsearch"
Dir.mkdir "#{p}/config/connections/csv"
Dir.mkdir "#{p}/log"
Dir.mkdir "#{p}/pid"
Dir.mkdir "#{p}/template"
Dir.mkdir "#{p}/transformations"
Dir.mkdir "#{p}/transports"
Dir.mkdir "#{p}/patterns"
template('source.yml', "#{p}/config/connections/mysql/source.yml")
template('destination.yml', "#{p}/config/connections/mysql/destination.yml")
template('email.yml', "#{p}/config/email.yml")
template('email.erb', "#{p}/template/email.erb")
template('plan.rb', "#{p}/plan.rb")
end
整个过程就是在当前目录中创建若干目录,然后把模板文件拷贝进来
其中使用到了 template 方法
def template(source, destination)
t = "#{File.expand_path(File.dirname(__FILE__))}/../template"
FileUtils.copy("#{t}/#{source}", destination)
puts "Example plan generated"
end
它起的作用就是负责拷贝模板文件到目标位置中
[root@h102 bin]# pwd
/usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin
[root@h102 bin]# ll ../template/
total 20
-rwxr-xr-x 1 root rvm 89 Aug 9 15:12 destination.yml
-rwxr-xr-x 1 root rvm 29 Aug 9 15:12 email.erb
-rwxr-xr-x 1 root rvm 538 Aug 9 15:12 email.yml
-rwxr-xr-x 1 root rvm 150 Aug 9 15:12 plan.rb
-rwxr-xr-x 1 root rvm 84 Aug 9 15:12 source.yml
[root@h102 bin]#
def run_plan
file = "#{Dir.pwd}/#{ARGV[0]}"
if ARGV[0].nil?
puts "[error] Please provide a plan.rb as the first argument"
exit(1)
end
Dir.chdir File.expand_path(File.dirname(ARGV[0]))
begin
require 'bundler'
Bundler.require(:default)
rescue Exception => e
puts "cannot load bundler: #{e}"
end
require file
end
这个方法最主要的就是 require file
file
就是当前目录中的 plan.rb 文件(可以不是这个文件名,自定义其它文件名),接在 forklift 后面,作为第一个参数
mkdir forklift
cd forklift/
vim Gemfile
cat Gemfile
bundle install
bundle exec forklift --generate
tree
which forklift
cat /usr/local/rvm/gems/ruby-2.3.0/bin/forklift
ruby -e "puts Gem.bin_path('forklift_etl', 'forklift', '>= 0.a')"
cat /usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/forklift
ruby x
ll /usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/../lib/forklift.rb
ll ../template/
原文地址
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。