Add config to allow terminals to run in given dir

Add in terminal_chdir and terminal_chdir_path so that when terminals start they are run in a specific directory.

Documentation to be added.

Github issue #42
This commit is contained in:
Duncan Ferguson 2015-11-09 22:46:02 +00:00
parent 3fcc001cca
commit ffe6a9d283
6 changed files with 110 additions and 32 deletions

View file

@ -87,6 +87,7 @@ my $build = $class->new(
'Exception::Class' => '1.31',
'Try::Tiny' => 0,
'Getopt::Long' => 0,
'File::Path' => 0,
},
build_requires => {
'Test::Pod::Coverage' => 0,

View file

@ -1,5 +1,6 @@
4.04_01 0000-00-00 Duncan Ferguson <duncan_ferguson@user.sf.net>
- Ensure documentation is generated using same perl as the build (Github issue #45)
- New config file settings to allow terminals to run from different directories (Github issue #42)
4.04 2015-11-03 Duncan Ferguson <duncan_ferguson@user.sf.net>
- Include bash completion script in distribution (Github issue #29)

View file

@ -37,6 +37,7 @@ use Net::hostent;
use Sys::Hostname;
use English;
use Socket;
use File::Path qw(make_path);
# Notes on general order of processing
#
@ -510,6 +511,38 @@ SWITCH: {
return $self;
}
sub substitute_macros {
my ( $self, $svr, $text ) = @_;
return $text unless ( $self->config->{macros_enabled} eq 'yes' );
{
my $macro_servername = $self->config->{macro_servername};
( my $servername = $svr ) =~ s/\s+//;
$text =~ s!$macro_servername!$servername!xsmg;
}
{
my $macro_hostname = $self->config->{macro_hostname};
my $hostname = $servers{$svr}{givenname};
$text =~ s!$macro_hostname!$hostname!xsmg;
}
{
my $macro_username = $self->config->{macro_username};
my $username = $servers{$svr}{username};
$text =~ s!$macro_username!$username!xsmg;
}
{
my $macro_newline = $self->config->{macro_newline};
$text =~ s!$macro_newline!$/!xsmg;
}
{
my $macro_version = $self->config->{macro_version};
$text =~ s/$macro_version/$VERSION/xsmg;
}
return $text;
}
sub send_text($@) {
my $self = shift;
my $svr = shift;
@ -518,34 +551,36 @@ sub send_text($@) {
$self->debug( 2, "servers{$svr}{wid}=$servers{$svr}{wid}" );
$self->debug( 3, "Sending to '$svr' text:$text:" );
# command macro substitution
if ( $self->config->{macros_enabled} eq 'yes' ) {
$text = $self->substitute_macros( $svr, $text );
# $svr contains a trailing space here, so ensure its stripped off
{
my $macro_servername = $self->config->{macro_servername};
my $servername = $svr;
$servername =~ s/\s+//;
$text =~ s/$macro_servername/$servername/xsmg;
}
$text =~ s/%h/hostname()/xsmeg;
# use connection username, else default to current username
{
my $macro_username = $self->config->{macro_username};
my $username = $servers{$svr}{username};
$username ||= getpwuid($UID);
$text =~ s/$macro_username/$username/xsmg;
}
{
my $macro_newline = $self->config->{macro_newline};
$text =~ s/$macro_newline/\n/xsmg;
}
{
my $macro_version = $self->config->{macro_version};
$text =~ s/$macro_version/$VERSION/xsmg;
}
}
# # command macro substitution
# if ( $self->config->{macros_enabled} eq 'yes' ) {
#
# # $svr contains a trailing space here, so ensure its stripped off
# {
# my $macro_servername = $self->config->{macro_servername};
# my $servername = $svr;
# $servername =~ s/\s+//;
# $text =~ s/$macro_servername/$servername/xsmg;
# }
# $text =~ s/%h/hostname()/xsmeg;
#
# # use connection username, else default to current username
# {
# my $macro_username = $self->config->{macro_username};
# my $username = $servers{$svr}{username};
# $username ||= getpwuid($UID);
# $text =~ s/$macro_username/$username/xsmg;
# }
# {
# my $macro_newline = $self->config->{macro_newline};
# $text =~ s/$macro_newline/\n/xsmg;
# }
# {
# my $macro_version = $self->config->{macro_version};
# $text =~ s/$macro_version/$VERSION/xsmg;
# }
# }
foreach my $char ( split( //, $text ) ) {
next if ( !defined($char) );
@ -724,6 +759,28 @@ sub open_client_windows(@) {
# Since this is the child, we can mark any server unresolved without
# affecting the main program
$servers{$server}{realname} .= "==" if ( !$realname );
# If set, use the chdir path
if ( $self->config->{terminal_chdir} ) {
my $chdir_path = $self->substitute_macros( $server,
$self->config->{terminal_chdir_path} );
if ( !-d $chdir_path ) {
$self->debug( 1,
"Creating terminal directory path '$chdir_path'" );
make_path($chdir_path)
|| $self->debug( 0,
"WARNING: Could not create '$chdir_path'" );
}
$self->debug( 1,
"Changing directory to $chdir_path for terminal to $given_server_name"
);
chdir($chdir_path)
|| $self->debug( 0,
"WARNING: Could not change directory to '$chdir_path': $!"
);
}
my $exec = join( ' ',
$self->config->{terminal},
$color,
@ -1681,11 +1738,14 @@ sub key_event {
$self->debug( 3, "matched combo" );
if ( $event eq "KeyRelease" ) {
$self->debug( 2, "Received hotkey: $hotkey" );
$self->send_text_to_all_servers('%s')
$self->send_text_to_all_servers(
$self->config->{macro_servername} )
if ( $hotkey eq "key_clientname" );
$self->send_text_to_all_servers('%h')
$self->send_text_to_all_servers(
$self->config->{macro_hostname} )
if ( $hotkey eq "key_localname" );
$self->send_text_to_all_servers('%u')
$self->send_text_to_all_servers(
$self->config->{macro_username} )
if ( $hotkey eq "key_username" );
$self->add_host_by_name()
if ( $hotkey eq "key_addhost" );
@ -2226,6 +2286,8 @@ the code until this time.
=item show_history
=item substitute_macros
=item terminate_host
=item toggle_active_state

View file

@ -63,6 +63,9 @@ my %default_config = (
terminal_decoration_height => 10,
terminal_decoration_width => 8,
terminal_chdir_path => $ENV{HOME} . '/.clusterssh/work/%s',
terminal_chdir => 0,
console => 'console',
console_args => '',
rsh => 'rsh',

View file

@ -63,6 +63,7 @@ sub script {
my \$port=shift;
my \$mstr=shift;
my \$command="$comms $comms_args ";
my \$cwd=qx!pwd!;
open(PIPE, ">", \$pipe) or die("Failed to open pipe: \$!\\n");
print PIPE "\$\$:\$ENV{WINDOWID}"
or die("Failed to write to pipe: $!\\n");
@ -99,7 +100,10 @@ sub script {
\$command .= " \\\"$config_command\\\"";
}
\$command .= " ; $postcommand";
warn("Running:\$command\\n"); # for debug purposes
# provide some info for debugging purposes
warn("Running: \\n");
warn(" pwd: \$cwd"); # already has newline
warn(" cmd: \$command\\n");
exec(\$command);
HERE

View file

@ -28,6 +28,8 @@ isa_ok( $config, 'App::ClusterSSH::Config' );
Readonly::Hash my %default_config => {
terminal => "xterm",
terminal_chdir => 0,
terminal_chdir_path => $ENV{HOME} . '/.clusterssh/work/%s',
terminal_args => "",
terminal_title_opt => "-T",
terminal_colorize => 1,
@ -517,7 +519,10 @@ is( $trap->stderr,
note('Checking dump');
$config = App::ClusterSSH::Config->new(
send_menu_xml_file => $ENV{HOME} . '/.clusterssh/send_menu' );
send_menu_xml_file => $ENV{HOME} . '/.clusterssh/send_menu',
terminal_chdir_path => $ENV{HOME} . '/.clusterssh/work/%s',
);
trap {
$config->dump();
};
@ -569,6 +574,8 @@ terminal=xterm
terminal_allow_send_events=-xrm '*.VT100.allowSendEvents:true'
terminal_args=
terminal_bg_style=dark
terminal_chdir=0
terminal_chdir_path=} . $ENV{HOME} . qq{/.clusterssh/work/%s
terminal_colorize=1
terminal_decoration_height=10
terminal_decoration_width=8