File: search.pm

package info (click to toggle)
cme 1.044-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 392 kB
  • sloc: perl: 1,301; makefile: 19; sh: 9
file content (134 lines) | stat: -rw-r--r-- 2,437 bytes parent folder | download
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
128
129
130
131
132
133
134
# ABSTRACT: Search the configuration of an application

package App::Cme::Command::search ;

use strict;
use warnings;
use 5.10.1;

use App::Cme -command ;

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

use Config::Model::ObjTreeScanner;

sub validate_args {
    my ($self, $opt, $args) = @_;
    $self->check_unknown_args($args);
    $self->process_args($opt,$args);
    return;
}

sub opt_spec {
    my ( $class, $app ) = @_;
    return (
        [
            "search=s"        => "string or pattern to search in configuration" ,
            { required => 1 }
        ],
        [
            "narrow-search=s" => "narrows down the search to element, value, key, summary, description or help",
            { regex => qr/^(?:element|value|key|summary|description|help|all)$/, default => 'all' }
        ],
        $class->cme_global_options,
    );
}

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

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

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

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

    my @res = $root->tree_searcher( type => $opt->{narrow_search} )->search($opt->{search});
    foreach my $path (@res) {
        print "$path";
        my $obj = $root->grab($path);
        if ( $obj->get_type =~ /leaf|check_list/ ) {
            my $v = $obj->fetch;
            $v = defined $v ? $v : '<undef>';
            print " -> '$v'";
        }
        print "\n";
    }
    return;
}

1;

__END__

=head1 SYNOPSIS


=head1 DESCRIPTION

Search configuration data with the following options:

=over

=item -search

Specifies a string or pattern to search. C<cme> will a list of path pointing
to the matching tree element and their value.
See L<Config::Model::AnyThing/grab(...)> for details
on the path syntax.

=item -narrow-search

Narrows down the search to:

=over

=item element

The element name (e.g. C<Host> for ssh model)

=item value

The element value

=item key

A hash key (e.g. key of C<Match> element for ssh model)

=item summary

Summary text

=item description

description text

=item help

value help text

=back

=back

Example:

 $ cme search ssh --search dod -narrow value
 Host:"*.debian.org" User -> 'dod'

=head1 Common options

See L<cme/"Global Options">.

=head1 SEE ALSO

L<cme>

=cut