App-genconf

 view release on metacpan or  search on metacpan

lib/App/genconf.pm  view on Meta::CPAN

package App::genconf;
BEGIN {
  $App::genconf::AUTHORITY = 'cpan:FFFINKEL';
}
{
  $App::genconf::VERSION = '0.006';
}

#ABSTRACT: The world's simplest config file generator

use strict;
use warnings;

use File::Find;
use Getopt::Long qw/ GetOptions :config bundling /;
use Path::Class qw/ file /;
use Template;
use Try::Tiny;


sub new {
	my ( $class, $inc ) = @_;
	$inc = [@INC] unless ref $inc eq 'ARRAY';
	bless { verbose => 0, }, $class;
}

sub run {
	my ( $self, @args ) = @_;

	local @ARGV = @args;
	GetOptions(
		'v|verbose!'   => sub { ++$self->{verbose} },
		'V|version!'   => \$self->{version},
		'config-dir=s' => \$self->{config_dir},
	) or $self->usage;

	if ( $self->{version} ) {
		$self->puts("genconf (App::genconf) version $App::genconf::VERSION");
		exit 1;
	}

	die 'Must specify template file or directory' unless $ARGV[0];

	if ( -f $ARGV[0] ) {
		$self->_generate_config( $ARGV[0] );
	}
	else {
		my @files;
		find( sub { push @files, $File::Find::name unless -d; }, $ARGV[0] );
		for my $file (@files) {
			$self->_generate_config($file);
		}
	}

}

sub usage {
	my $self = shift;
	$self->puts(<< 'USAGE');
Usage:
  genconf [options] template|dir

  options:
    -v,--verbose                  Turns on chatty output
    --config-dir                  Specify config file directory, default .
USAGE

	exit 1;
}

sub _generate_config {
	my $self     = shift;
	my $template = shift;

	my $template_file = file($template);
	my $filename      = $template_file->basename;

	my $config =
	  $self->{config_dir}
	  ? "$self->{config_dir}/$filename"
	  : $filename;

	my $tt = Template->new( { ABSOLUTE => 1, } ) || die "$Template::ERROR\n";
	$tt->process( $template, \%ENV, $config ) || die $tt->error();

	return 1;
}

1;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 2.312 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-c30982ac1bc3 )