Class: Taps::Cli
- Inherits:
-
Object
- Object
- Taps::Cli
- Defined in:
- lib/taps/cli.rb
Instance Attribute Summary collapse
-
#argv ⇒ Object
Returns the value of attribute argv.
Instance Method Summary collapse
- #clientoptparse(cmd) ⇒ Object
- #clientresumexfer(method, opts) ⇒ Object
- #clientxfer(method, opts) ⇒ Object
- #help ⇒ Object
-
#initialize(argv) ⇒ Cli
constructor
A new instance of Cli.
- #pull ⇒ Object
- #push ⇒ Object
- #run ⇒ Object
- #server ⇒ Object
- #serveroptparse ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(argv) ⇒ Cli
Returns a new instance of Cli.
20 21 22 |
# File 'lib/taps/cli.rb', line 20 def initialize(argv) @argv = argv end |
Instance Attribute Details
#argv ⇒ Object
Returns the value of attribute argv.
18 19 20 |
# File 'lib/taps/cli.rb', line 18 def argv @argv end |
Instance Method Details
#clientoptparse(cmd) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/taps/cli.rb', line 118 def clientoptparse(cmd) opts = { default_chunksize: 1000, database_url: nil, remote_url: nil, debug: false, resume_filename: nil, disable_compresion: false, indexes_first: false } OptionParser.new do |o| o. = "Usage: #{File.basename($PROGRAM_NAME)} #{cmd} [OPTIONS] <local_database_url> <remote_url>" case cmd when :pull o.define_head 'Pull a database from a taps server' when :push o.define_head 'Push a database to a taps server' end o.on('-s', '--skip-schema', "Don't transfer the schema, just data") { |_v| opts[:skip_schema] = true } o.on('-i', '--indexes-first', 'Transfer indexes first before data') { |_v| opts[:indexes_first] = true } o.on('-r', '--resume=file', 'Resume a Taps Session from a stored file') { |v| opts[:resume_filename] = v } o.on('-c', '--chunksize=N', 'Initial Chunksize') { |v| opts[:default_chunksize] = (v.to_i < 10 ? 10 : v.to_i) } o.on('-g', '--disable-compression', 'Disable Compression') { |_v| opts[:disable_compression] = true } o.on('-f', '--filter=regex', 'Regex Filter for tables') { |v| opts[:table_filter] = v } o.on('-t', '--tables=A,B,C', Array, 'Shortcut to filter on a list of tables') do |v| r_tables = v.collect { |t| "^#{t}$" }.join('|') opts[:table_filter] = "(#{r_tables})" end o.on('-e', '--exclude_tables=A,B,C', Array, 'Shortcut to exclude a list of tables') { |v| opts[:exclude_tables] = v } o.on('-d', '--debug', 'Enable Debug Messages') { |_v| opts[:debug] = true } o.parse!(argv) opts[:database_url] = argv.shift opts[:remote_url] = argv.shift if opts[:database_url].nil? warn 'Missing Database URL' puts o exit 1 end if opts[:remote_url].nil? warn 'Missing Remote Taps URL' puts o exit 1 end end opts end |
#clientresumexfer(method, opts) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/taps/cli.rb', line 173 def clientresumexfer(method, opts) session = ::OkJson.decode(File.read(opts.delete(:resume_filename))) session.symbolize_recursively! database_url = opts.delete(:database_url) remote_url = opts.delete(:remote_url) || session.delete(:remote_url) Taps::Config.verify_database_url(database_url) require 'taps/operation' newsession = session.merge(default_chunksize: opts[:default_chunksize], disable_compression: opts[:disable_compression], resume: true) Taps::Operation.factory(method, database_url, remote_url, newsession).run end |
#clientxfer(method, opts) ⇒ Object
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/taps/cli.rb', line 162 def clientxfer(method, opts) database_url = opts.delete(:database_url) remote_url = opts.delete(:remote_url) Taps::Config.verify_database_url(database_url) require 'taps/operation' Taps::Operation.factory(method, database_url, remote_url, opts).run end |
#help ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/taps/cli.rb', line 72 def help puts <<EOHELP Options ======= server Start a taps database import/export server pull Pull a database from a taps server push Push a database to a taps server version Taps version Add '-h' to any command to see their usage EOHELP end |
#pull ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/taps/cli.rb', line 33 def pull opts = clientoptparse(:pull) Taps.log.level = Logger::DEBUG if opts[:debug] if opts[:resume_filename] clientresumexfer(:pull, opts) else clientxfer(:pull, opts) end end |
#push ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/taps/cli.rb', line 43 def push opts = clientoptparse(:push) Taps.log.level = Logger::DEBUG if opts[:debug] if opts[:resume_filename] clientresumexfer(:push, opts) else clientxfer(:push, opts) end end |
#run ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/taps/cli.rb', line 24 def run method = (argv.shift || 'help').to_sym if %i[pull push server version].include? method send(method) else help end end |
#server ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/taps/cli.rb', line 53 def server opts = serveroptparse Taps.log.level = Logger::DEBUG if opts[:debug] Taps::Config.database_url = opts[:database_url] Taps::Config.login = opts[:login] Taps::Config.password = opts[:password] Taps::Config.verify_database_url require 'taps/server' Taps::Server.run!(port: opts[:port], environment: :production, logging: true, dump_errors: true) end |
#serveroptparse ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/taps/cli.rb', line 85 def serveroptparse opts = { port: 5000, database_url: nil, login: nil, password: nil, debug: false } OptionParser.new do |o| o. = "Usage: #{File.basename($PROGRAM_NAME)} server [OPTIONS] <local_database_url> <login> <password>" o.define_head 'Start a taps database import/export server' o.on('-p', '--port=N', 'Server Port') { |v| opts[:port] = v.to_i if v.to_i > 0 } o.on('-d', '--debug', 'Enable Debug Messages') { |_v| opts[:debug] = true } o.parse!(argv) opts[:database_url] = argv.shift opts[:login] = argv.shift || ENV['TAPS_LOGIN'] opts[:password] = argv.shift || ENV['TAPS_PASSWORD'] if opts[:database_url].nil? warn 'Missing Database URL' puts o exit 1 end if opts[:login].nil? warn 'Missing Login' puts o exit 1 end if opts[:password].nil? warn 'Missing Password' puts o exit 1 end end opts end |