Class: CloneRemoteDb::Loader
- Inherits:
-
Object
- Object
- CloneRemoteDb::Loader
- Defined in:
- lib/clone_remote_db/loader.rb
Instance Attribute Summary collapse
-
#script ⇒ Object
readonly
Returns the value of attribute script.
Instance Method Summary collapse
- #banner(opts) ⇒ Object
- #defaults ⇒ Object
- #die(key_or_msg, msg = nil, exit_code = -1)) ⇒ Object
-
#initialize(script) ⇒ Loader
constructor
A new instance of Loader.
- #opt(opts, key, msg, arg_opts = {}) ⇒ Object
- #options ⇒ Object
- #parse_opts! ⇒ Object
- #replace_variables(str) ⇒ Object
- #version(opts) ⇒ Object
Constructor Details
#initialize(script) ⇒ Loader
Returns a new instance of Loader.
6 7 8 |
# File 'lib/clone_remote_db/loader.rb', line 6 def initialize(script) @script = script end |
Instance Attribute Details
#script ⇒ Object (readonly)
Returns the value of attribute script.
4 5 6 |
# File 'lib/clone_remote_db/loader.rb', line 4 def script @script end |
Instance Method Details
#banner(opts) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/clone_remote_db/loader.rb', line 34 def (opts) opts. = "Download and import a remote (PostgreSQL) database into a local database.\n\nUsage:\n \#{script} [options]\n\nOptions:\n" end |
#defaults ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/clone_remote_db/loader.rb', line 66 def defaults { local_dest: '~/pg_dumps/{remote_db}/%Y-%m/%Y-%m-%d_%H.%M.sql.gz', gzip_opts: %w(-9 --stdout), pg_dump_opts: %w(-c -O), pg_exclude_data: %w(versions), remote_user: 'postgres' } end |
#die(key_or_msg, msg = nil, exit_code = -1)) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/clone_remote_db/loader.rb', line 24 def die(key_or_msg, msg = nil, exit_code = -1) if key_or_msg.is_a?(String) $stderr.puts "Error: #{key_or_msg}" else arg = '--' + key_or_msg.to_s.gsub('_', '-') $stderr.puts "Error: argument #{arg} #{msg}." end exit(exit_code) end |
#opt(opts, key, msg, arg_opts = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/clone_remote_db/loader.rb', line 52 def opt(opts, key, msg, arg_opts = {}) arg = "--#{key.to_s.gsub('_', '-')}" arg << " #{arg_opts[:arg]}" if arg_opts[:arg] on_args = [arg, msg] on_args.unshift(arg_opts[:short]) if arg_opts[:short] opts.on(*on_args, msg) do |v| [key] = v end end |
#options ⇒ Object
62 63 64 |
# File 'lib/clone_remote_db/loader.rb', line 62 def ||= {}.merge(defaults) end |
#parse_opts! ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/clone_remote_db/loader.rb', line 10 def parse_opts! OptionParser.new do |opts| (opts) version(opts) opt(opts, :local_db, "Local postgresql database name", arg: 'DBNAME', short: '-l') opt(opts, :remote_db, "Remote postgresql database name", arg: 'DBNAME', short: '-r') opt(opts, :host, "Host of the postgresql database", arg: 'HOST', short: '-h') opt(opts, :import_only, "Skip download and provide path to dump.sql.gz file", arg: 'PATH') opt(opts, :local_dest, "The path to save the dump.sql.gz file to", arg: 'PATH') opt(opts, :dry_run, "Don't actually do anything", short: '-n') end.parse! end |
#replace_variables(str) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/clone_remote_db/loader.rb', line 76 def replace_variables(str) new_str = str %w(local_db remote_db remote_user).each do |arg| new_str = new_str.gsub('{' + arg + '}', [arg.to_sym]) end new_str end |
#version(opts) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/clone_remote_db/loader.rb', line 45 def version(opts) opts.on('--version', 'Show the version') do io.puts "clone_remote_db #{CloneRemoteDb::VERSION} (c) Josh McDade" exit(0) end end |