1 | #!/usr/bin/env perl
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use warnings;
|
---|
5 | use utf8;
|
---|
6 |
|
---|
7 | use Data::Dumper;
|
---|
8 | use POSIX 'isatty';
|
---|
9 |
|
---|
10 | use lib 'perllib';
|
---|
11 | use Greenstone::XML::Tidy;
|
---|
12 |
|
---|
13 | my $hash = Hash::Ordered->new;
|
---|
14 | my %commands;
|
---|
15 | # Command structure:
|
---|
16 | # key: Case-insensitive name of the command
|
---|
17 | # val: Array of info about the command
|
---|
18 | # [0]: Description of the command
|
---|
19 | # [1]: Function that is run for the command
|
---|
20 | # [2]: Number of arguments of the command (if any)
|
---|
21 | # [3]: True if the command has a variable number of args
|
---|
22 | %commands = (
|
---|
23 | help => [
|
---|
24 | 'Prints help about the available commands',
|
---|
25 | sub {
|
---|
26 | for my $cmd (sort keys %commands) {
|
---|
27 | printf "%-10s %s\n", $cmd, $commands{$cmd}[0];
|
---|
28 | }
|
---|
29 | },
|
---|
30 | ],
|
---|
31 | clear => [
|
---|
32 | 'Clears the internal state',
|
---|
33 | sub {
|
---|
34 | $hash = Hash::Ordered->new;
|
---|
35 | }
|
---|
36 | ],
|
---|
37 | read => [
|
---|
38 | "Parses XML from a file into the internal state\n input_file (- for STDIN)",
|
---|
39 | sub {
|
---|
40 | my $new = read_xml shift @ARGV;
|
---|
41 | # Append the new data to the current data
|
---|
42 | for my $key ($new->keys) {
|
---|
43 | if ($key eq '.attr' and $hash->exists ('.attr')) {
|
---|
44 | my $existing_attr = $hash->get ('.attr');
|
---|
45 | my $new_attr = $new->get ('.attr');
|
---|
46 | for my $attr ($new_attr->keys) {
|
---|
47 | $existing_attr->set ($attr => $new_attr->get ($attr));
|
---|
48 | }
|
---|
49 | } elsif ($hash->exists ($key)) {
|
---|
50 | my $existing = $hash->get ($key);
|
---|
51 | if (ref $existing ne 'ARRAY') {
|
---|
52 | $existing = [ $existing ];
|
---|
53 | $hash->set ($key => $existing);
|
---|
54 | }
|
---|
55 | my $new_val = $new->get ($key);
|
---|
56 | push @{$existing},
|
---|
57 | (ref $new_val eq 'ARRAY' ? @{$new_val} : $new_val);
|
---|
58 | } else {
|
---|
59 | $hash->set ($key => $new->get ($key));
|
---|
60 | }
|
---|
61 | }
|
---|
62 | },
|
---|
63 | 1,
|
---|
64 | ],
|
---|
65 | write => [
|
---|
66 | "Writes the current internal state as XML to a file\n output_file (- for STDOUT)",
|
---|
67 | sub {
|
---|
68 | write_xml $hash, shift @ARGV;
|
---|
69 | },
|
---|
70 | 1,
|
---|
71 | ],
|
---|
72 | debug => [
|
---|
73 | 'Dumps the current internal state to stdout',
|
---|
74 | sub {
|
---|
75 | print Dumper $hash;
|
---|
76 | },
|
---|
77 | ],
|
---|
78 | count => [
|
---|
79 | 'Returns the number of servlets in the current internal state',
|
---|
80 | sub {
|
---|
81 | my $count = 0;
|
---|
82 | if (exists $hash->{servlet}) {
|
---|
83 | $count = 1;
|
---|
84 | my $servlets = $hash->{servlet};
|
---|
85 | if (ref $servlets eq 'ARRAY') {
|
---|
86 | $count = scalar @{$hash->{servlet}};
|
---|
87 | }
|
---|
88 | }
|
---|
89 | print $count, "\n";
|
---|
90 | },
|
---|
91 | ],
|
---|
92 | list => [
|
---|
93 | 'Lists the servlets in the current internal state',
|
---|
94 | sub {
|
---|
95 | if (exists $hash->{servlet}) {
|
---|
96 | my $servlets = $hash->{servlet};
|
---|
97 | if (ref $servlets eq 'ARRAY') {
|
---|
98 | for my $servlet (@{$hash->{servlet}}) {
|
---|
99 | print $servlet->{'servlet-name'}, "\n";
|
---|
100 | }
|
---|
101 | } else {
|
---|
102 | print $servlets->{'servlet-name'}, "\n";
|
---|
103 | }
|
---|
104 | } else {
|
---|
105 | print STDERR "No servlets found. Was valid XML provided?\n";
|
---|
106 | }
|
---|
107 | },
|
---|
108 | ],
|
---|
109 | remove => [
|
---|
110 | 'Removes a servlet from the current internal state',
|
---|
111 | sub {
|
---|
112 | my $name = shift @ARGV;
|
---|
113 | if (exists $hash->{servlet}) {
|
---|
114 | my $servlets = $hash->{servlet};
|
---|
115 | if (ref $servlets eq 'ARRAY') {
|
---|
116 | my @array = grep { $_->{'servlet-name'} ne $name } @$servlets;
|
---|
117 | $hash->{servlet} = \@array;
|
---|
118 | } elsif ($servlets->{'servlet-name'} eq $name) {
|
---|
119 | delete $hash->{servlet};
|
---|
120 | }
|
---|
121 | }
|
---|
122 | },
|
---|
123 | 1,
|
---|
124 | ],
|
---|
125 | add => [
|
---|
126 | "Adds a new servlet to the current internal state\n name description class [param=value param2=value...] ;",
|
---|
127 | sub {
|
---|
128 | my %servlet = (
|
---|
129 | 'servlet-name' => shift @ARGV,
|
---|
130 | 'description' => shift @ARGV,
|
---|
131 | 'servlet-class'=> shift @ARGV,
|
---|
132 | 'init-param' => [],
|
---|
133 | );
|
---|
134 | while (@ARGV) {
|
---|
135 | my $param = shift @ARGV;
|
---|
136 | $param eq ';' and last;
|
---|
137 | my ($key, $value) = split '=', $param, 2;
|
---|
138 | (defined $key and defined $value) or die "Expected params in form 'param=value'\n";
|
---|
139 | push @{$servlet{'init-param'}}, {
|
---|
140 | 'param-name' => $key,
|
---|
141 | 'param-value' => $value,
|
---|
142 | };
|
---|
143 | }
|
---|
144 | if (exists $hash->{servlet}) {
|
---|
145 | unless (ref $hash->{servlet} eq 'ARRAY') {
|
---|
146 | $hash->{servlet} = [ $hash->{servlet} ];
|
---|
147 | }
|
---|
148 | push @{$hash->{servlet}}, \%servlet;
|
---|
149 | } else {
|
---|
150 | $hash->{servlet} = \%servlet;
|
---|
151 | }
|
---|
152 | },
|
---|
153 | 3,
|
---|
154 | 1,
|
---|
155 | ],
|
---|
156 | );
|
---|
157 |
|
---|
158 | # Check that all given commands are valid
|
---|
159 | my $argc = 0;
|
---|
160 | my $varargs = 0;
|
---|
161 | for my $cmd (@ARGV) {
|
---|
162 | if ($varargs and $cmd eq ';') {
|
---|
163 | $varargs = 0;
|
---|
164 | } elsif ($argc > 0) {
|
---|
165 | # skip arguments to a previous command
|
---|
166 | $argc --;
|
---|
167 | } elsif ($varargs) {
|
---|
168 | } elsif (exists $commands{$cmd}) {
|
---|
169 | # get the argument count of a valid command
|
---|
170 | $argc = @{$commands{$cmd}}[2];
|
---|
171 | defined $argc or ($argc = 0);
|
---|
172 | $varargs = @{$commands{$cmd}}[3];
|
---|
173 | } else {
|
---|
174 | # invalid command
|
---|
175 | print STDERR "Valid commands are:\n";
|
---|
176 | @{$commands{help}}[1]->();
|
---|
177 | die "Invalid command '" . $cmd . "'\n";
|
---|
178 | }
|
---|
179 | }
|
---|
180 | $argc != 0 and die "Expected $argc more argument" . ($argc != 1 ? "s" : "") . "\n";
|
---|
181 | $varargs and die "Unclosed vararg command. Add an argument ';' to close the varargs\n";
|
---|
182 |
|
---|
183 | # Run the commands
|
---|
184 | while (@ARGV) {
|
---|
185 | @{$commands{lc shift}}[1]->();
|
---|
186 | }
|
---|