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

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

la-cli: add collection command (save object list)

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