source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli/Context.pm @ 2255

Last change on this file since 2255 was 2245, checked in by nanardon, 5 years ago

Cli don't redirect to Term->Out (disallow shell redirect)

File size: 2.3 KB
Line 
1package LATMOS::Accounts::Cli::Context;
2
3# $Id: Cli.pm 2145 2018-08-29 18:15:46Z nanardon $
4
5use strict;
6use warnings;
7use Moose;
8use LATMOS::Accounts::Log;
9use LATMOS::Accounts::Utils;
10use Term::ReadLine;
11use Text::ParseWords;
12use Getopt::Long;
13
14=head1 NAME
15
16LATMOS::Accounts::Cli - Command line interface functions
17
18=head1 DESCRIPTION
19
20This module handle envirronment and functons for L<la-cli> tools.
21
22=cut
23
24{
25    open (my $fh, "/dev/tty" )
26        or eval 'sub Term::ReadLine::findConsole { ("&STDIN", "&STDERR") }';
27    die $@ if $@;
28    close ($fh);
29}
30
31has Term => ( is => 'ro', isa => 'Term::ReadLine' );
32has TransMode => ( is => 'rw', isa => 'Bool', default => 0 );
33has TransStarted => ( is => 'rw', isa => 'Bool', default => 0 );
34has base => ( is => 'ro' );
35has Out => ( is => 'ro' );
36
37=head1 FUNCTIONS
38
39=head2 new ($env, $labase)
40
41Create an envirronment object.
42
43C<$env> is functions descriptions.
44
45=cut
46
47around BUILDARGS => sub {
48    my $orig = shift;
49    my $class = shift;
50
51    my $term = Term::ReadLine->new('LA CLI');
52    $term->MinLine(99999);
53    my $OUT = \*STDOUT;
54
55
56    return $class->$orig( Out => $OUT, Term => $term, @_ );
57
58};
59
60=head2 print
61
62=cut
63
64sub print {
65    my ( $self, $str, @args ) = @_;
66    my $out = $self->Out;
67
68    printf $out $str, @args;
69}
70
71=head2 commit
72
73Call commit to base unelss in transaction mode
74
75=cut
76
77sub commit {
78    my ($self) = @_;
79    if ($self->TransMode || $self->TransStarted) {
80        $self->TransStarted(1);
81    } else {
82        $self->_commit;
83    }
84}
85
86sub _commit {
87    my ($self) = @_;
88    $self->base->commit;
89    $self->TransStarted(0);
90}
91
92=head2 rollback
93
94Perform rollback unless in transaction mode
95
96=cut
97
98sub rollback {
99    my ($self) = @_;
100    if ($self->TransMode) {
101        $self->print("All pending changes get rollback\n");
102    }
103    if (!$self->TransStarted) {
104        $self->_rollback;
105    }
106}
107
108sub _rollback {
109    my ($self) = @_;
110    $self->base->rollback;
111    $self->TransStarted(0);
112}
113
1141;
115
116__END__
117
118=head1 SEE ALSO
119
120L<LATMOS::Accounts>
121
122=head1 AUTHOR
123
124Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
125
126=head1 COPYRIGHT AND LICENSE
127
128Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS
129
130This library is free software; you can redistribute it and/or modify
131it under the same terms as Perl itself, either Perl version 5.10.0 or,
132at your option, any later version of Perl 5 you may have available.
133
134=cut
Note: See TracBrowser for help on using the repository browser.