1 | package LATMOS::Accounts::Cli::Context; |
---|
2 | |
---|
3 | # $Id: Cli.pm 2145 2018-08-29 18:15:46Z nanardon $ |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use Moose; |
---|
8 | use LATMOS::Accounts::Log; |
---|
9 | use LATMOS::Accounts::Utils; |
---|
10 | use Term::ReadLine; |
---|
11 | use Text::ParseWords; |
---|
12 | use Getopt::Long; |
---|
13 | use YAML; |
---|
14 | use utf8; |
---|
15 | use open qw( :std :utf8 ); |
---|
16 | |
---|
17 | =head1 NAME |
---|
18 | |
---|
19 | LATMOS::Accounts::Cli - Command line interface functions |
---|
20 | |
---|
21 | =head1 DESCRIPTION |
---|
22 | |
---|
23 | This 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 | |
---|
34 | has Term => ( is => 'ro', isa => 'Term::ReadLine' ); |
---|
35 | has TransMode => ( is => 'rw', isa => 'Bool', default => 0 ); |
---|
36 | has TransStarted => ( is => 'rw', isa => 'Bool', default => 0 ); |
---|
37 | has base => ( is => 'ro' ); |
---|
38 | has La => ( is => 'ro' ); |
---|
39 | has Out => ( is => 'ro' ); |
---|
40 | has TempOut => ( is => 'rw' ); |
---|
41 | has Interractive => ( is => 'rw', isa => 'Bool', default => 1 ); |
---|
42 | has Preferences => ( is => 'rw', default => sub { {} } ); |
---|
43 | |
---|
44 | =head1 FUNCTIONS |
---|
45 | |
---|
46 | =head2 new ($env, $labase) |
---|
47 | |
---|
48 | Create an envirronment object. |
---|
49 | |
---|
50 | C<$env> is functions descriptions. |
---|
51 | |
---|
52 | =cut |
---|
53 | |
---|
54 | around 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 | |
---|
69 | sub _historyFile { "$ENV{HOME}/.lacli_history" } |
---|
70 | |
---|
71 | sub ReadHistory { |
---|
72 | my ( $self ) = @_; |
---|
73 | $self->Term->ReadHistory( _historyFile() ); |
---|
74 | } |
---|
75 | |
---|
76 | sub WriteHistory { |
---|
77 | my ( $self ) = @_; |
---|
78 | $self->Term->WriteHistory( _historyFile() ); |
---|
79 | } |
---|
80 | |
---|
81 | sub _preferenceFile { "$ENV{HOME}/.lacli_preference" } |
---|
82 | |
---|
83 | sub ReadPreferences { |
---|
84 | my ( $self ) = @_; |
---|
85 | |
---|
86 | if (-f _preferenceFile() ) { |
---|
87 | $self->Preferences( YAML::LoadFile( _preferenceFile() ) ); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | sub WritePreferences { |
---|
92 | my ( $self ) = @_; |
---|
93 | |
---|
94 | YAML::DumpFile( _preferenceFile(), $self->Preferences() ); |
---|
95 | } |
---|
96 | |
---|
97 | =head2 print |
---|
98 | |
---|
99 | =cut |
---|
100 | |
---|
101 | sub 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 | |
---|
112 | sub 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 | |
---|
121 | Call commit to base unelss in transaction mode |
---|
122 | |
---|
123 | =cut |
---|
124 | |
---|
125 | sub commit { |
---|
126 | my ($self) = @_; |
---|
127 | if ($self->TransMode || $self->TransStarted) { |
---|
128 | $self->TransStarted(1); |
---|
129 | } else { |
---|
130 | $self->_commit; |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | sub _commit { |
---|
135 | my ($self) = @_; |
---|
136 | $self->base->commit; |
---|
137 | $self->TransStarted(0); |
---|
138 | } |
---|
139 | |
---|
140 | =head2 rollback |
---|
141 | |
---|
142 | Perform rollback unless in transaction mode |
---|
143 | |
---|
144 | =cut |
---|
145 | |
---|
146 | sub 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 | |
---|
156 | sub _rollback { |
---|
157 | my ($self) = @_; |
---|
158 | $self->base->rollback; |
---|
159 | $self->TransStarted(0); |
---|
160 | } |
---|
161 | |
---|
162 | 1; |
---|
163 | |
---|
164 | __END__ |
---|
165 | |
---|
166 | =head1 SEE ALSO |
---|
167 | |
---|
168 | L<LATMOS::Accounts> |
---|
169 | |
---|
170 | =head1 AUTHOR |
---|
171 | |
---|
172 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
173 | |
---|
174 | =head1 COPYRIGHT AND LICENSE |
---|
175 | |
---|
176 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
177 | |
---|
178 | This library is free software; you can redistribute it and/or modify |
---|
179 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
180 | at your option, any later version of Perl 5 you may have available. |
---|
181 | |
---|
182 | =cut |
---|