Changeset 29672


Ignore:
Timestamp:
2015-01-07T14:49:26+13:00 (9 years ago)
Author:
Jeremy Symon
Message:

Added command to add a servlet. Config is given as commandline options. Variable numbers of arguments (such as init-params) are terminated with \; (similar to find -exec)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/package-kits/linux/files/servlet.pl

    r29671 r29672  
    165165#      [1]: Function that is run for the command
    166166#      [2]: Number of arguments of the command (if any)
     167#      [3]: True if the command has a variable number of args
    167168%commands = (
    168169    help => [
     
    182183    ],
    183184    read => [
    184         'Parses XML from a file into the internal state',
     185        "Parses XML from a file into the internal state\n  input_file (- for STDIN)",
    185186        sub {
    186187            my $file = shift @ARGV;
     
    206207                        $hash->{$key} = [ $hash->{$key} ];
    207208                    }
    208                     if (ref $new->{$key} eq 'ARRAY') {
    209                         for my $elem (@{$new->{$key}}) {
    210                             push @{$hash->{$key}}, $elem;
    211                         }
    212                     } else {
    213                         push @{$hash->{$key}}, $new->{$key};
    214                     }
     209                    push @{$hash->{$key}},
     210                        (ref $new->{$key} eq 'ARRAY' ? @{$new->{$key}} : $new->{$key});
    215211                } else {
    216212                    $hash->{$key} = $new->{$key};
     
    221217    ],
    222218    write => [
    223         'Writes the current internal state as XML to a file',
     219        "Writes the current internal state as XML to a file\n  output_file (- for STDOUT)",
    224220        sub {
    225221            my $file = shift @ARGV;
     
    287283        1,
    288284    ],
     285    add => [
     286        "Adds a new servlet to the current internal state\n  name description class [param=value param2=value...] ;",
     287        sub {
     288            my %servlet = (
     289                'servlet-name' => shift @ARGV,
     290                'description'  => shift @ARGV,
     291                'servlet-class'=> shift @ARGV,
     292                'init-param'   => [],
     293            );
     294            while (@ARGV) {
     295                my $param = shift @ARGV;
     296                $param eq ';' and last;
     297                my ($key, $value) = split '=', $param, 2;
     298                (defined $key and defined $value) or die "Expected params in form 'param=value'\n";
     299                push @{$servlet{'init-param'}}, {
     300                    'param-name'  => $key,
     301                    'param-value' => $value,
     302                };
     303            }
     304            if (defined $hash->{servlet}) {
     305                unless (ref $hash->{servlet} eq 'ARRAY') {
     306                    $hash->{servlet} = [ $hash->{servlet} ];
     307                }
     308                push @{$hash->{servlet}}, \%servlet;
     309            } else {
     310                $hash->{servlet} = \%servlet;
     311            }
     312        },
     313        3,
     314        1,
     315    ],
    289316);
    290317
    291318# Check that all given commands are valid
    292319my $argc = 0;
     320my $varargs = 0;
    293321for my $cmd (@ARGV) {
    294     if ($argc > 0) {
     322    if ($varargs and $cmd eq ';') {
     323        $varargs = 0;
     324    } elsif ($argc > 0) {
    295325        # skip arguments to a previous command
    296326        $argc --;
     327    } elsif ($varargs) {
    297328    } elsif (defined $commands{$cmd}) {
    298329        # get the argument count of a valid command
    299330        $argc = @{$commands{$cmd}}[2];
    300331        defined $argc or ($argc = 0);
     332        $varargs = @{$commands{$cmd}}[3];
    301333    } else {
    302334        # invalid command
     
    306338    }
    307339}
    308 $argc > 0 and die "Expected $argc more argument" . ($argc > 1 ? "s" : "") . "\n";
     340$argc != 0 and die "Expected $argc more argument" . ($argc != 1 ? "s" : "") . "\n";
     341$varargs and die "Unclosed vararg command. Add an argument ';' to close the varargs\n";
    309342
    310343# Run the commands
Note: See TracChangeset for help on using the changeset viewer.