since pluggable store landed in trunk add --store-path and --store-module options
[perl-cwmp.git] / bin / cli.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use lib 'lib';
7
8 use Term::Shelly;
9 use CWMP::Store;
10 use CWMP::Tree;
11 use DBM::Deep;
12 use Data::Dump qw/dump/;
13 use Getopt::Long;
14 use Carp qw/confess/;
15
16 my $debug = 0;
17 my $store_path = './';
18 my $store_plugin = 'YAML';
19
20 GetOptions(
21         'debug+' => \$debug,
22         'store-path=s' => \$store_path,
23         'store-plugin=s' => \$store_plugin,
24 );
25
26 my $sh = Term::Shelly->new();
27 my $tree = CWMP::Tree->new({ debug => $debug });
28
29 our $store = CWMP::Store->new({
30         module => $store_plugin,
31         path => $store_path,
32         debug => $debug,
33 });
34
35 $sh->out(
36 "You can issue commenads in form using tab-complete:
37
38 CPE_Serial ( parametar [ [=] value ] | command )
39 "
40 );
41
42 $sh->{"completion_function"} = \&completer;
43 $sh->{"readline_callback"} = \&command;
44
45 my @history = ( 'exit' );
46 my $pos = $#history;
47 $sh->{'mappings'}->{'up-history'} = [ sub {
48         my $self = shift;
49         if ( $pos >= 0 ) {
50                 $self->{'input_line'} = $history[ $pos ];
51                 $pos--;
52                 $self->{'input_position'} = length( $self->{'input_line'} );
53                 $self->fix_inputline;
54         }
55 } ];
56 $sh->{'mappings'}->{'down-history'} = [ sub {
57         my $self = shift;
58         my $line = '';
59         if ( $pos < $#history ) {
60                 $pos++;
61                 $line = $history[ $pos ];
62         }
63         $self->{'input_line'} = $line;
64         $self->{'input_position'} = length( $self->{'input_line'} );
65         $self->fix_inputline;
66 } ];
67
68 $sh->prompt( '> ' );
69
70 while (1) {
71         $sh->do_one_loop();
72 }
73
74 sub completer {
75         my ($line, $bword, $pos, $curword) = @_;
76
77         $sh->out( "line: '$line' [ $bword - $pos ] -> '$curword'" );
78
79         my @matches;
80
81         # do we have list (part) of CPE?
82         if ( $line =~ /^(\S*)\s*$/ ) {
83                 @matches = sort grep { /^\Q$curword\E/ } $store->all_uids;
84                 $sh->out( "CPE available: ", join(",", @matches ) );
85         } elsif ( $line =~ /^(\w+)\s+(\S+)$/ ) {
86                 $sh->out("finding completes for '$2'");
87                 my ( $cpe_uid, $name ) = ( $1, $2 );
88
89                 my $beginning = $name;
90                 my $part = '';
91                 if ( $beginning =~ s/\.([^\.]+)$// ) {
92                         $part = $1;
93                 } elsif ( $beginning =~ s/^(\S+)$// ) {
94                         $part = $1;
95                 } else {
96                         confess "can't extract suffix";
97                 }
98
99                 $sh->out( "## $cpe_uid ## beginning: $beginning -- part: $part" );
100                 my $perl = "\$store->db->{state}->{'$cpe_uid'}->{ParameterInfo}";
101                 $perl .= '->' . $tree->name2perl( $beginning ) if ( defined( $beginning ) && $beginning ne '' );
102                 $sh->out( "## $cpe_uid ## $perl" );
103
104                 @matches = eval "keys %{ $perl }";
105
106         }
107
108         return @matches;
109 }
110
111 sub command {
112         my ( $line ) = @_;
113
114         return unless ( $line && $line ne '' );
115
116         # just CPE uid
117         if ( $line =~ /^(\w+)\s*$/ ) {
118                 if ( main->can( $1 ) ) {
119                         $sh->out( "# execute command $1" );
120                         eval " \&$1( \$line ) ";
121                 } elsif ( defined ( $store->db->{state}->{$1} ) ) {
122                         $sh->out(
123                                 join(" ", keys %{ $store->db->{state}->{$1}->{ParameterInfo} })
124                         );
125                         push @history, $line;
126                         $pos = $#history;
127                 } else {
128                         $sh->out( "$line: CPE not found" );
129                 }
130         } else {
131                 $sh->out( "$line: command not recognized" );
132         }
133 }
134
135 sub history {
136         $sh->out( "history: ", dump ( @history ) );
137 }
138
139 sub exit {
140         CORE::exit();
141 }