Fix case when the terminal isn't installed

Fix the case when the configured terminal isn't actually on the system.  Alert for the problem rather than attempt to continue and hang.
This commit is contained in:
Duncan Ferguson 2014-12-12 17:18:19 +00:00
parent 97ec81692a
commit 9b21aaaa61
2 changed files with 56 additions and 35 deletions

View file

@ -179,12 +179,10 @@ sub validate_args {
);
}
# # Don't search for the path to the binary - assume it is on the path
# # or defined correctly in the config.
# if( !-e $self->{ $self->{comms} } )
# {
# $self->{ $self->{comms} } = $self->find_binary( $self->{comms} );
# }
# check the terminal has been found correctly
if ( !-e $self->{terminal} ) {
$self->{terminal} = $self->find_binary( $self->{terminal} );
}
return $self;
}
@ -387,6 +385,25 @@ sub write_user_config_file {
return $self;
}
# search given directories for the given file
sub search_dirs {
my ( $self, $file, @directories ) = @_;
my $path;
foreach my $dir (@directories) {
$self->debug( 3, "Looking for $file in $dir" );
if ( -f $dir . '/' . $file && -x $dir . '/' . $file ) {
$path = $dir . '/' . $file;
$self->debug( 2, "Found at $path" );
last;
}
}
return $path;
}
# could use File::Which for some of this but we also search a few other places
# just in case $PATH isnt set up right
sub find_binary {
@ -405,39 +422,39 @@ sub find_binary {
# if not found, strip the path and look again
if ( $binary =~ m!^/! ) {
if ( -f $binary ) {
$self->debug( 2, "$binary already fully qualified" );
$self->debug( 2, "Already have full path to in $binary" );
return $binary;
}
else {
$self->debug( 2, "$binary not found - re-searching" );
$self->debug( 2, "Full path for $binary incorrect; searching" );
$binary =~ s!^.*/!!;
}
}
my $path;
if ( !-x $binary || substr( $binary, 0, 1 ) ne '/' ) {
$path = $self->search_dirs( $binary, split( /:/, $ENV{PATH} ) );
foreach (
split( /:/, $ENV{PATH} ), qw!
/bin
/sbin
/usr/sbin
/usr/bin
/usr/local/bin
/usr/local/sbin
/opt/local/bin
/opt/local/sbin
!
)
{
$self->debug( 3, "Looking in $_" );
if ( -f $_ . '/' . $binary && -x $_ . '/' . $binary ) {
$path = $_ . '/' . $binary;
$self->debug( 2, "Found at $path" );
last;
}
# if it is on $PATH then no need to qualitfy the path to it
# keep it as it is
if ($path) {
return $binary;
}
else {
$path = $self->search_dirs(
$binary, qw!
/bin
/sbin
/usr/sbin
/usr/bin
/usr/local/bin
/usr/local/sbin
/opt/local/bin
/opt/local/sbin
!
);
}
}
else {
$self->debug( 2, "Already configured OK" );
@ -517,6 +534,10 @@ Read in configuration from given filename
Validate and apply all configuration loaded at this point
=item $path = $config->search_dirs('<name>', @seaarch_directories);
Search the given directories for the name given. Return undef if not found.
=item $path = $config->find_binary('<name>');
Locate the binary <name> and return the full path. Doesn't just search

View file

@ -251,7 +251,7 @@ isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
is_deeply( $config, \%expected, 'amended config is correct' );
is( $path, which('ls'), 'Found correct path to "ls"' );
is( $path, 'ls', 'Found correct path to "ls"' );
# check for a binary already found
my $newpath;
@ -264,8 +264,8 @@ isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
is_deeply( $config, \%expected, 'amended config is correct' );
is( $path, which('ls'), 'Found correct path to "ls"' );
is( $path, $newpath, 'No change made from find_binary' );
is( $path, 'ls', 'Found correct path to "ls"' );
is( $path, $newpath, 'No change made from find_binary' );
# give false path to force another search
trap {
@ -277,8 +277,8 @@ isa_ok( $config, "App::ClusterSSH::Config" );
is( $trap->stdout, q{}, 'Expecting no STDOUT' );
is( $trap->stderr, q{}, 'Expecting no STDERR' );
is_deeply( $config, \%expected, 'amended config is correct' );
is( $path, which('ls'), 'Found correct path to "ls"' );
is( $path, $newpath, 'No change made from find_binary' );
is( $path, 'ls', 'Found correct path to "ls"' );
is( $path, $newpath, 'No change made from find_binary' );
note('Checks on loading configs');
note('empty dir');
@ -376,9 +376,9 @@ is_deeply( $config, \%expected, 'amended config is correct' );
note('no .csshrc warning, .clusterssh dir plus config + extra config');
open( $csshrc, '>', $ENV{HOME} . '/clusterssh.config' );
print $csshrc 'terminal = something', $/;
print $csshrc 'terminal_args = something', $/;
close($csshrc);
$expected{terminal} = 'something';
$expected{terminal_args} = 'something';
$config = App::ClusterSSH::Config->new();
trap {
$config->load_configs( $ENV{HOME} . '/clusterssh.config' );