class PostDB::CLI::Domains

Public Instance Methods

add(domain_name = nil) click to toggle source
# File lib/postdb/cli/domains.rb, line 26
def add(domain_name = nil)
  unless domain_name
    domain_name = prompt.ask("Domain:") do |q|
      q.required(true)
    end
  end

  domain_name = domain_name.downcase

  if PostDB::Domain.where(name: domain_name).count > 0
    exit_with_warning("The domain '#{domain_name}' has already been added.")
  end

  domain = PostDB::Domain.new
  domain.name = domain_name

  if domain.save
    prompt.ok("The domain '#{domain_name}' has been added.")
  else
    errors = domain.errors.full_messages.map { |m| " #{m}" }
    exit_with_error("The domain '#{domain_name}' couldn't be added:", *errors)
  end
end
list() click to toggle source
# File lib/postdb/cli/domains.rb, line 9
def list
  domains = PostDB::Domain.all

  if domains.empty?
    exit_with_warning("There don't appear to be any domains on this system.")
  end

  domains = domains.to_a
  domains.sort! { |a, b| a.name <=> b.name }

  puts TTY::Table.new(
    header: ["Domain Name", "Total Users", "Total Aliases"].pad(' '),
    rows: domains.map { |d| [d.name, d.users.count, d.forwarding_aliases.count].pad(' ') }
  ).render(:ascii, multiline: true)
end
remove(domain_name = nil) click to toggle source
# File lib/postdb/cli/domains.rb, line 52
def remove(domain_name = nil)
  unless domain_name
    domains = PostDB::Domain.all

    if domains.empty?
      exit_with_warning("There don't appear to be any domains on this system.")
    end

    domains = domains.to_a
    domains.sort! { |a, b| a.name <=> b.name }

    domain_name = prompt.select("Domain:", domains.map(&:name))
  end

  domain_name = domain_name.downcase

  domains = PostDB::Domain.where(name: domain_name)

  if domains.empty?
    exit_with_warning("The domain '#{domain_name}' could not be found.")
  end

  unless options[:force]
    confirm_action!("Remove the domain '#{domain_name}'?", "'#{domain_name}' left untouched.")
  end

  domains.each(&:destroy)

  prompt.ok("The domain '#{domain_name}' has been removed.")
end