App-Cme

 view release on metacpan or  search on metacpan

lib/App/Cme/Command/modify.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: Modify the configuration of an application

package App::Cme::Command::modify ;
$App::Cme::Command::modify::VERSION = '1.047';
use strict;
use warnings;
use v5.20;
use feature qw/postderef signatures/;
no warnings qw/experimental::postderef experimental::signatures/;

use App::Cme -command ;

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

use Config::Model::ObjTreeScanner;
use Config::Model qw/initialize_log4perl/;

sub validate_args {
    my ($self, $opt, $args) = @_;
    $self->check_unknown_args($args);
    $self->process_args($opt,$args);
    $self->usage_error("No modification instructions given on command line")
        unless @$args or $opt->{save};
    return;
}

sub opt_spec {
    my ( $class, $app ) = @_;
    return ( 
        [ "backup:s"  => "Create a backup of configuration files before saving." ],
        [ "commit|c:s" => "commit change with message passed as argument" ],
        $class->cme_global_options,
    );
}

sub usage_desc {
    my ($self) = @_;
    my $desc = $self->SUPER::usage_desc; # "%c COMMAND %o"
    return "$desc [application] [file ] instructions";
}

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

sub commit ($self, $msg) {
    system(qw/git commit -a -m/, $msg) == 0
        or die "git commit failed: $?\n";
    return;
}

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

    $opt->{_verbose} = 'Loader' if $opt->{verbose};

    my $stashed;

    # stash pending work
    if ($opt->{commit}) {
        $stashed = $self->autostash;
    }

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

    # needed to create write_back subs
    if ($opt->{save} and not @$args) {
        $root->dump_tree();
    }

    $root->load("@$args");

    $root->deep_check; # consistency check

    if ($inst->needs_save or $opt->{save}) {
        $self->save($inst,$opt) ;



( run in 0.738 second using v1.01-cache-2.11-cpan-39bf76dae61 )