App-Rcsync

 view release on metacpan or  search on metacpan

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

package App::Rcsync;
{
  $App::Rcsync::VERSION = '0.03';
}

# ABSTRACT: Sync configuration files across machines

use strict;
use warnings;

use File::HomeDir;
use Template;
use Config::General;
use File::Path     qw(make_path);
use Path::Class    qw(file dir);
use File::Copy     qw(copy);
use Ref::Explicit  qw(hashref);

use base qw(App::Cmd::Simple);

sub opt_spec
{
    return (
        [ "help|h",     "display usage information"      ],
        [ "config|c=s", "configuration file to use",
            { default => file( File::HomeDir->my_home, '.rcsync' ) }
        ],
        [ "all|a",      "sync all profiles"              ],
        [ "list|l",     "list all profiles"              ],
        [ "init|i",     "create configuration file"      ],
        [ "which|w",    "print path to profile template" ],
        [ "stdout|s",   "print to STDOUT"                ],
    );
}

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

    if ( !$opt->{init} and ! -e $opt->{config} )
    {
        $self->usage_error("Configuration file " . $opt->{config} . " not found, aborting");
    }

    if ( !$opt->{help} and !$opt->{init} and !$opt->{all} and !$opt->{list} and !@$args )
    {
        $self->usage_error("Please specify profiles to sync");
    }
}

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

    if ( $opt->{init} )
    {
        my $home = File::HomeDir->my_home;
        my $rcsync_home = dir $home, 'rcsync';

        if ( -e $opt->{config} )
        {
            print "Configuration file $$opt{config} already exists, will not overwrite\n";
            return;
        }

        make_path $rcsync_home unless -e $rcsync_home;

        my $sample_config = file ($rcsync_home, 'rcsync');

        if ( -e ( my $sample_config = file ($rcsync_home, 'rcsync') ) )
        {
            copy( $sample_config, $opt->{config} )
                or die "Failed to copy $sample_config to $$opt{config}: $!";
            print "Created configuration file $$opt{config} as copy of $sample_config";
        }
        else
        {
            my @children = $rcsync_home->children( no_hidden => 1 );
            @children = file ('sample.tt') unless @children;

            my @templates;

            foreach my $template (@children)
            {
                my $basename = $template->basename;
                $basename =~ s/\.\w+$//;

                push @templates, {
                    name        => $basename,
                    base_name   => $template->basename,
                    deploy_path => file ( $home, ".$basename" ),
                };
            }

            my $tt = Template->new or die Template->error;
            $tt->process(



( run in 1.383 second using v1.01-cache-2.11-cpan-22024b96cdf )