lib/postfix_admin/cli.rb in postfix_admin-0.3.1 vs lib/postfix_admin/cli.rb in postfix_admin-0.3.2

- old
+ new

@@ -58,10 +58,12 @@ else # no argument: show all domains and admins show_domains puts show_admins + puts + show_recent_logs end end def show_summary(domain_name = nil) if domain_name @@ -69,15 +71,27 @@ else show_general_summary end end + def show_recent_logs + if Log.count.zero? + puts "No logs" + return + end + + logs = Log.last(10) + puts_title("Recent Logs") + puts_log_table(logs) + end + # Set up a domain # Add a domain, add an admin, and grant the admin access to the domain - def setup_domain(domain_name, password, scheme: nil, rounds: nil) + def setup_domain(domain_name, password, description: nil, + scheme: nil, rounds: nil) admin = "admin@#{domain_name}" - add_domain(domain_name) + add_domain(domain_name, description: description) add_admin(admin, password, scheme: scheme, rounds: rounds) add_admin_domain(admin, domain_name) end # Tear down a domain @@ -137,20 +151,19 @@ rows = [] headings = ["No.", "Domain", "Aliases", "Mailboxes","Max Quota (MB)", "Active", "Description"] puts_title("Domains") + if Domain.without_all.empty? puts "No domains" return end Domain.without_all.each_with_index do |d, i| no = i + 1 - aliases_str = "%4d / %4s" % [d.pure_aliases.count, d.aliases_str] - mailboxes_str = "%4d / %4s" % [d.rel_mailboxes.count, d.mailboxes_str] - rows << [no.to_s, d.domain, aliases_str, mailboxes_str, + rows << [no.to_s, d.domain, d.alias_usage_display_str, d.mailbox_usage_display_str, d.maxquota_str, d.active_str, d.description] end puts_table(headings: headings, rows: rows) end @@ -205,10 +218,11 @@ def show_admins(domain_name = nil) admins = domain_name ? Admin.select { |a| a.rel_domains.exists?(domain_name) } : Admin.all headings = ["No.", "Admin", "Domains", "Active", "Scheme Prefix"] puts_title("Admins") + if admins.empty? puts "No admins" return end @@ -233,10 +247,11 @@ end headings = ["No.", "Email", "Name", "Quota (MB)", "Active", "Scheme Prefix", "Maildir"] puts_title("Accounts") + if mailboxes.empty? puts "No accounts" return end @@ -274,20 +289,22 @@ end def show_admin_domain(user_name) admin = Admin.find(user_name) puts_title("Admin Domains (#{user_name})") + if admin.rel_domains.empty? puts "\nNo domains for #{user_name}" return end rows = [] admin.rel_domains.each_with_index do |d, i| no = i + 1 rows << [no.to_s, d.domain] end + puts_table(rows: rows, headings: %w[No. Domain]) end def add_admin(user_name, password, super_admin: false, scheme: nil, rounds: nil) @@ -329,11 +346,11 @@ puts_registered("#{address}: #{goto}", "an alias") end def edit_account(address, options) quota = options[:quota] - raise "Invalid Quota value: #{quota}" if quota && quota <= 0 + raise "Invalid Quota value: #{quota}" if quota && quota < 0 mailbox_check(address) mailbox = Mailbox.find(address) mailbox.name = options[:name] if options[:name] mailbox.quota_mb = quota if quota @@ -375,25 +392,32 @@ @base.delete_account(address) puts_deleted(address) end def log(domain: nil, last: nil) - headings = %w[Timestamp Admin Domain Action Data] + headings = %w[No. Timestamp Admin Domain Action Data] rows = [] logs = if domain Log.where(domain: domain) else Log.all end logs = logs.last(last) if last - logs.each do |l| + puts_log_table(logs) + end + + def puts_log_table(logs) + headings = %w[No. Timestamp Admin Domain Action Data] + rows = [] + logs.each_with_index do |l, i| + no = i + 1 # TODO: Consider if zone should be included ('%Z'). time = l.timestamp.strftime("%Y-%m-%d %X") - rows << [time, l.username, l.domain, l.action, l.data] + rows << [no, time, l.username, l.domain, l.action, l.data] end puts_table(headings: headings, rows: rows) end @@ -402,28 +426,32 @@ puts "User Name,Password,Super Admin,Active" Admin.all.each do |a| puts [a.username, %Q!"#{a.password}"!, a.super_admin?, a.active].join(',') end puts + puts "Domains" puts "Domain Name,Max Quota,Active" Domain.without_all.each do |d| puts [d.domain, d.maxquota, d.active].join(',') end puts + puts "Mailboxes" puts "User Name,Name,Password,Quota,Maildir,Active" Mailbox.all.each do |m| puts [m.username, %Q!"#{m.name}"!, %Q!"#{m.password}"!, m.quota, %Q!"#{m.maildir}"!, m.active].join(',') end puts + puts "Aliases" puts "Address,Go to,Active" Alias.all.select { |a| !a.mailbox? }.each do |a| puts [a.address, %Q!"#{a.goto}"!, a.active].join(',') end puts + puts "Forwards" puts "Address,Go to,Active" Alias.all.select { |a| a.mailbox? && a.goto != a.address }.each do |a| puts [a.address, %Q!"#{a.goto}"!, a.active].join(',') end @@ -436,10 +464,11 @@ title = "Summary" rows << ["Domains", Domain.without_all.count] rows << ["Admins", Admin.count] rows << ["Mailboxes", Mailbox.count] rows << ["Aliases", Alias.pure.count] + rows << ["Logs", Log.count] puts_title(title) puts_table(rows: rows) end @@ -447,12 +476,12 @@ domain_name = domain_name.downcase domain_check(domain_name) rows = [] domain = Domain.find(domain_name) - rows << ["Mailboxes", "%4d / %4s" % [domain.rel_mailboxes.count, domain.mailboxes_str]] - rows << ["Aliases", "%4d / %4s" % [domain.pure_aliases.count, domain.aliases_str]] + rows << ["Mailboxes", domain.mailbox_usage_display_str] + rows << ["Aliases", domain.alias_usage_display_str] rows << ["Max Quota (MB)", domain.maxquota_str] rows << ["Active", domain.active_str] rows << ["Description", domain.description] puts_title(domain_name) @@ -503,9 +532,10 @@ unless File.exist?(config_file) create_config(config_file) puts "configure file: #{config_file} was generated.\nPlease execute after edit it." exit end + open(config_file) do |f| YAML.load(f.read) end end