Skip to content

Commit

Permalink
Convert hostfiles script to use object-oriented Hostfile::Manager module
Browse files Browse the repository at this point in the history
  • Loading branch information
Aneurysm9 committed Nov 12, 2010
1 parent fe6aa06 commit 630967b
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions bin/hostfiles
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use File::Find;
use Hostfile::Manager;

my @enabled = ();
Expand All @@ -28,15 +29,29 @@ pod2usage(-exitstatus => 0, -verbose => 2) if $man;

pod2usage(1) unless (@enabled || @disabled || $status);

read_hostfile();
my $manager = Hostfile::Manager->new;

disable_fragments(@disabled) if @disabled;
enable_fragments(@enabled) if @enabled;
map { print "Disabling $_\n"; $manager->disable_fragment($_) } @disabled;
map { print "Enabling $_\n"; $manager->enable_fragment($_) } @enabled;

write_hostfile() if (@disabled || @enabled);
$manager->write_hostfile if (@disabled || @enabled);

show_status() if $status;
find(\&status_helper, $manager->path_prefix) if $status;

sub status_helper
{
my $fragment = $File::Find::name;
return if -d $fragment;

my $path_prefix = $manager->path_prefix;
$fragment =~ s{^$path_prefix}{};

my $fragment_contents = $manager->get_fragment($fragment);

my $found = $manager->hostfile =~ /@{[$manager->block($fragment)]}/g;
my $flag = $found ? ($1 eq $fragment_contents ? "+" : "*") : " ";
print "$flag $fragment\n";
}

__END__
Expand Down

0 comments on commit 630967b

Please sign in to comment.