class Gitrob::CLI::Commands::Configure

Constants

CONFIGURATION_FILE_PATH

Public Class Methods

configured?() click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 22
def self.configured?
  File.exist?(CONFIGURATION_FILE_PATH)
end
load_configuration!() click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 26
def self.load_configuration!
  fail ConfigurationFileNotFound \
    unless File.exist?(CONFIGURATION_FILE_PATH)
  fail ConfigurationFileNotReadable \
    unless File.readable?(CONFIGURATION_FILE_PATH)
  YAML.load(File.read(CONFIGURATION_FILE_PATH))
rescue Psych::SyntaxError
  raise ConfigurationFileCorrupt
end
new(options) click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 12
def initialize(options)
  @options = options
  info("Starting Gitrob configuration wizard")
  return unless agree_to_overwrite?
  config = gather_configuration
  task("Saving configuration to #{CONFIGURATION_FILE_PATH}") do
    save_configuration(config)
  end
end

Private Instance Methods

agree_to_overwrite?() click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 38
def agree_to_overwrite?
  return true unless self.class.configured?
  warn("Configuration file already exists\n")
  agree(
    "Proceed and overwrite existing configuration file? (y/n): ")
end
build_yaml(config) click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 108
def build_yaml(config)
  YAML.dump(
    "sql_connection_uri" => make_connection_uri(
      config[:username],
      config[:password],
      config[:hostname],
      config[:port],
      config[:database]
    ),
    "github_access_tokens" => config[:access_tokens]
  )
end
gather_access_tokens() click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 87
def gather_access_tokens
  tokens = []
  while tokens.uniq.empty?
    tokens = ask("Enter GitHub access tokens (blank line to stop):",
                 ->(ans) { ans =~ /[a-f0-9]{40}/ ? ans : nil }) do |q|
                   q.gather = ""
                 end
  end
  tokens
end
gather_configuration() click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 45
def gather_configuration
  {
    :hostname      => gather_hostname,
    :port          => gather_port,
    :username      => gather_username,
    :password      => gather_password,
    :database      => gather_database,
    :access_tokens => gather_access_tokens
  }
end
gather_database() click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 81
def gather_database
  ask("Enter PostgreSQL database name: ") do |q|
    q.default = "gitrob"
  end
end
gather_hostname() click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 56
def gather_hostname
  ask("Enter PostgreSQL hostname: ") do |q|
    q.default = "localhost"
  end
end
gather_password() click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 75
def gather_password
  ask("Enter PostgreSQL password (masked): ") do |q|
    q.echo = "x"
  end
end
gather_port() click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 62
def gather_port
  ask("Enter PostgreSQL port: |5432| ", Integer) do |q|
    q.default = 5432
    q.in = 1..65_535
  end
end
gather_username() click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 69
def gather_username
  ask("Enter PostgreSQL username: ") do |q|
    q.default = "gitrob"
  end
end
make_connection_uri(username, password, hostname, port, database) click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 104
def make_connection_uri(username, password, hostname, port, database)
  "postgres://#{username}:#{password}@#{hostname}:#{port}/#{database}"
end
save_configuration(config) click to toggle source
# File lib/gitrob/cli/commands/configure.rb, line 98
def save_configuration(config)
  File.open(CONFIGURATION_FILE_PATH, "w") do |file|
    file.write(build_yaml(config))
  end
end