App-BCVI

 view release on metacpan or  search on metacpan

bin/bcvi  view on Meta::CPAN

    print "Copying bcvi to remote bin directory on $host\n";
    my $output = `scp $0 $host:bin/bcvi 2>&1`;
    if($? != 0) {
        warn "** failed to copy bcvi to remote bin directory on $host\n"
             . $output;
        return;
    }
    return 1;
}


sub install_plugins {
    my($self, $host) = @_;

    return 1 unless @installables;
    if(system("ssh $host test -d ./.config/bcvi") != 0) {
        print "Creating plugins directory on $host\n";
        if( system("ssh $host mkdir -p ./.config/bcvi") != 0 ) {
            warn "** mkdir of .config/bcvi failed on $host\n";
            return;
        }
    }
    print "Copying plugin files to $host\n";
    my $output = `scp @installables $host:.config/bcvi 2>&1`;
    if($? != 0) {
        warn "** failed to copy bcvi to remote plugins directory on $host\n"
             . $output;
        return;
    }
    return 1;
}


sub install_remote_aliases {
    my($self, $host) = @_;

    if( system("ssh $host bin/bcvi --add-aliases") != 0 ) {
        warn "** failed to install aliases on $host\n";
        return;
    }
    return 1;
}


sub report_install_failure {
    my($self, $host) = @_;
    warn "\n*** Installation of bcvi on host '$host' failed ***\n\n";
}


sub add_aliases {
    my($self) = @_;

    my $bcvi_commands = $self->shell_aliases();

    $self->update_existing_aliases($bcvi_commands)
    or $self->aliases_initial_install($bcvi_commands);
}


sub update_existing_aliases {
    my($self, $bcvi_commands) = @_;

    foreach my $file ( $self->candidate_rc_files() ) {
        my($script) = $self->read_file($file) or next;
        if(index($script, $bcvi_commands) > -1) {
            print "Found bcvi commands in $file\n";
            return 1;
        }
        if($script =~ s{^## START-BCVI.*^## END-BCVI\r?\n}{$bcvi_commands}sm) {
            open my $fh, '>', $file or die "open($file): $!";
            print $fh $script;
            close($fh);
            print "Updated bcvi commands in $file\n";
            return 1;
        }
        if($script =~ m{^[^#]*\bbcvi\b}m) {
            print "Adhoc bcvi commands found in $file\n"
                  . "*** Manual update may be required.            ***\n"
                  . "*** Consider deleting commands and re-adding. ***\n";
            return 1;
        }
    }
    return;   # No existing aliases found
}


sub aliases_initial_install {
    my($self, $bcvi_commands) = @_;

    my $target = $self->preferred_rc_file();

    open my $fh, '>>', $target or die "open(>>$target): $!";
    print $fh "\n$bcvi_commands\n";
    close($fh);
    print "Added bcvi commands to $target\n";
}


sub candidate_rc_files {
    my($self) = @_;
    my $home = $self->home_directory();
    return(
        "$home/.bashrc_local",
        "$home/.bashrc",
        "$home/.bash_profile_local",
        "$home/.bash_profile",
        "$home/.profile",
        "$home/.common-configs/bashrc",
    );
}


sub preferred_rc_file {
    my($self) = @_;

    # Add to .bashrc_local if it is referenced from .bashrc

    my $home = $self->home_directory();
    my $bashrc       = "$home/.bashrc";
    my $bashrc_local = "$home/.bashrc_local";



( run in 1.777 second using v1.01-cache-2.11-cpan-9581c071862 )