X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=scripts%2Fconsole;h=8ce978346329df8645bc85bd65d879012b211a8f;hb=7b195af14e0c5a01f86d7dccdd72a3b14ee52430;hp=21644c8c736d7b6adcea737e53ee8a4dc15e5c72;hpb=3e2ecde78dba6b3ab866a4ad044e0e748edede82;p=kivitendo-erp.git diff --git a/scripts/console b/scripts/console index 21644c8c7..8ce978346 100755 --- a/scripts/console +++ b/scripts/console @@ -11,29 +11,65 @@ BEGIN { use Data::Dumper; use Devel::REPL 1.002001; +use File::Slurp; +use Getopt::Long; +use Pod::Usage; use Term::ReadLine::Perl::Bind; # use sane key binding for rxvt users use SL::LxOfficeConf; SL::LxOfficeConf->read; -my $login = shift || $::lx_office_conf{console}{login} || 'demo'; -my $history_file = $::lx_office_conf{console}{history_file} || '/tmp/lxoffice_console_history.log'; # fallback if users is not writable -my $debug_file = $::lx_office_conf{console}{log_file} || '/tmp/lxoffice_console_debug.log'; -my $autorun = $::lx_office_conf{console}{autorun}; +my $client = $::lx_office_conf{console}{client}; +my $login = $::lx_office_conf{console}{login} || 'demo'; +my $history_file = $::lx_office_conf{console}{history_file} || '/tmp/kivitendo_console_history.log'; # fallback if users is not writable +my $debug_file = $::lx_office_conf{console}{log_file} || '/tmp/kivitendo_console_debug.log'; +my $autorun = $::lx_office_conf{console}{autorun}; +my ($execute_code, $execute_file, $help, $man); + +my $result = GetOptions( + "login|l=s" => \$login, + "client|c=s" => \$client, + "history-file|i=s" => \$history_file, + "log-file|o=s" => \$debug_file, + "execute|e=s" => \$execute_code, + "file|f=s" => \$execute_file, + "help|h" => \$help, + "man" => \$man, +); +pod2usage(2) if !$result; +pod2usage(1) if $help; +pod2usage(-exitstatus => 0, -verbose => 2) if $man; # will be configed eventually my @plugins = qw(History LexEnv Colors MultiLine::PPI FancyPrompt PermanentHistory AutoloadModules); +sub execute_code { + my ($repl, $code) = @_; + + my $result = $repl->eval($code); + if (ref($result) eq 'Devel::REPL::Error') { + $repl->print($result->message); + return 0; + } + if ($@) { + $repl->print($@); + return 0; + } + + return 1; +} + my $repl = Devel::REPL->new; $repl->load_plugin($_) for @plugins; $repl->load_history($history_file); $repl->eval('help'); -$repl->print("trying to auto login as '$login'..."); -$repl->print($repl->eval("lxinit '$login'")); -if ($autorun) { - my $result = $repl->eval($autorun); - $repl->print($result->message) if ref($result) eq 'Devel::REPL::Error'; -} +$repl->print("trying to auto login into client '$client' with login '$login'...\n"); +execute_code($repl, "lxinit '$client', '$login'"); + +my @code_to_execute = grep { $_ } ($autorun, $execute_code, $execute_file ? join('', read_file($execute_file)) : undef); +execute_code($repl, $_) || exit 1 for @code_to_execute; +exit if $execute_code || $execute_file; + $repl->run; package Devel::REPL; @@ -55,9 +91,9 @@ use List::Util qw(max); # it is assumed that anyone with physical access and execution rights on this script # won't be hindered by authentication anyway. sub lxinit { - my $login = shift; + my ($client, $login) = @_; - die 'need login' unless $login; + die 'need client and login' unless $client && $login; package main; @@ -65,6 +101,8 @@ sub lxinit { $::locale = Locale->new($::lx_office_conf{system}->{language}); $::form = Form->new; $::auth = SL::Auth->new; + die "Cannot find client with ID or name '$client'" if !$::auth->set_client($client); + $::instance_conf = SL::InstanceConfiguration->new; $::request = SL::Request->new( cgi => CGI->new({}), @@ -180,9 +218,51 @@ scripts/console - kivitendo console =head1 SYNOPSIS - ./script/console + ./script/console [options] > help # displays a brief documentation +=head1 OPTIONS + +The list of supported command line options includes: + +=over 8 + +=item B<--help>, B<-h> + +Print this help message and exit. + +=item B<--man> + +Print the manual page and exit. + +=item B<-l>, B<--login>=C + +Log in as C. The default is to use the value from the +configuration file and C if none is set there. + +=item B<-o>, B<--log-file>=C + +Use C as the log file. The default is to use the value from +the configuration file and C if none +is set there. + +=item B<-i>, B<--history-file>=C + +Use C as the history file for commands input by the +user. The default is to use the value from the configuration file and +C if none is set there. + +=item B<-e>, B<--execute>=C + +Execute this code on startup and exit afterwards. + +=item B<-f>, B<--file>=C + +Execute the code from the file C on startup and exit +afterwards. + +=back + =head1 DESCRIPTION Users of Ruby on Rails will recognize this as a perl reimplementation of the