Consistently "use strictures 2" in all source files.
[tails-persistence-setup.git] / lib / Tails / Persistence / Configuration.pm
blob4bfafac898b44fd53fc87a28a1474e6b5dcfd151
1 =head1 NAME
3 Tails::Persistence::Configuration - manage live-persistence.conf and presets
5 =cut
7 package Tails::Persistence::Configuration;
8 use Moo;
9 use MooX::late;
10 use MooX::HandlesVia;
12 use strictures 2;
13 use autodie qw(:all);
14 use Carp;
16 use Tails::Persistence::Configuration::Atom;
17 use Tails::Persistence::Configuration::ConfigFile;
18 use Tails::Persistence::Configuration::Presets;
19 use Types::Path::Tiny qw{AbsPath};
21 use Locale::gettext;
22 use POSIX;
23 setlocale(LC_MESSAGES, "");
24 textdomain("tails-persistence-setup");
26 use namespace::clean;
29 =head1 ATTRIBUTES
31 =cut
33 has 'config_file_path' => (
34 isa => AbsPath,
35 is => 'rw',
36 coerce => AbsPath->coercion,
39 has 'file' => (
40 lazy_build => 1,
41 is => 'rw',
42 isa => 'Tails::Persistence::Configuration::ConfigFile',
44 has 'presets' => (
45 lazy_build => 1,
46 is => 'rw',
47 isa => 'Tails::Persistence::Configuration::Presets',
49 has 'force_enable_presets' => (
50 is => 'ro',
51 isa => 'ArrayRef[Str]',
52 default => sub { [] },
54 has 'atoms' => (
55 lazy_build => 1,
56 is => 'rw',
57 isa => 'ArrayRef[Tails::Persistence::Configuration::Atom]',
58 handles_via => 'Array',
59 handles => {
60 all_atoms => 'elements',
61 push_atom => 'push',
66 =head1 CONSTRUCTORS
68 =cut
70 sub _build_file {
71 my $self = shift;
72 my $file = Tails::Persistence::Configuration::ConfigFile->new(
73 config_file_path => $self->config_file_path
75 return $file;
78 sub _build_presets {
79 my $self = shift;
80 Tails::Persistence::Configuration::Presets->new();
83 sub _build_atoms {
84 my $self = shift;
85 return $self->merge_file_with_presets($self->file, $self->presets);
88 =head1 METHODS
90 =cut
92 sub lines_not_in_presets {
93 my $self = shift;
94 grep {
95 my $line = $_;
96 ! grep { $_->equals_line($line) } $self->presets->all
97 } $self->file->all_lines;
100 sub merge_file_with_presets {
101 my $self = shift;
102 # Modifying and returning clones of the presets atoms would be a bit cleaner.
103 $self->presets->set_state_from_lines($self->file->all_lines);
104 $self->presets->set_state_from_overrides($self->force_enable_presets);
107 $self->presets->all,
108 map {
109 Tails::Persistence::Configuration::Atom->new_from_line(
111 enabled => 1
113 } $self->lines_not_in_presets,
117 sub all_enabled_atoms {
118 my $self = shift;
119 grep { $_->enabled } $self->all_atoms;
122 sub all_enabled_lines {
123 my $self = shift;
124 map {
125 Tails::Persistence::Configuration::Line->new(
126 destination => $_->destination,
127 options => $_->options,
129 } $self->all_enabled_atoms;
132 sub save {
133 my $self = shift;
134 $self->file->lines([ $self->all_enabled_lines ]);
135 $self->file->save;
138 no Moo;