File Coverage

blib/lib/App/Cme/Command/edit.pm
Criterion Covered Total %
statement 17 60 28.3
branch 0 20 0.0
condition n/a
subroutine 6 11 54.5
pod 5 5 100.0
total 28 96 29.1


line stmt bran cond sub pod time code
1             #
2             # This file is part of App-Cme
3             #
4             # This software is Copyright (c) 2014-2022 by Dominique Dumont <ddumont@cpan.org>.
5             #
6             # This is free software, licensed under:
7             #
8             # The GNU Lesser General Public License, Version 2.1, February 1999
9             #
10             # ABSTRACT: Edit the configuration of an application
11              
12             package App::Cme::Command::edit ;
13             $App::Cme::Command::edit::VERSION = '1.037';
14 1     1   975 use strict;
  1         3  
  1         41  
15 1     1   5 use warnings;
  1         3  
  1         29  
16 1     1   22 use 5.10.1;
  1         3  
17              
18 1     1   5 use App::Cme -command ;
  1         2  
  1         9  
19              
20 1     1   385 use base qw/App::Cme::Common/;
  1         3  
  1         112  
21              
22 1     1   5 use Config::Model::ObjTreeScanner;
  1         2  
  1         549  
23              
24             sub validate_args {
25 0     0 1   my ($self, $opt, $args) = @_;
26 0           $self->check_unknown_args($args);
27 0           $self->process_args($opt,$args);
28 0           return;
29             }
30              
31             sub opt_spec {
32 0     0 1   my ( $class, $app ) = @_;
33             return (
34 0           [ "ui|if=s" => "user interface type. Either tk, curses, shell" ],
35             [ "backup:s" => "Create a backup of configuration files before saving." ],
36             [ "open-item=s" => "open a specific item of the configuration" ],
37             $class->cme_global_options,
38             );
39             }
40              
41             sub usage_desc {
42 0     0 1   my ($self) = @_;
43 0           my $desc = $self->SUPER::usage_desc; # "%c COMMAND %o"
44 0           return "$desc [application] [ file ] [ -ui tk|curses|shell ] [ -open-item xxx ] ";
45             }
46              
47             sub description {
48 0     0 1   my ($self) = @_;
49 0           return $self->get_documentation;
50             }
51             sub execute {
52 0     0 1   my ($self, $opt, $args) = @_;
53              
54 0           my ($model, $inst, $root) = $self->init_cme($opt,$args);
55              
56 0           my $has_tk = eval { require Config::Model::TkUI; 1; };
  0            
  0            
57              
58 0           my $has_curses = eval { require Config::Model::CursesUI; 1; };
  0            
  0            
59              
60 0           my $ui_type = $opt->{ui};
61              
62 0 0         if ( not defined $ui_type ) {
63 0 0         if ($has_tk) {
    0          
64 0           $ui_type = 'tk';
65             }
66             elsif ($has_curses) {
67 0           warn "You should install Config::Model::TkUI for a ", "more friendly user interface\n";
68 0           $ui_type = 'curses';
69             }
70             else {
71 0           warn "You should install Config::Model::TkUI or ",
72             "Config::Model::CursesUI ",
73             "for a more friendly user interface\n";
74 0           $ui_type = 'shell';
75             }
76             }
77              
78 0           $root->deep_check;
79              
80 0 0         if ( $ui_type eq 'shell' ) {
    0          
    0          
81 0           require Config::Model::TermUI;
82 0           $self->run_shell_ui('Config::Model::TermUI', $inst) ;
83             }
84             elsif ( $ui_type eq 'curses' ) {
85 0 0         die "cannot run curses interface: ",
86             "Config::Model::CursesUI is not installed, please use shell or simple UI\n"
87             unless $has_curses;
88 0           my $err_file = '/tmp/cme-error.log';
89              
90 0           print "In case of error, check $err_file\n";
91              
92 0 0         open( my $fh, ">", $err_file ) || die "Can't open $err_file: $!\n";
93 0 0         open(STDERR, ">&", $fh)|| die "Can't open STDERR:$!\n";
94              
95 0           my $dialog = Config::Model::CursesUI->new();
96              
97             # engage in user interaction
98 0           $dialog->start($model);
99              
100 0           close($fh);
101             }
102             elsif ( $ui_type eq 'tk' ) {
103 0 0         die "cannot run Tk interface: Config::Model::TkUI is not installed, please use curses or shell or simple ui\n"
104             unless $has_tk;
105 0           $self ->run_tk_ui ( $inst, $opt);
106             }
107             else {
108 0           die "Unsupported user interface: $ui_type\n";
109             }
110 0           return;
111             }
112              
113             1;
114              
115             __END__
116              
117             =pod
118              
119             =encoding UTF-8
120              
121             =head1 NAME
122              
123             App::Cme::Command::edit - Edit the configuration of an application
124              
125             =head1 VERSION
126              
127             version 1.037
128              
129             =head1 SYNOPSIS
130              
131             # edit dpkg config with GUI (requires Config::Model::Dpkg)
132             cme edit dpkg
133              
134             # force usage of simple shell like interface
135             cme edit dpkg-copyright --ui shell
136              
137             # edit /etc/sshd_config (requires Config::Model::OpenSsh)
138             sudo cme edit sshd
139              
140             # edit ~/.ssh/config (requires Config::Model::OpenSsh)
141             cme edit ssh
142              
143             # edit a file (file name specification is mandatory here)
144             cme edit multistrap my.conf
145              
146             =head1 DESCRIPTION
147              
148             Edit a configuration. By default, a Tk GUI will be opened if C<Config::Model::TkUI> is
149             installed. You can choose another user interface with the C<-ui> option:
150              
151             =over
152              
153             =item *
154              
155             C<tk>: provides a Tk graphical interface (If C<Config::Model::TkUI> is
156             installed).
157              
158             =item *
159              
160             C<curses>: provides a curses user interface (If
161             L<Config::Model::CursesUI> is installed).
162              
163             =item *
164              
165             C<shell>: provides a shell like interface. See L<Config::Model::TermUI>
166             for details. This is equivalent to running C<cme shell> command.
167              
168             =back
169              
170             =head1 Common options
171              
172             See L<cme/"Global Options">.
173              
174             =head1 options
175              
176             =over
177              
178             =item -open-item
179              
180             Open a specific item of the configuration when opening the editor
181              
182             =back
183              
184             =head1 SEE ALSO
185              
186             L<cme>
187              
188             =head1 AUTHOR
189              
190             Dominique Dumont
191              
192             =head1 COPYRIGHT AND LICENSE
193              
194             This software is Copyright (c) 2014-2022 by Dominique Dumont <ddumont@cpan.org>.
195              
196             This is free software, licensed under:
197              
198             The GNU Lesser General Public License, Version 2.1, February 1999
199              
200             =cut