/
YAML.pm
73 lines (51 loc) · 1.27 KB
/
YAML.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
package Rex::Interface::Cache::YAML;
use strict;
use warnings;
use Rex::Interface::Cache::Base;
use base qw(Rex::Interface::Cache::Base);
# VERSION
require Rex::Commands;
require Rex::Commands::Fs;
require YAML;
sub new {
my $that = shift;
my $proto = ref($that) || $that;
my $self = $proto->SUPER::new(@_);
bless( $self, $proto );
return $self;
}
sub save {
my ($self) = @_;
my $path = Rex::Commands::get("cache_path") || ".cache";
if ( exists $ENV{REX_CACHE_PATH} ) {
$path = $ENV{REX_CACHE_PATH};
}
if ( !-d $path ) {
Rex::Commands::LOCAL( sub { mkdir $path } );
}
open( my $fh, ">", "$path/" . Rex::Commands::connection->server . ".yml" )
or die($!);
print $fh YAML::Dump( $self->{__data__} );
close($fh);
}
sub load {
my ($self) = @_;
my $path = Rex::Commands::get("cache_path") || ".cache";
if ( exists $ENV{REX_CACHE_PATH} ) {
$path = $ENV{REX_CACHE_PATH};
}
my $file_name = "$path/" . Rex::Commands::connection->server . ".yml";
if ( !-f $file_name ) {
# no cache found
return;
}
my $yaml = eval { local ( @ARGV, $/ ) = ($file_name); <>; };
$yaml .= "\n";
$self->{__data__} = YAML::Load($yaml);
}
1;