Skip to content

Commit

Permalink
Begin moving functionality to module
Browse files Browse the repository at this point in the history
  • Loading branch information
Aneurysm9 committed Nov 10, 2010
1 parent e12f5d3 commit 6cc74f3
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 59 deletions.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ MANIFEST
MANIFEST.SKIP
README
bin/hostfiles
lib/Hostfile/Manager.pm
2 changes: 1 addition & 1 deletion MANIFEST.SKIP
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.git
.swp
*.swp
3 changes: 2 additions & 1 deletion Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use ExtUtils::MakeMaker;

WriteMakefile(
NAME => 'Hostfile::Manager',
VERSION => '0.1',
VERSION_FROM => 'lib/Hostfile/Manager.pm',
EXE_FILES => ['bin/hostfiles'],
PREREQ_PM => {
'Getopt::Long' => 0,
'File::Find' => 0,
'File::Slurp' => 0,
'Pod::Usage' => 0,
},
AUTHOR => 'Anthony J. Mirabella',
);
68 changes: 11 additions & 57 deletions bin/hostfiles
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
use strict;
use warnings;
use Getopt::Long;
use File::Find;
use File::Slurp;
use Pod::Usage;

my $path_prefix = '/etc/hostfiles/';
my $hostfile_path = '/etc/hosts';
use Hostfile::Manager;

my @enabled = ();
my @disabled = ();
Expand All @@ -31,58 +27,16 @@ pod2usage(-exitstatus => 0, -verbose => 2) if $man;
@disabled = split(/,/, join(',', @disabled));

pod2usage(1) unless (@enabled || @disabled || $status);
die("You must have permission to read and write $hostfile_path\n") unless (-r $hostfile_path && -w $hostfile_path);

my $hostfile = read_file($hostfile_path);

foreach my $disable (@disabled)
{
print "Disabling $disable\n";
$hostfile =~ s/@{[block($disable)]}//g;
}

foreach my $enable (@enabled)
{
my $filename = $path_prefix . $enable;
unless (-e $filename)
{
print "Hostfile fragment $enable does not exist\n";
next;
}

print "Enabling $enable\n";
$hostfile =~ s/@{[block($enable)]}//g;

my $addition = read_file($filename);
$hostfile .= "# BEGIN: $enable\n$addition# END: $enable\n\n";
}

write_file($hostfile_path, $hostfile);

find(\&show_status, $path_prefix) if $status;
sub show_status
{
my $fragment = $File::Find::name;
return if -d $fragment;

my $fragment_contents = read_file($fragment);
$fragment =~ s{^$path_prefix}{};

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

BEGIN {
my %re;
sub block
{
my $name = shift;
$re{$name} ||= qr/(?:#+[\r\n])?#+\s*BEGIN: $name[\r\n](?:#+[\r\n])?(.*)(?:#+[\r\n])?#+\s*END: $name[\r\n](?:#+[\r\n])?/ms;
return $re{$name};
}
}

read_hostfile();

disable_fragments(@disabled) if @disabled;
enable_fragments(@enabled) if @enabled;

write_hostfile() if (@disabled || @enabled);

show_status() if $status;


__END__
Expand Down
90 changes: 90 additions & 0 deletions lib/Hostfile/Manager.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package Hostfile::Manager;

use strict;
use warnings;
use File::Slurp;
use File::Find;

BEGIN {
use Exporter ();
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK);

$VERSION = '0.2';
@ISA = qw(Exporter);
@EXPORT = qw(disable_fragments enable_fragments read_hostfile show_status write_hostfile);
}

my %re;
my $path_prefix = '/etc/hostfiles/';
my $hostfile_path = '/etc/hosts';
my $hostfile;

sub block
{
my $name = shift;
$re{$name} ||= qr/(?:#+[\r\n])?#+\s*BEGIN: $name[\r\n](?:#+[\r\n])?(.*)(?:#+[\r\n])?#+\s*END: $name[\r\n](?:#+[\r\n])?/ms;
return $re{$name};
}

sub enable_fragments
{
my @enabled = shift || ();
foreach my $enable (@enabled)
{
my $filename = $path_prefix . $enable;
unless (-e $filename)
{
print "Hostfile fragment $enable does not exist\n";
next;
}

print "Enabling $enable\n";
$hostfile =~ s/@{[block($enable)]}//g;

my $addition = read_file($filename);
$hostfile .= "# BEGIN: $enable\n$addition# END: $enable\n\n";
}
}

sub disable_fragments
{
my @disabled = shift || ();
foreach my $disable (@disabled)
{
print "Disabling $disable\n";
$hostfile =~ s/@{[block($disable)]}//g;
}
}

sub read_hostfile
{
die("You must have permission to read $hostfile_path\n") unless (-r $hostfile_path);
$hostfile = read_file($hostfile_path);
}

sub show_status
{
find(\&status_helper, $path_prefix);
}

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

my $fragment_contents = read_file($fragment);
$fragment =~ s{^$path_prefix}{};

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

sub write_hostfile
{
die("You must have permission to write $hostfile_path\n") unless (-w $hostfile_path);
write_file($hostfile_path, $hostfile);
}

1;

0 comments on commit 6cc74f3

Please sign in to comment.