Permalink
Browse files

first commit

  • Loading branch information...
1 parent a1c6424 commit 14458503d13261de203fe8903b1661c0075e36c8 @xaicron committed Jul 8, 2011
Showing with 215 additions and 5 deletions.
  1. +3 −2 Makefile.PL
  2. +19 −0 bin/envfile
  3. +89 −3 lib/App/envfile.pm
  4. +9 −0 t/01_new.t
  5. +93 −0 t/02_load_envfile.t
  6. +2 −0 xt/01_podspell.t
View
@@ -4,8 +4,9 @@ all_from 'lib/App/envfile.pm';
#requires '';
-#build_requires '';
-test_requires 'Test::More', 0.96;
+test_requires 'Test::More', 0.98;
+
+install_script 'bin/envfile';
tests join q{ }, map { sprintf 't%s.t', '/*' x $_ } 1..3;
author_tests 'xt';
View
@@ -0,0 +1,19 @@
+#!perl
+
+use strict;
+use warnings;
+use App::envfile;
+
+App::envfile->new->run(@ARGV);
+
+__END__
+
+=head1 NAME
+
+ envfile - runs another program with environment modified according to envfile
+
+=head1 SYNOPSIS
+
+ envfile file commands
+
+=cut
View
@@ -5,6 +5,46 @@ use warnings;
use 5.008_001;
our $VERSION = '0.01';
+sub new {
+ my $class = shift;
+ bless {}, $class;
+}
+
+sub run {
+ my ($self, @args) = @_;
+ my $envfile = shift @args;
+ $self->usage unless defined $envfile;
+ $self->load_envfile($envfile);
+ system(@args);
+}
+
+sub load_envfile {
+ my ($self, $file) = @_;
+ open my $fh, '<', $file or die "$file $!";
+ while (defined (my $line = readline $fh)) {
+ chomp $line;
+ next if index($line, '#') == 0;
+ next if $line =~ /^\s*$/;
+ my ($key, $value) = $self->_split_line($line);
+ $ENV{$key} = $value;
+ }
+ close $fh;
+}
+
+sub _split_line {
+ my ($self, $line) = @_;
+ my ($key, $value) = map { s/^\s+|\s+$//g; $_ } split '=', $line, 2;
+ return $key, $value;
+}
+
+sub usage {
+ my $self = shift;
+print << 'USAGE';
+Usage: evnfile [options] file command
+USAGE
+ exit 1;
+}
+
1;
__END__
@@ -14,15 +54,61 @@ __END__
=head1 NAME
-App::envfile -
+App::envfile - runs another program with environment modified according to envfile
=head1 SYNOPSIS
- use App::envfile;
+ $ cat > envfile
+ FOO=bar
+ HOGE=fuga
+ $ envfile envfile perl -le 'print "$ENV{FOO}, $ENV{HOGE}"'
+ bar, fuga
+
+like
+
+ $ env FOO=bar HOGE=fuga perl -le 'print "$ENV{FOO}, $ENV{HOGE}"'
=head1 DESCRIPTION
-App::envfile is
+App::envfile is sets environment from file.
+
+envfile inspired djb's envdir program.
+
+=head1 METHODS
+
+=over
+
+=item C<< new() >>
+
+Create App::envfile instance.
+
+ my $envf = App::envfile->new();
+
+=item C<< run($envfile, @commands) >>
+
+Runs another program.
+
+ my $exit_code = $envf->run($envfile, @commands);
+
+=item C<< load_envfile($envfile) >>
+
+Sets %ENV from file.
+
+ $envf->load_envfile($envfile);
+
+Supported file format are:
+
+ KEY=VALUE
+ KEY2=VALUE
+ ...
+
+=item C<< usage() >>
+
+Show usage.
+
+ $envf->usage();
+
+=back
=head1 AUTHOR
View
@@ -0,0 +1,9 @@
+use strict;
+use warnings;
+use Test::More;
+
+use_ok 'App::envfile';
+my $envf = new_ok 'App::envfile';
+isa_ok $envf, 'App::envfile';
+
+done_testing;
View
@@ -0,0 +1,93 @@
+use strict;
+use warnings;
+use Test::More;
+use File::Temp qw(tempfile tempdir);
+
+use App::envfile;
+
+sub test_load_envfile {
+ my %specs = @_;
+ my ($input, $expects, $desc) = @specs{qw/input expects desc/};
+
+ local $Test::Builder::Level = $Test::Builder::Level + 1;
+ subtest $desc => sub {
+ local %ENV;
+ my $tempfile = write_envfile($input);
+ my $envf = App::envfile->new;
+ $envf->load_envfile($tempfile);
+ for my $key (sort keys %$expects) {
+ is $ENV{$key}, $expects->{$key}, "$key ok";
+ }
+ };
+}
+
+sub write_envfile {
+ my $input = shift;
+ my (undef, $filename) = tempfile DIR => tempdir CLEANUP => 1;
+ open my $fh, '>', $filename or die "$filename: $!";
+ print $fh $input;
+ close $fh;
+ return $filename;
+}
+
+test_load_envfile(
+ expects => { FOO => 'bar' },
+ desc => 'simple',
+ input => << 'ENV');
+FOO=bar
+ENV
+
+test_load_envfile(
+ expects => { FOO => 'bar', HOGE => 'fuga' },
+ desc => 'multi',
+ input => << 'ENV');
+FOO=bar
+HOGE=fuga
+ENV
+
+test_load_envfile(
+ expects => { FOO => 'bar=baz' },
+ desc => 'contains split charctor',
+ input => << 'ENV');
+FOO=bar=baz
+ENV
+
+test_load_envfile(
+ expects => { 'HOGE FUGA' => 'piyo' },
+ desc => 'key contains space',
+ input => << 'ENV');
+HOGE FUGA=piyo
+ENV
+
+test_load_envfile(
+ expects => { 'FOO' => 'bar baz' },
+ desc => 'value contains space',
+ input => << 'ENV');
+FOO=bar baz
+ENV
+
+test_load_envfile(
+ expects => { 'FOO' => 'bar baz' },
+ desc => 'spaces',
+ input => << 'ENV');
+ FOO = bar baz
+ENV
+
+test_load_envfile(
+ expects => { 'FOO' => 'bar' },
+ desc => 'skip comment',
+ input => << 'ENV');
+# here is comment
+FOO = bar
+ENV
+
+test_load_envfile(
+ expects => { 'FOO' => 'bar' },
+ desc => 'skip white line',
+ input => << 'ENV');
+
+FOO = bar
+
+ENV
+
+done_testing;
View
@@ -31,6 +31,8 @@ all_pod_files_spelling_ok('lib');
__DATA__
App::envfile
+envdir
+djb's
# personal section
xaicron

0 comments on commit 1445850

Please sign in to comment.