updated git and svn scripts
[xrzperl.git] / r-xconfig
blob42a73baa9fb240744641a3346cbf53d8b5340635
1 #!/usr/bin/perl -w
2 ###APPNAME: r-xconfig
3 ###APPAUTHOR: xiaoranzzz
4 ###APPDATE: Tue May 25 22:26:26 2010
5 ###APPVER: 0.1
6 ###APPDESC: a basic config system
7 ###APPUSAGE: (action) key
8 ###APPEXAMPLE: r-xconfig read 'names,/jordan/,id,jordan,1'
9 ###APPOPTION:
10 use strict;
12 #ENV variable MUST be defined somewhere,
13 #FOR perl to search modules from,
14 #OR nothing will work
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));
20 use MyPlace::XConfig;
21 use Term::ANSIColor;
23 sub print1 {
24 print STDERR color('green'),@_,color('reset');
26 sub print2 {
27 print STDERR color('yellow'),@_,color('reset');
29 sub print3 {
30 print STDERR color('red'),@_,color('reset');
33 sub print_path {
34 my ($pre,$path,$suf,$sep) = @_;
35 $sep = '->' unless($sep);
36 if(@{$path}) {
37 $pre = "" unless($pre);
38 $suf = "" unless($suf);
39 print1 $pre,join($sep,@{$path}),$suf;
43 sub print_status {
44 if(@_) {
45 print1 "\t[OK]\n";
47 else {
48 print3 "\t[Failed]\n";
52 sub get_data {
53 my $CONFIG=shift;
54 my @r=$CONFIG->list(@_);
55 return sort @r;
58 sub print_target {
59 my $pre=shift;
60 my $suf=shift;
61 my $CONFIG=shift;
62 my @r=$CONFIG->list(@_);
63 $pre="" unless($pre);
64 $suf="\n" unless(defined $suf);
65 foreach(sort @r) {
66 print1 $pre,$_,$suf;
70 my $CONFIG;
71 my $database;
72 my $opt = shift @ARGV;
73 if($opt and $opt eq '-f') {
74 $database = shift @ARGV;
76 else {
77 unshift @ARGV,$opt;
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)$/) {
85 $query = $action;
86 $action = 'query';
91 if($action eq 'add' and !$query) {
92 print3 "Invalid usage\n";
93 print2 "Usage:\n\tr-xconfig add keys...|values...\n";
94 exit 1;
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);
107 exit 0;
110 if($action eq 'add') {
111 my @keys;
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));
118 else {
119 my @target = $CONFIG->query($query);
120 if(!@target) {
121 print3 "query match nothing\n";
122 exit 2;
124 $!=undef;
125 if($action eq 'list') {
126 print_target("","\n",$CONFIG,@target);
127 exit 0;
129 elsif($action eq 'print') {
130 print $_,"\n" foreach(&get_data($CONFIG,@target));
131 exit 0;
133 elsif($action eq 'read') {
134 my @r=$CONFIG->read(@target);
135 foreach(@r) {
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);
151 foreach(@records) {
152 print_path('',$_,"\n","->");
156 if($!) {
157 print3 "$!\n";
158 exit 1;
161 if($CONFIG->{dirty}) {
162 print2 "Saving configs to $database";
163 print_status($CONFIG->write_plainfile($database));
165 exit 0;