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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
#!/usr/bin/perl
# $Id$
use strict;
use warnings;
use MDV::Distribconf::Build;
use Term::ReadLine;
use Text::ParseWords;
use Getopt::Long;
my @distribs;
# list of availlable command
# name => coderef
my $commands = {
load => sub {
my ($param, @path) = @_;
if ($param->{current}{help}) {
print "unload path1 [path2 [...]]\n";
print "Try to load distrib configuration from given path\n";
return;
}
foreach (@path) {
my $dbuild = MDV::Distribconf::Build->new($_);
$dbuild->load() or do {
print STDERR "Can't load distrib from $_\n";
next;
};
push(@distribs, $dbuild);
}
},
unload => sub {
my ($param, @ids) = @_;
if ($param->{current}{help}) {
print "unload ID1 [ID2 [...]]\n";
print "Unload tree\n";
return;
}
my @new;
foreach my $id (0 .. $#distribs) {
grep { $id == $_ } @ids and next;
push(@new, $distribs[$id]);
}
@distribs = @new;
$param->{selected} = undef;
},
list => sub {
my ($param) = @_;
if ($param->{current}{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 {
my ($param) = shift(@_);
local @ARGV = @_;
GetOptions(
'm=s' => \my @medias,
'a' => \my $allmedia,
);
if ($param->{current}{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 (@{$param->{distrib}}) {
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 {
my ($param, $m, $name) = @_;
foreach (@{$param->{distrib}}) {
$distribs[$_]->setvalue($m);
}
},
delmedia => sub {
my ($param, $m) = @_;
foreach (@{$param->{distrib}}) {
$distribs[$_]->delvalue($m);
}
},
sel => sub {
my $param = shift(@_);
local @ARGV = @_;
GetOptions(
e => \my $empty,
);
if ($empty) {
$param->{selected} = {};
}
foreach (@ARGV) {
if ($_ < 0 || $_ > $#distribs) {
next;
}
$param->{selected}{$_} = 1;
}
},
set => sub {
my $param = shift(@_);
local @ARGV = @_;
GetOptions(
'm=s' => \my $media,
'a' => \my $allmedia,
);
my ($var, $val) = @ARGV;
foreach my $d (@{$param->{distrib}}) {
if ($media || $allmedia) {
foreach my $m ($distribs[$d]->listmedia) {
if ($media && $m ne $media) {
next;
}
$distribs[$d]->setvalue($m, $var, $val);
}
} else {
$distribs[$d]->setvalue(undef, $var, $val);
}
}
},
del => sub {
my $param = shift(@_);
local @ARGV = @_;
GetOptions(
'm=s' => \my $media,
'a' => \my $allmedia,
);
my ($var) = @ARGV;
foreach my $d (@{$param->{distrib}}) {
if ($media || $allmedia) {
foreach my $m ($distribs[$d]->listmedia) {
if ($media && $m ne $media) {
next;
}
$distribs[$d]->delvalue($m, $var);
}
} else {
$distribs[$d]->delvalue(undef, $var);
}
}
},
check => sub {
my $param = shift(@_);
local @ARGV = @_;
GetOptions(
);
foreach my $d (@{$param->{distrib}}) {
printf "%3d %s\n", $d, $distribs[$d]->getpath(undef, "root");
$distribs[$d]->check();
}
},
save => sub {
my $param = shift(@_);
local @ARGV = @_;
GetOptions(
);
foreach my $d (@{$param->{distrib}}) {
$distribs[$d]->write_mediacfg();
}
},
};
$commands->{load}->(undef, @ARGV);
my $session_param;
my $term = new Term::ReadLine 'editdistrib';
my $prompt = '(all) > ';
while ( defined ($_ = $term->readline($prompt)) ) {
local @ARGV = &shellwords($_);
my $command = shift(@ARGV);
$command ||= ""; # avoid undef
if ($command eq 'help') {
print "Availlable commands:\n ";
print join(' ', sort keys %$commands) . "\n";
next;
}
$session_param->{current} = {};
my $gl = new Getopt::Long::Parser config => [ qw(pass_through no_auto_abbrev) ];
$gl->getoptions(
all => \$session_param->{current}{all},
'h|help' => \$session_param->{current}{help},
);
if ($session_param->{current}{all} || ! $session_param->{selected}) {
@{$session_param->{distrib}} = (0 .. $#distribs);
} else {
@{$session_param->{distrib}} = keys %{$session_param->{selected}};
}
if (defined($commands->{$command})) {
$commands->{$command}->($session_param, @ARGV);
} else {
print STDERR "Unkown command\n";
}
$prompt = sprintf("(%s) > ",
$session_param->{selected} ? join(' ', sort keys %{$session_param->{selected}}) : "all");
}
|