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

Last change on this file since 2399 was 2399, checked in by nanardon, 4 years ago

la-cli: add | and > feature (act like in shell)

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