Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix output when needing sudo

  • Loading branch information...
commit 468fc75c57a909dde7c2dff96c8953276690cef9 1 parent b279c61
@bjeanes authored
View
186 bin/ghost
@@ -20,112 +20,114 @@ def help_text(exit_code = 0)
exit(exit_code)
end
-if ARGV.size.zero? || ['-h', '--help', 'help'].include?(ARGV.first)
- help_text
-else
- case ARGV[0]
- when 'add'
- if [2,3].include?(ARGV.size)
- begin
+begin
+ if ARGV.size.zero? || ['-h', '--help', 'help'].include?(ARGV.first)
+ help_text
+ else
+ case ARGV[0]
+ when 'add'
+ if [2,3].include?(ARGV.size)
+ begin
+ ARGV.shift
+ host = Ghost::Host.add(*ARGV)
+ puts " [Adding] #{host.name} -> #{host.ip}"
+ exit 0
+ rescue SocketError
+ $stderr.puts "Cannot find IP for host"
+ exit 3
+ rescue Ghost::RecordExists
+ $stderr.puts "Cannot overwrite an existing entry. Use the modify subcommand"
+ exit 3
+ end
+ else
+ $stderr.puts "The add subcommand requires at least a hostname.\n\n"
+ help_text 2
+ end
+ when 'modify'
+ if ARGV.size == 3
ARGV.shift
+ ARGV << true
host = Ghost::Host.add(*ARGV)
- puts " [Adding] #{host.name} -> #{host.ip}"
+ puts " [Modifying] #{host.name} -> #{host.ip}"
exit 0
- rescue SocketError
- $stderr.puts "Cannot find ip for host"
- exit 3
- rescue Errno::EACCES
- $stderr.puts "Please re-run as root or using sudo"
- exit 3
- rescue StandardError
- $stderr.puts "Cannot overwrite an existing entry. Use the modify subcommand"
- exit 3
+ else
+ $stderr.puts "The modify subcommand requires a hostname and an IP.\n\n"
+ help_text 4
end
- else
- $stderr.puts "The add subcommand requires at least a hostname.\n\n"
- help_text 2
- end
- when 'modify'
- if ARGV.size == 3
- ARGV.shift
- ARGV << true
- host = Ghost::Host.add(*ARGV)
- puts " [Modifying] #{host.name} -> #{host.ip}"
- exit 0
- else
- $stderr.puts "The modify subcommand requires a hostname and an IP.\n\n"
- help_text 4
- end
- when 'delete', 'del', 'remove', 'rm'
- if ARGV.size == 2
- Ghost::Host.delete(ARGV[1])
- puts " [Deleting] #{ARGV[1]}"
- exit 0
- else
- $stderr.puts "The delete subcommand requires a hostname.\n\n"
- help_text 2
- end
- when 'delete_matching'
- if ARGV.size == 2
- matched = Ghost::Host.delete_matching(ARGV[1])
- if matched.empty?
- puts " No matching entries found"
+ when 'delete', 'del', 'remove', 'rm'
+ if ARGV.size == 2
+ Ghost::Host.delete(ARGV[1])
+ puts " [Deleting] #{ARGV[1]}"
+ exit 0
else
- matched.each { |h| puts " [Deleting] #{h}" }
+ $stderr.puts "The delete subcommand requires a hostname.\n\n"
+ help_text 2
end
- exit 0
- else
- $stderr.puts "The delete_matching subcommand requires a hostname pattern.\n\n"
- help_text 2
- end
- when 'list'
- filter = %r|#{ARGV[1]}| if ARGV.size == 2
+ when 'delete_matching'
+ if ARGV.size == 2
+ matched = Ghost::Host.delete_matching(ARGV[1])
+ if matched.empty?
+ puts " No matching entries found"
+ else
+ matched.each { |h| puts " [Deleting] #{h}" }
+ end
+ exit 0
+ else
+ $stderr.puts "The delete_matching subcommand requires a hostname pattern.\n\n"
+ help_text 2
+ end
+ when 'list'
+ filter = %r|#{ARGV[1]}| if ARGV.size == 2
- hosts = Ghost::Host.list
+ hosts = Ghost::Host.list
- if filter
- hosts.reject! { |host| host.name !~ filter && host.ip !~ filter }
- end
+ if filter
+ hosts.reject! { |host| host.name !~ filter && host.ip !~ filter }
+ end
- pad = hosts.max{|a,b| a.to_s.length <=> b.to_s.length }.to_s.length
+ pad = hosts.max{|a,b| a.to_s.length <=> b.to_s.length }.to_s.length
- puts "Listing #{hosts.size} host(s):"
+ puts "Listing #{hosts.size} host(s):"
- hosts.each do |host|
- puts "#{host.name.rjust(pad+2)} -> #{host.ip}"
- end
- exit 0
- when 'empty'
- print " [Emptying] "
- Ghost::Host.empty!
- puts "Done."
- exit 0
- when 'export'
- hosts = Ghost::Host.list
- hosts.each do |host|
- puts "#{host.name} #{host.ip}"
- end
- exit 0
- when 'import'
- if ARGV.size == 2
- begin
- import_file = open(ARGV[1])
- import_file.each_line do |line|
- host_infos = line.strip.split(' ', 2)
- host = Ghost::Host.add(*host_infos)
- puts " [Adding] #{host.name} -> #{host.ip}"
+ hosts.each do |host|
+ puts "#{host.name.rjust(pad+2)} -> #{host.ip}"
+ end
+ exit 0
+ when 'empty'
+ print " [Emptying] "
+ Ghost::Host.empty!
+ puts "Done."
+ exit 0
+ when 'export'
+ hosts = Ghost::Host.list
+ hosts.each do |host|
+ puts "#{host.name} #{host.ip}"
+ end
+ exit 0
+ when 'import'
+ if ARGV.size == 2
+ begin
+ import_file = open(ARGV[1])
+ import_file.each_line do |line|
+ host_infos = line.strip.split(' ', 2)
+ host = Ghost::Host.add(*host_infos)
+ puts " [Adding] #{host.name} -> #{host.ip}"
+ end
+ exit 0
+ rescue
+ $stderr.puts "Cannot import. A problem occured while opening the import file."
+ exit 5
end
- exit 0
- rescue
- $stderr.puts "Cannot import. A problem occured while opening the import file."
- exit 5
+ else
+ $stderr.puts "The import command requires an input file.\n\n"
+ help_text 6
end
else
- $stderr.puts "The import command requires an input file.\n\n"
- help_text 6
+ $stderr.puts "Invalid option: #{ARGV[0]}"
+ help_text 1
end
- else
- $stderr.puts "Invalid option: #{ARGV[0]}"
- help_text 1
end
+rescue Errno::EACCES
+ $stderr.puts "Please re-run as root or using sudo"
+ exit 3
end
View
5 lib/ghost.rb
@@ -1,5 +1,10 @@
$: << File.dirname(__FILE__)
+module Ghost
+ class RecordExists < StandardError
+ end
+end
+
require 'rbconfig'
case RbConfig::CONFIG['host_os']
View
3  lib/ghost/linux-host.rb
@@ -2,6 +2,7 @@
module Ghost
class Host
+
attr_reader :host, :ip
def initialize(host, ip)
@@ -64,7 +65,7 @@ def add(host, ip = "127.0.0.1", force = false)
new_host
else
- raise "Can not overwrite existing record"
+ raise Ghost::RecordExists, "Can not overwrite existing record"
end
end
end
View
2  lib/ghost/mac-host.rb
@@ -26,7 +26,7 @@ def add(host, ip = "127.0.0.1", force = false)
flush!
find_by_host(host)
else
- raise "Can not overwrite existing record"
+ raise Ghost::RecordExists, "Can not overwrite existing record"
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.