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
135
136
137
138
|
#!/usr/bin/perl
# $Id$
use strict;
use warnings;
use MDV::Distribconf::Build;
use Term::ReadLine;
use Text::ParseWords;
use Getopt::Long;
my @distribs;
my @selected;
# list of availlable command
# name => coderef
my $commands = {
load => sub {
if (grep { /-h|--help/ } @_) {
print "unload path1 [path2 [...]]\n";
print "Try to load distrib configuration from given path\n";
return;
}
foreach (@_) {
my $dbuild = MDV::Distribconf::Build->new($_);
$dbuild->load() or do {
print STDERR "Can't load distrib from $_\n";
next;
};
push(@distribs, $dbuild);
}
},
unload => sub {
if (grep { /-h|--help/ } @_) {
print "unload ID1 [ID2 [...]]\n";
print "Unload tree\n";
return;
}
my @toremove = @_;
my @new;
foreach my $id (0 .. $#distribs) {
grep { $id == $_ } @toremove and next;
push(@new, $distribs[$id]);
}
@distribs = @new;
},
list => sub {
if (grep { /-h|--help/ } @_) {
print "list current load distrib tree\n";
return;
}
foreach (0 .. $#distribs) {
my $d = $distribs[$_];
printf "%3d %s\n", $_, $d->getpath(undef, "root");
}
},
show => sub {
local @ARGV = @_;
GetOptions(
'h|help' => \my $help,
'm=s' => \my @medias,
'a' => \my $allmedia,
);
if ($help) {
print "show [-m medianame [-m ...]] [-a] value\n";
print "show a value for each distrib\n";
print " -a list a value for all media\n";
print " -m show the value only for given media\n";
print "Default action is to give the global value\n";
return;
}
foreach (0 .. $#distribs) {
my $d = $distribs[$_];
printf "%3d %s\n", $_, $d->getpath(undef, "root");
my $m;
if ($allmedia) {
$m = [ $d->listmedia ];
} elsif (@medias) {
$m = \@medias;
}
if ($m) {
foreach my $med (@{$m}) {
foreach my $var (@ARGV) {
printf
" %10s [%10s]: %s\n",
$var,
$med ? $med : "(global)",
$d->getvalue($med, $var);
}
}
} else {
foreach my $var (@ARGV) {
printf
" %10s: %s\n",
$var,
$d->getvalue(undef, $var);
}
}
}
},
addmedia => sub {
local @ARGV = @_;
my ($m, $name) = @ARGV;
foreach (@distribs) {
$_->setvalue($m);
}
},
};
$commands->{load}->(@ARGV);
my $term = new Term::ReadLine 'editdistrib';
while ( defined ($_ = $term->readline('> ')) ) {
my ($command, @arg) = &shellwords($_);
if ($command eq 'help') {
print "Availlable commands:\n ";
print join(' ', sort keys %$commands) . "\n";
next;
}
if (defined($commands->{$command})) {
$commands->{$command}->(@arg);
} else {
print STDERR "Unkown command\n";
}
}
|