1#
2# This file is part of Config-Model
3#
4# This software is Copyright (c) 2005-2021 by Dominique Dumont.
5#
6# This is free software, licensed under:
7#
8#   The GNU Lesser General Public License, Version 2.1, February 1999
9#
10package Config::Model::Backend::Mini ;
11use strict;
12use warnings;
13
14use 5.10.1;
15use Mouse;
16
17extends 'Config::Model::Backend::Any';
18with 'Config::Model::Role::FileHandler';
19
20use Path::Tiny;
21use YAML::Tiny qw/LoadFile Dump/;
22
23sub _get_cfg_dir {
24    my ($self,$root) = @_;
25    my $dir = $self->get_tuned_config_dir(
26        config_dir => 'debian/meta',
27        root => $root
28    );
29    my $file =  $dir->child('test.yml');
30    return $file;
31}
32
33sub read {
34    my $self = shift;
35    my %args = @_;
36
37    # args is:
38    # object     => $obj,         # Config::Model::Node object
39    # root       => './my_test',  # fake root directory, userd for tests
40    # config_dir => /etc/foo',    # absolute path
41    # file       => 'foo.conf',   # file name
42    # file_path  => './my_test/etc/foo/foo.conf'
43    # check      => yes|no|skip
44
45    my $file = $self->_get_cfg_dir($args{root});
46
47    return 0 unless $file->exists;    # no file to read
48
49    my $perl_data = LoadFile($file);
50
51    # load perl data in tree
52    $self->node->load_data( data => $perl_data, check => $args{check} || 'yes' );
53    return 1;
54}
55
56sub write {
57    my $self = shift;
58    my %args = @_;
59
60    # args is:
61    # object     => $obj,         # Config::Model::Node object
62    # root       => './my_test',  # fake root directory, used for tests
63    # config_dir => /etc/foo',    # absolute path
64    # file       => 'foo.conf',   # file name
65    # file_path  => './my_test/etc/foo/foo.conf'
66    # check      => yes|no|skip
67
68    my $perl_data = $self->node->dump_as_data( full_dump => $args{full_dump} // 0);
69
70    my $file = $self->_get_cfg_dir($args{root});
71    $file->parent->mkpath;
72
73    my $yaml = Dump( $perl_data );
74    $file->spew_utf8($yaml);
75
76    return 1;
77}
78
791;
80