lib/localdev.rb in localdev-0.3.2 vs lib/localdev.rb in localdev-0.4.0

- old
+ new

@@ -1,9 +1,9 @@ # # Localdev - Hosts file tool for local development # -# Copyright 2011 by Mark Jaquith +# Copyright 2011-2015 by Mark Jaquith # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. @@ -16,16 +16,17 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA require 'digest/md5' +require 'yaml' class Localdev - VERSION = '0.3.2' + VERSION = '0.4.0' def initialize - @debug = false + @debugMode = false @localdev = '/etc/hosts-localdev' @hosts = '/etc/hosts' @start = '#==LOCALDEV==#' @end = '#/==LOCALDEV==#' if !ARGV.first.nil? && [:on, :off, :add, :remove, :clear].include?( ARGV.first.to_sym ) @@ -42,13 +43,17 @@ send command when :add, :remove require_sudo object.nil? && exit_error_message("'localdev #{command}' requires you to provide a domain") ensure_localdev_exists - send command, object + if ARGV.first.nil? + send command, object + else + send command, object, ARGV.first + end when nil, '--help', '-h' - exit_message "Usage: localdev [on|off|status|list|clear]\n localdev [add|remove] domain" + exit_message "Usage: localdev [on|off|status|list|clear]\n localdev [add|remove] domain [ip-address]" else exit_error_message "Invalid command" end end @@ -61,11 +66,11 @@ def info puts "Localdev #{self.class::VERSION}" end def debug message - puts message if @debug + puts message if @debugMode end def exit_message message puts message exit @@ -85,19 +90,21 @@ def enable disable domains = [] File.open( @localdev, 'r' ) do |file| - domains = file.read.split("\n").uniq + domains = YAML::load file.read + domains = [] unless domains.respond_to? 'each' end File.open( @hosts, 'a' ) do |file| file.puts "\n" file.puts @start file.puts "# The md5 dummy entries are here so that things like MAMP Pro don't" file.puts "# discourtiously remove our entries" domains.each do |domain| - file.puts "127.0.0.1 #{Digest::MD5.hexdigest(domain)}.#{domain} #{domain}" + # puts domain.inspect + file.puts "#{domain['ip']} #{Digest::MD5.hexdigest(domain['domain'])}.#{domain['domain']} #{domain['domain']}" end file.puts @end end end @@ -132,36 +139,47 @@ end def update_localdev domains = [] File.open( @localdev, 'r' ) do |file| - domains = file.read.split("\n") + domains = YAML::load file.read + domains = [] unless domains.respond_to? 'each' debug domains.inspect yield domains debug domains.inspect end File.open( @localdev, 'w' ) do |file| - file.puts domains + file.puts YAML::dump domains end end - def add domain - update_localdev {|domains| domains << domain unless domains.include? domain } + def add domain, ip='127.0.0.1' + domain = { + 'domain' => domain, + 'ip' => ip + } + + _remove domain['domain'] + update_localdev {|domains| domains << domain } enable if :on == get_status - puts "Added '#{domain}'" + puts "Added #{domain['domain']} => #{domain['ip']}" status end def remove domain - update_localdev {|domains| domains = domains.delete domain } + _remove domain enable if :on == get_status - puts "Removed '#{domain}'" + puts "Removed #{domain}" status end + def _remove domain + update_localdev {|domains| domains = domains.delete_if{|item| item['domain'] == domain } } + end + def clear - update_localdev {|domains| domains.clear() } + update_localdev {|domains| domains.clear } enable if :on == get_status puts "Removed all domains" status end @@ -179,10 +197,13 @@ return status end def list File.open( @localdev, 'r' ) do |file| - puts file.read + domains = YAML::load file.read + domains.each do |domain| + puts "#{domain['domain']} => #{domain['ip']}" + end end end def status puts "Localdev is #{get_status}"