App-Cme

 view release on metacpan or  search on metacpan

lib/App/Cme/Command/dump.pm  view on Meta::CPAN

#
# This file is part of App-Cme
#
# This software is Copyright (c) 2014-2022 by Dominique Dumont <ddumont@cpan.org>.
#
# This is free software, licensed under:
#
#   The GNU Lesser General Public License, Version 2.1, February 1999
#
# ABSTRACT: Dump the configuration of an application

package App::Cme::Command::dump ;
$App::Cme::Command::dump::VERSION = '1.048';
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__

=pod

=encoding UTF-8

=head1 NAME

App::Cme::Command::dump - Dump the configuration of an application

=head1 VERSION

version 1.048

=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 -



( run in 1.853 second using v1.01-cache-2.11-cpan-524268b4103 )