-
Notifications
You must be signed in to change notification settings - Fork 8
/
bcvi
executable file
·1551 lines (1158 loc) · 36.7 KB
/
bcvi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
##############################################################################
#
# Script: bcvi
#
# The 'Back-Channel vim' tool works with SSH to allow commands which are run
# on an SSH server to invoke processes back on the originating SSH client
# machine.
#
# Use 'bcvi --help' for the documentation
#
# Copyright (c) 2007-2010 Grant McLean <grantm@cpan.org>
#
use strict;
use warnings;
require 5.008;
##############################################################################
# This package implements some common functionality required by both the
# client and the server.
#
# It also serves as the entry-point for the command-line script.
##############################################################################
package App::BCVI;
use File::Spec;
use File::Path;
use Getopt::Long qw();
use Encode qw(encode decode);
use IO::Socket::INET;
my %class_map = (
base => 'App::BCVI',
client => 'App::BCVI::Client',
server => 'App::BCVI::Server',
pod => 'App::BCVI::POD',
);
my %response_message = (
100 => "Ready ($App::BCVI::VERSION)",
200 => "Success",
300 => "Response follows",
900 => "Permission denied",
910 => "Unrecognised command",
);
my $LF = "\x0A";
my(
%options, %option_name, %commands, @aliases, @installables,
%plugin_loaded, @plugins,
);
run(@ARGV) unless caller(); # Don't run anything if loaded via 'require'
sub run {
App::BCVI->base_init();
App::BCVI->load_plugins();
App::BCVI->base_class()->process_command_line(@_);
exit;
}
sub version { return $App::BCVI::VERSION; }
sub base_class { return $class_map{base}; }
sub client_class { return $class_map{client}; }
sub server_class { return $class_map{server}; }
sub pod_class { return $class_map{pod}; }
sub map_class { $class_map{$_[1]} = $_[2]; }
sub sock { shift->{sock}; }
sub installable_files { return @installables; }
sub message_from_code { $response_message{$_[1]}; }
sub base_init {
my($class) = @_;
$class->register_option(
name => 'help',
alias => '?',
dispatch_to => 'show_help',
summary => 'detailed help message',
description => <<'END_POD'
Display this documentation.
END_POD
);
$class->register_option(
name => 'add-aliases',
dispatch_to => 'add_aliases',
summary => 'install bcvi into shell startup files',
description => <<'END_POD'
Edit the bash startup script to add (or update) the required command aliases
for bcvi.
END_POD
);
$class->register_option(
name => 'listener',
alias => 'l',
dispatch_to => 'start_listener',
summary => 'start in listener mode',
description => <<'END_POD'
Start a background listener process. Also generates a new authentication key.
END_POD
);
$class->register_option(
name => 'install',
arg_name => '<hosts>',
dispatch_to => 'install_to_hosts',
summary => 'copy bcvi to named hosts and install aliases',
description => <<'END_POD'
Copy the C<bcvi> script to the C<$HOME/bin> directory on the specified host
and then remotely invoke it with the C<--add-aliases> option.
END_POD
);
$class->register_option(
name => 'unpack-term',
dispatch_to => 'unpack_term',
summary => 'unpack the overloaded TERM variable',
description => <<'END_POD'
This option is intended for use from a F<.profile> script. It outputs a
snippet of shell script to be passed to C<eval> in the calling shell.
END_POD
);
$class->register_option(
name => 'wrap-ssh',
alias => 's',
dispatch_to => 'wrap_ssh',
summary => 'pass all args after -- to ssh',
description => <<'END_POD'
A wrapper around invoking ssh to connect to a specified host. Ensures the
environment is set up to pass the authentication key and other data to the
C<bcvi> client on the remote server.
END_POD
);
$class->register_option(
name => 'version',
alias => 'v',
dispatch_to => 'show_versions',
summary => 'display bcvi version number',
description => <<'END_POD'
When invoking a command use this option to indicate that the arguments are not
filenames and the translation of relative pathnames to absolute should be
skipped.
END_POD
);
$class->register_option(
name => 'no-path-xlate',
alias => 'n',
summary => 'skip translation of args from relative to absolute',
description => <<'END_POD'
Displays the version number of the bcvi client and if applicable, of the
listener process.
END_POD
);
$class->register_option(
name => 'port',
alias => 'p',
arg_spec => '=i',
arg_name => '<port>',
summary => 'port number for listener/port-forward',
description => <<'END_POD'
When used with C<--listener>, this is the port the server process will listen
on. When used with C<--wrap-ssh> this is the port number on the remote machine
that will be forwarded back to the listener process. The default in both cases
is calculated using the user's numeric UID multiplied by 10 and added to 9.
The intent is to reduce the chance of collisions with other bcvi users.
END_POD
);
$class->register_option(
name => 'command',
alias => 'c',
arg_spec => '=s',
arg_name => '<cmnd>',
summary => 'command to send over back-channel',
description => <<'END_POD'
Use C<cmnd> as the command to send over the back-channel (default: vi).
Recognised commands are described in L<COMMANDS> below.
END_POD
);
$class->register_option(
name => 'reuse-auth',
summary => "don't generate a new auth key on listener startup",
description => <<'END_POD'
A new (random) authorisation key is generated when the listener process is
started - this will invalidate the keys in use by existing SSH sessions.
This option is for use when it is necessary to restart the listener process
without invalidating client keys.
END_POD
);
$class->register_option(
name => 'plugin-help',
arg_spec => '=s',
arg_name => '<plugin>',
dispatch_to => 'plugin_help',
summary => "display documentation for <plugin>",
description => <<'END_POD'
The --help output includes a list of installed plugins. Use this option to
read the documentation for a named plugin.
END_POD
);
$class->register_command(
name => 'vi',
description => <<'END_POD'
Invokes C<gvim> on the remote file - after translating the host+path to
an scp URI. This is the default command if no C<--command> option is
specified. If multiple filenames are supplied, the first will be opened
in gvim and you should use C<:n> to load the 'next' file.
END_POD
);
$class->register_command(
name => 'viwait',
description => <<'END_POD'
This command works exactly the same as C<vi> above, except it waits for the
editor process to exit before bcvi exits on the remote machine. This is
primarily for use with C<sudoedit>. Note: when used with C<sudoedit>, the file
will not be updated on the remote machine until you exit the editor on your
workstation.
END_POD
);
$class->register_command(
name => 'scpd',
description => <<'END_POD'
Uses C<scp> to copy the specified files or directories to the calling user's
F<~/Desktop>.`
END_POD
);
$class->add_home_bin();
$class->register_aliases(
'test -n "$(which bcvi)" && eval "$(bcvi --unpack-term)"',
'test -n "${BCVI_CONF}" && alias vi="bcvi"',
'test -n "${BCVI_CONF}" && alias suvi="EDITOR=\'bcvi -c viwait\' sudoedit"',
'test -n "${BCVI_CONF}" && alias bcp="bcvi -c scpd"',
);
$class->pod_class->init();
}
sub register_option {
my $class = shift;
my $opt = { @_ };
my $key = $opt->{name};
if(!defined $key or !length $key) {
die "Can't register option without 'name'";
}
my($package, $filename, $line) = caller();
$opt->{provider} = "$package at $filename line $line";
my $taken = $options{$key};
if($taken && !$opt->{force_override}) {
warn "option '--$key' already registered by $taken->{provider}\n";
}
if($opt->{alias}) {
foreach my $a (map { s/^-+//; $_ } split /\|/, $opt->{alias}) {
if($option_name{$a} && !$opt->{force_override}) {
if($taken = $options{$option_name{$a}}) {
warn "alias '$a' already registered for option "
. "'--$taken->{name}' by $taken->{provider}\n";
}
}
$option_name{$a} = $key;
}
}
$options{$key} = $opt;
}
sub register_command {
my $class = shift;
my $cmd = { @_ };
my $key = $cmd->{name};
if(!defined $key or !length $key) {
die "Can't register command without 'name'";
}
$cmd->{dispatch_to} ||= "execute_$key";
my($package, $filename, $line) = caller();
$cmd->{provider} = "$package at $filename line $line";
warn "option '$key' already registered by $commands{$key}->{provider}\n"
if $commands{$key} && !$cmd->{force_override};
$commands{$key} = $cmd;
}
sub each_option {
my($class, $sub) = @_;
$sub->($options{$_}) foreach sort keys %options;
}
sub each_command {
my($class, $sub) = @_;
$sub->($commands{$_}) foreach sort keys %commands;
}
sub command_handler {
my($class, $name) = @_;
return unless defined $name;
return "execute_commands_pod" if $name eq 'commands_pod';
my $spec = $commands{$name} or return;
return $spec->{dispatch_to};
}
sub add_home_bin {
my $class = shift;
my $home_bin = $class->home_directory . '/bin';
$class->register_aliases(
qq{echo \$PATH | grep -q $home_bin || PATH="$home_bin/\$PATH"},
);
}
sub register_aliases {
my $class = shift;
push @aliases, @_;
}
sub register_installable {
my $class = shift;
my($package, $filename, $line) = caller();
push @installables, $filename;
}
sub shell_aliases {
my($self) = @_;
return
"## START-BCVI\n"
. join("\n", map { " $_" } @aliases)
. "\n## END-BCVI\n";
}
sub load_plugins {
my($class) = @_;
my $dir = $class->conf_directory();
foreach my $file (sort glob("$dir/*.pm")) {
$class->load_plugin_file($file);
}
}
sub load_plugin_file {
my($class, $file) = @_;
my @parts = File::Spec->splitdir($file);
my $key = pop @parts;
return if $plugin_loaded{$key};
eval { require $file; };
if($@) {
die qq{Error loading plugin "$file"\n$@\n}
}
$plugin_loaded{$key} = $file;
}
sub hook_client_class {
my($class) = @_;
my($calling_class, $calling_file) = caller();
my $client_class = $class->client_class();
$class->map_class(client => $calling_class);
no strict 'refs';
unshift @{"${calling_class}::ISA"}, $client_class;
push @plugins, { class => $calling_class, file => $calling_file };
return 1;
}
sub hook_server_class {
my($class) = @_;
my($calling_class, $calling_file) = caller();
my $server_class = $class->server_class();
$class->map_class(server => $calling_class);
no strict 'refs';
unshift @{"${calling_class}::ISA"}, $server_class;
push @plugins, { class => $calling_class, file => $calling_file };
return 1;
}
sub process_command_line {
my($class, @args) = @_;
my $opt = $class->option_defaults();
my @cfg = $class->getopt_config();
local(@ARGV) = @args;
Getopt::Long::GetOptions($opt, @cfg) or $class->die_synopsis();
my $handler = $opt->{listener}
? $class->server_class
: $class->client_class;
$handler->new(_options => $opt)->dispatch(@ARGV);
}
sub option_defaults {
return { };
}
sub getopt_config {
my($class) = @_;
my @spec;
$class->each_option(sub {
my($o) = @_;
my $def = $o->{name};
$def .= "|$o->{alias}" if defined $o->{alias};
$def .= $o->{arg_spec} if defined $o->{arg_spec};
push @spec, $def;
});
return @spec
}
sub die_synopsis {
my($class, $message) = @_;
warn "$message\n" if $message;
$class->pod_class->synopsis();
exit(1);
}
sub new {
my $class = shift;
return bless { @_ }, $class;
}
sub dispatch {
my($self, @args) = @_;
if(my $method = $self->dispatch_option) {
$self->$method(@args);
exit;
}
elsif(!$self->opt('command') and !@args) {
$self->die_synopsis();
}
$self->send_command(@args);
}
sub opt {
my($self, $key) = @_;
return $self->{_options}->{$key};
}
sub set_opt {
my($self, $key, $value) = @_;
return $self->{_options}->{$key} = $value;
}
sub dispatch_option {
my($self) = @_;
my @set;
$self->each_option(sub {
my($o) = @_;
push @set, $o if $o->{dispatch_to} && defined $self->opt($o->{name});
});
return unless @set;
if(@set > 1) {
@set = map { "--$_->{name}" } @set;
my $last = pop @set;
$self->die_synopsis(
"Which did you want: " . join(', ', @set) . " or $last?"
);
}
return $set[0]->{dispatch_to};
}
sub default_port {
return( ($< * 10 + 9) % 65536 );
}
sub listen_address {
return 'localhost';
};
sub default_command {
return 'vi';
}
sub read_file {
my($self, $path) = @_;
return unless -e $path;
return if -d $path;
my $data = do {
open my $fh, '<', $path or die "open($path): $!\n";
local($/) = undef;
<$fh>;
};
return $data;
}
sub home_directory {
return (getpwuid($>))[7];
}
sub conf_directory {
my($self) = @_;
my $conf_dir = File::Spec->catdir($self->home_directory(), '.config', 'bcvi');
File::Path::mkpath($conf_dir) unless -d $conf_dir;
return $conf_dir;
}
sub auth_key_filename {
return File::Spec->catfile(shift->conf_directory(), 'listener_key');
}
sub listener_port_filename {
return File::Spec->catfile(shift->conf_directory(), 'listener_port');
}
sub get_listener_auth_key {
my($self) = @_;
my $auth_file = $self->auth_key_filename();
my $auth_key = $self->read_file($auth_file)
or die "Auth key file does not exist: $auth_file";
chomp($auth_key);
return $auth_key;
}
sub list_plugins {
my($self) = @_;
my @plugins;
foreach my $name (sort keys %plugin_loaded) {
my $path = $plugin_loaded{$name};
$name =~ s{[.]pm$}{};
if(my $title = $self->pod_class->extract_title($path)) {
push @plugins, "$name - $title";
}
else {
push @plugins, "$name - no documentation";
}
}
return @plugins;
}
sub plugin_help {
my($self) = @_;
my $plugin = $self->opt('plugin-help');
if(my $path = $plugin_loaded{"${plugin}.pm"}) {
$self->pod_class->show_plugin_help($path);
}
else {
die "Can't find plugin: '$plugin'\n";
}
}
##############################################################################
# The App::BCVI::Client class implements the command-line UI and the client
# side of the BCVI protocol. You can use inheritance to customise the
# behaviour of this class.
##############################################################################
package App::BCVI::Client;
BEGIN { @App::BCVI::Client::ISA = qw( App::BCVI ); }
sub get_connection_details {
my($self) = @_;
if(not $ENV{BCVI_CONF}) {
die "The \$BCVI_CONF environment variable has not been set\n";
}
my($alias, $gateway, $port, $auth_key) = split(/:/, $ENV{BCVI_CONF});
$self->{host_alias} = $alias;
$self->{gateway_address} = $gateway;
$self->{port} = $port;
$self->{auth_key} = $auth_key;
}
sub host_alias { return shift->{host_alias}; }
sub gateway_address { return shift->{gateway_address}; }
sub port { return shift->{port}; }
sub auth_key { return shift->{auth_key}; }
sub server_version { return shift->{server_version}; }
sub response_code { return shift->{response_code}; }
sub response_message { return shift->{response_message}; }
sub response { return shift->{response}; }
sub send_command {
my($self, @files) = @_;
my $command = $self->opt('command') || $self->default_command();
my $sock = $self->connect_to_listener();
$sock->write(
"Auth-Key: " . $self->auth_key . $LF .
"Host-Alias: " . $self->host_alias . $LF .
"Command: " . $command . $LF
) or die "Error sending command through backchannel: $!";
$self->send_body(\@files);
return if $self->check_response() =~ /^(?:200|300)$/;
die $self->response_message . "\n";
}
sub send_body {
my($self, $files) = @_;
my $translate_paths = ! $self->opt('no-path-xlate');
my $body = join '', map {
$_ = File::Spec->rel2abs($_) if $translate_paths;
"$_$LF";
} @$files;
$self->sock->write(
'Content-Length: ' . length($body) . $LF .
$LF .
$body
) or die "Error sending command through backchannel: $!";
}
sub check_response {
my($self) = @_;
my $response = $self->sock->getline() or die "Server hung up\n";
if(my($code, $message) = $response =~ m{^(\d\d\d) (.*)$}) {
($self->{response_code}, $self->{response_message}) = ($code, $message);
$self->read_response() if $code eq '300';
return $code;
}
die "Unexpected response: '$response'\n";
}
sub read_response {
my($self) = @_;
my $sock = $self->sock();
my $resp = {};
while(my($line) = $sock->getline() || '') {
chomp($line);
last if $line eq '';
$line = Encode::decode('utf8', $line);
if(my($name, $value) = $line =~ m{^(\S+)\s*:\s*(.*)$}) {
$name =~ s/-/_/;
$resp->{lc($name)} = $value;
}
}
$self->{response} = $resp;
my $bytes = $resp->{content_length} || return;;
my $buf = '';
while(my $count = $sock->read($buf, $bytes, length($buf))) {
$bytes -= $count;
last if $bytes < 1;
}
$resp->{body} = $buf;
}
sub connect_to_listener {
my($self) = @_;
$self->get_connection_details();
my $peer = $self->gateway_address . ':' . $self->port;
my $sock = IO::Socket::INET->new(
PeerAddr => $peer,
) or die "Can't connect to '$peer': $!\n";
binmode($sock);
my $welcome = $sock->getline() or die "No listener?\n";
if($welcome =~ /^100 Ready \(([^)]+)\)/) {
$self->{server_version} = $1;
}
$self->{sock} = $sock;
}
sub show_versions {
my($self) = @_;
print "bcvi client: $App::BCVI::VERSION\n";
if($ENV{BCVI_CONF}) {
$self->connect_to_listener();
print "bcvi server: $App::BCVI::VERSION\n";
}
}
sub show_help {
my($self) = @_;
$self->pod_class->full_pod($self);
}
sub commands_pod {
my($self) = @_;
eval {
$self->set_opt(command => 'commands_pod');
$self->send_command();
};
if($@) {
$@ = '';
return;
}
return $self->response->{body};
}
sub wrap_ssh {
my($self, @args_in) = @_;
if(my($target, @args_out) = $self->parse_ssh_args(@args_in)) {
$self->execute_wrapped_ssh($target, @args_out);
}
else {
warn "bcvi: unable to extract hostname from ssh command line\n";
$self->execute_ssh(@args_in);
}
}
sub parse_ssh_args {
my($self, @args_in) = @_;
my %need_arg = map { $_ => 1} split //, 'bcDeFiLlmOopRS';
my(@args_out, @hosts, $user);
while(@args_in) {
$_ = shift @args_in;
if(/^-l(.*)$/) { # extract username if specified with -l
$user = $1 ? $1 : $args_in[0];
}
if(/^-(.)(.*)$/) {
push @args_out, $_;
push @args_out, shift @args_in
if $need_arg{$1} && !length($2) && @args_in;
}
else {
push @args_out, $_;
push @hosts, $_;
}
}
return unless @hosts == 1;
my($target) = @hosts;
if($user and $target !~ /@/) {
$target = $user . '@' . $target;
}
return($target, @args_out);
}
sub execute_wrapped_ssh {
my($self, $target, @args) = @_;
my $remote_port = $self->opt('port') || $self->default_port();
my $local_port = $self->listener_port();
$ENV{TERM} = "$ENV{TERM}\n"
. "BCVI_CONF=${target}:localhost:$remote_port:"
. $self->get_listener_auth_key();
unshift @args, "-R $remote_port:localhost:$local_port";
$self->execute_ssh(@args);
}
sub execute_ssh {
my($self, @args) = @_;
system 'ssh', @args;
}
sub listener_port {
my($self) = @_;
my $port = $self->read_file($self->listener_port_filename())
or return $self->default_port();
chomp($port);
return $port;
}
sub unpack_term {
my($self) = @_;
my @parts = split /\x0D?\x0A/, $ENV{TERM} || '';
return unless @parts > 1;
print "TERM=$parts[0]\n";
shift @parts;
foreach (@parts) {
print if s{^(\w+)=(.*)$}{export $1="$2"\n};
}
}
sub install_to_hosts {
my($self, @args) = @_;
die "You must list one or more target hostnames\n" unless @args;
$self->install_to_host($_) foreach @args;
}
sub install_to_host {
my($self, $host) = @_;
$self->install_bin_directory($host);
$self->install_bcvi_script($host);
$self->install_plugins($host);
$self->install_remote_aliases($host);
}
sub install_bin_directory {
my($self, $host) = @_;
if(system("ssh $host test -d ./bin") != 0) {
print "Creating ~/bin directory on $host\n";
system("ssh $host mkdir ./bin") == 0
or die "** mkdir failed on $host";
}
}
sub install_bcvi_script {
my($self, $host) = @_;
print "Copying bcvi to remote bin directory on $host\n";
my $output = `scp $0 $host:bin/bcvi 2>&1`;
if($? != 0) {
die "** failed to copy bcvi to remote bin directory on $host\n"
. $output;
}
}
sub install_plugins {
my($self, $host) = @_;
return unless @installables;
if(system("ssh $host test -d ./.config/bcvi") != 0) {
print "Creating plugins directory on $host\n";
system("ssh $host mkdir -p ./.config/bcvi") == 0
or die "** mkdir failed on $host";
}
print "Copying plugin files to $host\n";
my $output = `scp @installables $host:.config/bcvi 2>&1`;
if($? != 0) {
die "** failed to copy bcvi to remote plugins directory on $host\n"
. $output;
}
}
sub install_remote_aliases {
my($self, $host) = @_;
system("ssh $host bin/bcvi --add-aliases") == 0
or die "** failed to install aliases on $host";
}
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 {