File: dump.pm

package info (click to toggle)
cme 1.043-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 392 kB
  • sloc: perl: 1,293; makefile: 19; sh: 9
file content (127 lines) | stat: -rw-r--r-- 3,039 bytes parent folder | download | duplicates (4)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# ABSTRACT: Dump the configuration of an application

package App::Cme::Command::dump ;

use strict;
use warnings;
use 5.10.1;

use App::Cme -command ;

use base qw/App::Cme::Common/;

use Config::Model::ObjTreeScanner;
use YAML::PP qw/Dump/;
use JSON;
use Data::Dumper;

sub validate_args {
    my ($self, $opt, $args) = @_;
    $self->check_unknown_args($args);
    $opt->{quiet} = 1; # don't want to mess up yaml output
    $self->process_args($opt,$args);
    return;
}

sub opt_spec {
    my ( $class, $app ) = @_;
    return (
        [
            "dumptype=s" => "Dump all values (full) or only customized values",
            {
                regex => qr/^(?:full|custom|non_upstream_default)$/,
                default => 'custom'
            }
        ],
        [
            "format=s" => "dump using specified format (yaml json perl cml)",
            {
                regex => qr/^(?:json|ya?ml|perl|cml|cds)$/i,
                default => 'yaml'
            },
        ],
        $class->cme_global_options,
    );
}

sub usage_desc {
  my ($self) = @_;
  my $desc = $self->SUPER::usage_desc; # "%c COMMAND %o"
  return "$desc [application]  [ config_file ] [ -dumptype full|custom ] [ path ]";
}

sub description {
    my ($self) = @_;
    return $self->get_documentation;
}

sub execute {
    my ($self, $opt, $args) = @_;

    my ($model, $inst, $root) = $self->init_cme($opt,$args);

    my $target_node = $root->grab(step => "@$args", type => 'node');

    my $dump_string;
    my $format = $opt->{format};
    my $mode = $opt->{dumptype} || 'custom';

    if ($format =~ /cml|cds/i) {
        $dump_string = $target_node->dump_tree( mode => $mode );
    }
    else {
        my $perl_data = $target_node->dump_as_data(
            ordered_hash_as_list => 0,
            mode => $mode
        );
        $dump_string
            = $format =~ /ya?ml/i ? Dump($perl_data)
            : $format =~ /json/i  ? encode_json($perl_data)
            :                       Dumper($perl_data) ; # Perl data structure
    }
    print $dump_string ;
    return;
}

1;

__END__

=head1 SYNOPSIS

  # dump ~/.ssh/config in cme syntax
  # (this example requires Config::Model::OpenSsh)
  $ cme dump -format cml ssh
  Host:"*" -
  Host:"*.debian.org"
    User=dod -


=head1 DESCRIPTION

Dump configuration content on STDOUT with YAML format.

By default, dump only custom values, i.e. different from application
built-in values or model default values. You can use the C<-dumptype> option for
other types of dump:

 -dumptype [ full | custom | non_upstream_default ]

Choose to dump every values (full), or only customized values (default)

C<non_upstream_default> is like C<full> mode, but value identical with
application default are omitted. But this should seldom happen.

By default, dump in yaml format. This can be changed in C<json>,
C<perl>, C<cml> (aka L<Config::Model::Loader> format, C<cds> is also
accepted) with C<-format> option.

=head1 Common options

See L<cme/"Global Options">.

=head1 SEE ALSO

L<cme>

=cut