-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin moving functionality to module
- Loading branch information
Showing
5 changed files
with
105 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ MANIFEST | |
MANIFEST.SKIP | ||
README | ||
bin/hostfiles | ||
lib/Hostfile/Manager.pm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
.git | ||
.swp | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |