Class: RemoteDatabaseImporter::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_database_importer/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



8
9
10
11
12
13
14
# File 'lib/remote_database_importer/config.rb', line 8

def initialize
  @config = TTY::Config.new

  config.filename = "remote_database_importer"
  config.extname = ".yml"
  config.append_path Dir.pwd
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/remote_database_importer/config.rb', line 6

def config
  @config
end

Instance Method Details

#ask(question, default: nil, options: nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/remote_database_importer/config.rb', line 28

def ask(question, default: nil, options: nil)
  question += " (#{options.join(" / ")})" if options.present?
  question += " [#{default}]" if default.present?

  puts Colorize.blue(question)
  answer = $stdin.gets.chomp
  answer.present? ? answer : default
end

#create_default_configObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/remote_database_importer/config.rb', line 37

def create_default_config
  enter_new_environments = true
  environment_count = 1

  local_db_name = ask("Whats the name of the local database you wanna import to?", default: "myawesomeapp_development")
  config.set(:local_db_name, value: local_db_name)
  puts

  while enter_new_environments
    puts Colorize.green("#{environment_count}. Environment")
    env = ask("Whats the name of the #{environment_count}. environment you wanna add?", default: "staging")
    puts

    puts Colorize.green("Database settings:")
    db_name = ask("Enter the DB name for the #{env} environment:", default: "myawesomeapp_#{env}")
    db_user = ask("Enter the DB user for the #{env} environment:", default: "deployer")
    puts

    puts Colorize.green("Connection settings:")
    host = ask("Enter the IP or hostname of the DB server:", default: "myawesomeapp.com")
    dump_type = ask("Should the DB dump happen over a ssh tunnel or can pg_dump connect to the DB port directly?", default: "pg_dump", options: ["ssh_tunnel", "pg_dump"])

    ssh_user, ssh_port, postgres_port = nil
    if dump_type == "ssh_tunnel"
      ssh_user = ask("Enter the username for the SSH connection:", default: "deployer")
      ssh_port = ask("Enter the port for the SSH connection:", default: "22")
    else
      postgres_port = ask("Enter the database port for the pg_dump command:", default: "5432")
    end

    puts Colorize.green("Define custom commands that run after successful import:")
    custom_commands = ask("Enter semicolon separated commands that should run after importing the DB:", default: "rake db:migrate; echo 'All Done'")
    puts

    env_config = {
      env.to_s => {
        "database" => {
          "name" => db_name,
          "user" => db_user
        },
        "connection" => {
          "host" => host,
          "dump_type" => dump_type,
          "postgres_port" => postgres_port,
          "ssh_user" => ssh_user,
          "ssh_port" => ssh_port
        },
        "custom_commands" => custom_commands
      }
    }
    config.append(env_config, to: :environments)

    continue = ask("Do you wanna add another environment? (anything other than 'yes' will exit)")
    if continue&.downcase == "yes"
      environment_count += 1
    else
      enter_new_environments = false
    end
  end

  config.write
end

#read_or_create_configfileObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/remote_database_importer/config.rb', line 16

def read_or_create_configfile
  unless config.exist?
    puts Colorize.green("===========================================================")
    puts "Hi there! There is no config file yet, lets create one! 😄"
    create_default_config
    config_location = [config.filename, config.extname].join
    puts "Created config file: #{config_location}"
    puts Colorize.green("===========================================================")
  end
  config.read
end