[2209] | 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; |
---|
[2423] | 13 | use YAML; |
---|
[2391] | 14 | use utf8; |
---|
| 15 | use open qw( :std :utf8 ); |
---|
[2209] | 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' ); |
---|
[2285] | 38 | has La => ( is => 'ro' ); |
---|
[2209] | 39 | has Out => ( is => 'ro' ); |
---|
[2399] | 40 | has TempOut => ( is => 'rw' ); |
---|
[2285] | 41 | has Interractive => ( is => 'rw', isa => 'Bool', default => 1 ); |
---|
[2423] | 42 | has Preferences => ( is => 'rw', default => sub { {} } ); |
---|
[2209] | 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 | |
---|
[2391] | 58 | my $term = Term::ReadLine->new('LA CLI', \*STDIN, \*STDOUT ); |
---|
| 59 | binmode($term->IN, ':utf8'); |
---|
[2209] | 60 | $term->MinLine(99999); |
---|
[2245] | 61 | my $OUT = \*STDOUT; |
---|
[2209] | 62 | |
---|
| 63 | return $class->$orig( Out => $OUT, Term => $term, @_ ); |
---|
| 64 | |
---|
| 65 | }; |
---|
| 66 | |
---|
[2423] | 67 | sub _historyFile { "$ENV{HOME}/.lacli_history" } |
---|
| 68 | |
---|
| 69 | sub ReadHistory { |
---|
| 70 | my ( $self ) = @_; |
---|
| 71 | $self->Term->ReadHistory( _historyFile() ); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | sub WriteHistory { |
---|
| 75 | my ( $self ) = @_; |
---|
| 76 | $self->Term->WriteHistory( _historyFile() ); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | sub _preferenceFile { "$ENV{HOME}/.lacli_preference" } |
---|
| 80 | |
---|
| 81 | sub ReadPreferences { |
---|
| 82 | my ( $self ) = @_; |
---|
| 83 | |
---|
| 84 | if (-f _preferenceFile() ) { |
---|
| 85 | $self->Preferences( YAML::LoadFile( _preferenceFile() ) ); |
---|
| 86 | } |
---|
[2424] | 87 | |
---|
| 88 | $self->ApplySetting(); |
---|
[2423] | 89 | } |
---|
| 90 | |
---|
| 91 | sub WritePreferences { |
---|
| 92 | my ( $self ) = @_; |
---|
| 93 | |
---|
| 94 | YAML::DumpFile( _preferenceFile(), $self->Preferences() ); |
---|
| 95 | } |
---|
| 96 | |
---|
[2424] | 97 | sub DefaultSetting { |
---|
| 98 | my ( $self, $setting ) = @_; |
---|
| 99 | |
---|
| 100 | my $defaults = { |
---|
| 101 | 'historysize' => 1000, |
---|
| 102 | }; |
---|
| 103 | if ($setting) { |
---|
| 104 | return $defaults->{ $setting }; |
---|
| 105 | } else { |
---|
| 106 | return sort keys %{ $defaults }; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | sub ApplySetting { |
---|
| 111 | my ( $self ) = @_; |
---|
| 112 | |
---|
| 113 | $self->Term->StifleHistory( $self->Setting('historysize') ); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | sub Setting { |
---|
| 117 | my ( $self, $setting, $value ) = @_; |
---|
| 118 | |
---|
| 119 | my $oldvalue = defined($self->Preferences->{settings}{ $setting }) |
---|
| 120 | ? $self->Preferences->{settings}{ $setting } : $self->DefaultSetting( $setting ); |
---|
| 121 | |
---|
| 122 | if (defined($value)) { |
---|
| 123 | if ($value eq 'default') { |
---|
| 124 | delete($self->Preferences->{settings}{ $setting }); |
---|
| 125 | } else { |
---|
| 126 | $self->Preferences->{settings}{ $setting } = $value; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | $self->ApplySetting(); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return ($oldvalue); |
---|
| 133 | } |
---|
| 134 | |
---|
[2209] | 135 | =head2 print |
---|
| 136 | |
---|
| 137 | =cut |
---|
| 138 | |
---|
| 139 | sub print { |
---|
[2397] | 140 | my ( $self, @args ) = @_; |
---|
[2399] | 141 | my $out = $self->TempOut || $self->Out; |
---|
[2397] | 142 | |
---|
| 143 | print $out @args; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | =head2 printf |
---|
| 147 | |
---|
| 148 | =cut |
---|
| 149 | |
---|
| 150 | sub printf { |
---|
[2209] | 151 | my ( $self, $str, @args ) = @_; |
---|
[2399] | 152 | my $out = $self->TempOut || $self->Out; |
---|
[2209] | 153 | |
---|
| 154 | printf $out $str, @args; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | =head2 commit |
---|
| 158 | |
---|
| 159 | Call commit to base unelss in transaction mode |
---|
| 160 | |
---|
| 161 | =cut |
---|
| 162 | |
---|
| 163 | sub commit { |
---|
| 164 | my ($self) = @_; |
---|
| 165 | if ($self->TransMode || $self->TransStarted) { |
---|
| 166 | $self->TransStarted(1); |
---|
| 167 | } else { |
---|
| 168 | $self->_commit; |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | sub _commit { |
---|
| 173 | my ($self) = @_; |
---|
| 174 | $self->base->commit; |
---|
| 175 | $self->TransStarted(0); |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | =head2 rollback |
---|
| 179 | |
---|
| 180 | Perform rollback unless in transaction mode |
---|
| 181 | |
---|
| 182 | =cut |
---|
| 183 | |
---|
| 184 | sub rollback { |
---|
| 185 | my ($self) = @_; |
---|
| 186 | if ($self->TransMode) { |
---|
| 187 | $self->print("All pending changes get rollback\n"); |
---|
| 188 | } |
---|
| 189 | if (!$self->TransStarted) { |
---|
| 190 | $self->_rollback; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | sub _rollback { |
---|
| 195 | my ($self) = @_; |
---|
| 196 | $self->base->rollback; |
---|
| 197 | $self->TransStarted(0); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | 1; |
---|
| 201 | |
---|
| 202 | __END__ |
---|
| 203 | |
---|
| 204 | =head1 SEE ALSO |
---|
| 205 | |
---|
| 206 | L<LATMOS::Accounts> |
---|
| 207 | |
---|
| 208 | =head1 AUTHOR |
---|
| 209 | |
---|
| 210 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 211 | |
---|
| 212 | =head1 COPYRIGHT AND LICENSE |
---|
| 213 | |
---|
| 214 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
| 215 | |
---|
| 216 | This library is free software; you can redistribute it and/or modify |
---|
| 217 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 218 | at your option, any later version of Perl 5 you may have available. |
---|
| 219 | |
---|
| 220 | =cut |
---|