3 ###APPAUTHOR: xiaoranzzz
4 ###APPDATE: Tue May 25 22:26:26 2010
6 ###APPDESC: a basic config system
7 ###APPUSAGE: (action) key
8 ###APPEXAMPLE: r-xconfig read 'names,/jordan/,id,jordan,1'
12 #ENV variable MUST be defined somewhere,
13 #FOR perl to search modules from,
15 use lib
$ENV{XR_PERL_MODULE_DIR
};
17 use MyPlace
::Script
::Usage qw
/help_required help_even_empty/;
18 exit 0 if(help_required
($0,@ARGV));
19 #exit 0 if(help_even_empty($0,@ARGV));
24 print STDERR color
('green'),@_,color
('reset');
27 print STDERR color
('yellow'),@_,color
('reset');
30 print STDERR color
('red'),@_,color
('reset');
34 my ($pre,$path,$suf,$sep) = @_;
35 $sep = '->' unless($sep);
37 $pre = "" unless($pre);
38 $suf = "" unless($suf);
39 print1
$pre,join($sep,@
{$path}),$suf;
48 print3
"\t[Failed]\n";
54 my @r=$CONFIG->list(@_);
62 my @r=$CONFIG->list(@_);
64 $suf="\n" unless(defined $suf);
72 my $opt = shift @ARGV;
73 if($opt and $opt eq '-f') {
74 $database = shift @ARGV;
79 my $action = shift @ARGV;
80 my $query = shift @ARGV;
81 my $userdata = shift @ARGV;
83 $action = 'list' unless($action);
84 if((!$query) and $action !~ m/^(?:list|add|plain|read|write|print|query)$/) {
91 if($action eq 'add' and !$query) {
92 print3
"Invalid usage\n";
93 print2
"Usage:\n\tr-xconfig add keys...|values...\n";
96 $CONFIG = MyPlace
::XConfig
->new();
97 $CONFIG->read_plainfile($database);
100 if(($action eq 'list') or ($action eq 'query')) {
101 $query = '/.+/' unless($query);
103 elsif($action eq 'plain') {
104 $query = '-' unless($query);
105 # print STDERR "Write plain data to $query \n";
106 $CONFIG->write_plainfile($query,1);
110 if($action eq 'add') {
112 push @keys,$query if($query);
113 push @keys,$userdata if($userdata);
114 push @keys,@ARGV if(@ARGV);
115 print1
"add ", join(' -> ',@keys);
116 print_status
($CONFIG->add(@keys));
119 my @target = $CONFIG->query($query);
121 print3
"query match nothing\n";
125 if($action eq 'list') {
126 print_target
("","\n",$CONFIG,@target);
129 elsif($action eq 'print') {
130 print $_,"\n" foreach(&get_data
($CONFIG,@target));
133 elsif($action eq 'read') {
134 my @r=$CONFIG->read(@target);
136 my($path,$values) = @
{$_};
137 print_path
("",$path," = ");
138 print2
join(", ",@
{$values}),"\n";
141 elsif($action eq 'write') {
142 print_target
("write \"","\" = \"$userdata\"\n",$CONFIG,@target);
143 print_status
($CONFIG->write($userdata,@target));
145 elsif($action eq 'delete') {
146 print_target
("delete \"",($userdata ?
"->$userdata\"\n" : "\"\n"),$CONFIG,@target);
147 print_status
($CONFIG->delete($userdata,@target));
149 elsif($action eq 'query') {
150 my @records = $CONFIG->get_records(@target);
152 print_path
('',$_,"\n","->");
161 if($CONFIG->{dirty
}) {
162 print2
"Saving configs to $database";
163 print_status
($CONFIG->write_plainfile($database));